Docker Commands Cheat Sheet with Examples

Hakan Bayraktar
4 min readAug 22, 2024

--

1. Docker Container Lifecycle Commands

docker create

Creates a new container without starting it.

docker create --name my_container nginx

docker run

Creates and starts a new container.

docker run -d --name my_container nginx

docker pause

Pauses a running container.

docker pause my_container

docker unpause

Unpauses a paused container.

docker unpause my_container

docker stop

Stops a running container.

docker stop my_container

docker start

Starts a stopped container.

docker start my_container

docker restart

Restarts a container.

docker restart my_container

docker attach

Attaches to a running container’s terminal.

docker attach my_container

docker wait

Waits for a container to stop and prints its exit code.

docker wait my_container

docker rm

Removes a container. The container must be stopped before removing.

docker rm my_container

docker kill

Forcefully stops a running container.

docker kill my_container

2. Docker Image Commands

docker build

Builds an image from a Dockerfile.

docker build -t my_image .

docker pull

Pulls an image from Docker Hub.

docker pull nginx

docker push

Pushes an image to Docker Hub.

docker push my_image

docker images

Lists local Docker images.

docker images

To filter Docker Images list

docker images — filter “<key>=<value>”

Below are some “–filter” options

  • dangling– Images are not used
  • label– List the Docker Images those you added a label
  • before– List the Docker Images which is created in specific time
  • since– Created in specific time with another image creation
  • reference — List Docker Images which has name or Tag

To list Docker Images which is not used or Dangling docker images

docker images -a

docker rmi

Removes a Docker image.

docker rmi my_image

docker tag

Tags an image with a new name or version.

docker tag my_image my_repo/my_image:v1

docker history

Displays the history of an image.

docker history nginx

docker inspect

Provides detailed information about an image or container.

docker inspect my_image

docker save

Saves an image to a tar archive.

docker save -o my_image.tar my_image

docker load

Loads an image from a tar archive.

docker load -i my_image.tar

docker export

Exports a container’s filesystem as a tar archive.

docker export my_container -o my_container.tar

docker import

Imports a tarball to create an image.

docker import my_container.tar

3. Docker Container Commands

docker ps

Lists running containers. Use -a to see all containers.

docker ps -a

docker exec

Runs a command inside a running container.

docker exec -it my_container /bin/bash

docker logs

Shows the logs from a container.

docker logs my_container

docker rename

Renames a container.

docker rename old_name new_name

docker inspect

Provides detailed information about a container.

docker inspect my_container

docker cp

Copies files or directories between a container and the local filesystem.

docker cp my_container:/path/to/file /local/path

4. Docker Compose Commands

Docker Compose is a tool used to define and manage multi-container Docker applications. In earlier versions, it was a standalone tool invoked using docker-compose. However, in newer versions of Docker, Compose is bundled directly within Docker itself, and the command is now docker compose (note the space instead of a hyphen). This integration simplifies usage, as no separate installation is needed for Docker Compose.

Now, let’s explore some essential Docker Compose commands.

docker-compose build

Builds services defined in the Compose file.

docker-compose build

docker-compose up

Starts containers as defined in the Compose file.

docker-compose up -d

docker-compose down

Stops and removes containers, networks, and volumes created by up.

docker-compose down

docker-compose ls

Lists active Compose projects.

docker-compose ls

docker-compose start

Starts existing containers defined in the Compose file.

docker-compose start

docker-compose run

Runs a one-off command against a service.

docker-compose run service_name

docker-compose rm

Removes stopped service containers.

docker-compose rm

docker-compose ps

Lists containers for a Compose project.

docker-compose ps

5. Docker Volume Commands

docker volume create

Creates a new volume.

docker volume create my_volume

docker volume inspect

Displays detailed information about a volume.

docker volume inspect my_volume

docker volume rm

Removes a volume.

docker volume rm my_volume

6. Docker Networking Commands

docker network create

Creates a new network.

docker network create my_network

docker network ls

Lists all networks.

docker network ls

docker network inspect

Displays detailed information about a network.

docker network inspect my_network

7. Docker Logs and Monitoring Commands

docker logs

Shows the logs of a container.

docker logs my_container

docker top

Displays running processes inside a container.

docker top my_container

docker stats

Shows live resource usage statistics of containers.

docker stats my_container

docker events

Monitors Docker events in real-time.

docker events

docker port

Displays the public ports mapped by a container.

docker port my_container

8. Docker Hub Commands

docker login

Authenticates your Docker client to the Docker Hub.

docker login

First login to your Docker Private Registry URL, UserName and Password

docker login docker.example.com — username=USERNAME

docker logout

Logs out from Docker Hub.

docker logout

docker search

Searches Docker Hub for images.

docker search nginx

docker pull

Pulls an image from Docker Hub.

docker pull ubuntu

docker push

Pushes an image to Docker Hub.

docker push my_image

docker tag

Tags an image with a new name before pushing it to Docker Hub.

docker tag my_image my_repo/my_image:v1

9. Docker Prune Commands

docker system prune

Removes all unused containers, networks, images, and optionally, volumes.

docker system prune -a

docker image prune

Removes unused Docker images.

docker image prune

docker container prune

Removes all stopped containers.

docker container prune

docker volume prune

Removes all unused volumes.

docker volume prune

docker network prune

Removes all unused networks.

docker network prune

This cheat sheet covers essential Docker commands, grouped into relevant categories. These commands should help you manage Docker containers, images, volumes, networks, and more effectively.

--

--