Comprehensive cheatsheet of Docker commands

  • avatar
  • 7 mins read

In this post, we examine various Docker commands and concepts that are frequently used to manage Docker containers in local development environment. We provide a detailed analysis of how these essential tools function and give integration examples. Whether you're a novice or an experienced Docker user, our post can offer valuable insights and tips for effectively managing Docker containers on your machine.

Listing available commands

You can discover all the available commands for each Docker entity.

#list available container commands
docker container

#list available image commands
docker image

#list available volume commands
docker volume

#list available network commands
docker network

Docker management commands

To manage Docker containers, the following commands are the most commonly used:

#list running and stopped containers
#omitting the flag will only list running containers
docker ps -a

#stop one or more running container by name or id
#when running a container with the --rm flag, stopping the container will auto remove it
docker stop container_name_or_id

#remove one or more stopped container by name or id
docker rm container_name_or_id

#stop all running containers
docker stop $(docker ps -q)

#remove all stopped containers
docker container prune -f

And for Docker images:

#list all images on host machine
docker images

#remove all images (either command will work)
docker image prune -a -f
docker rmi $(docker images -q)

Here are a few system commands that can also come in handy:

#show all docker stats about images and containers on the host
docker system info

#remove unused docker resources.
docker system prune

The inspect command

The inspect command dumps the configuration of images and containers.

#inspect the build time configuration of an image
docker inspect image_name_or_id

#inspect the runtime configuration of a container
docker inspect container_name_or_id

Examples of inspecting images:

docker inspect nginx
docker inspect nginx:latest
docker inspect nginx:1.23

Running a container

To run a container using the Ubuntu image as an example, the simplest way would be:

docker run ubuntu

This is equivalent to running it with the default tag:

docker run ubuntu:latest

It is a good practice to specify the tag. This is because using latest or omitting the tag (which defaults to latest) could result in a different version of the image being used depending on the time when the command is executed. Using a specific tag ensures that we are always getting the same image.

To specify the tag, you should use the following syntax:

docker run ubuntu:22.04

In the given examples, the default entrypoint/command of the image will be executed because we do not specify any command after the image name. Whenever a container is started, it is assigned a unique container ID that persists even after the container is stopped.

Command options

Typically, we use a set of standard options when running containers.

The --rm tag auto remove the container after it has been stoped.

docker run --rm ubuntu:22.04

Using the --name tag allows us to assign a name to a running or stopped container, which can be used as a reference instead of the container ID. It is important to note that only one container with a specific name can be running at a time. Therefore, attempting to use the same name twice in a docker run command will result in an error.

docker run --rm --name my_ubuntu ubuntu:22.04

The -it option is used to run the container in interactive mode ('i' option) and establish communication with the container via the teletype command-line interface ('t' option). These options are combined to enable interaction with the running container.

docker run --rm --name my_ubuntu -it ubuntu:22.04

Executing this command will place you inside the Ubuntu container. Type exit to quit the container.

The -w option can be used to set the working directory within the container.

docker run --rm --name my_ubuntu -it -w /home ubuntu:22.04

In this case, it changes the current directory to the /home directory when the container starts.

Running a container in the background

The -d option is used to run the container in detached mode. If the container is started without this option, it runs in the foreground by default. In this case, the control does not return to the terminal if the container continues to run.

This option is especially useful when using a web server image that needs to continue running after starting the container. With the -d option, the control returns to the terminal on the host while the container runs in the background.

docker run --rm --name my_nginx -d -p 8080:80 nginx

Since the -d option does not allow interaction with the container, we can use the the exec command to connect to the running container using its name or ID.

docker exec my_nginx bash -it

Note that with the Nginx image, we need to explicitly run the 'bash' command because the image does not run it by default.

Docker mapping

Docker mapping is a mechanism used create a connection between the host machine and the container. It allows services running inside a Docker container to be accessible from the host machine and the outside world.

Port Mapping

When we start a container, we can specify the ports that should be exposed to the host machine by using the -p option. For example, we can map port 80 on the container to port 8080 on the host machine by running:

docker run --rm --name my_nginx -d -p 8080:80 nginx

This maps port 80 on the container to port 8080 on the host machine. So any requests to port 8080 on the host machine will be forwarded to port 80 inside the container. Multiple ports can be mapped at the same time.

Volume Mapping

Volume mapping involves mapping directories or files on the host machine to directories or files inside the container, allowing for persistent data storage. This is useful when you need to preserve data between container runs or when you have multiple containers that need to share data. It can also help you backup your data by simply backing up the directory or file on the host machine.

To create a volume mapping, the -v option can be utilized:

docker run --rm --name my_nginx -d -p 8080:80 -v $(pwd)/html:/var/www/html nginx

The ${pwd} syntax allows you to retrieve the current directory similar to a dot. So, this will map the html folder within the current directory on the host to the /var/www/html directory in the container.

Additionally, we can make the content mapped into the container read-only, which means that the container cannot modify the content.

docker run --rm --name my_nginx -d -p 8080:80 -v $(pwd)/html:/var/www/html:ro nginx

Note that you can specify multiple mappings in the same command. Make sure the destination directory exists, otherwise there will be a runtime error.

 Join Our Monthly Newsletter

Get the latest news and popular articles to your inbox every month

We never send SPAM nor unsolicited emails


Leave a Reply

Your email address will not be published.

Replying to the message: View original

Hey visitor! Unlock access to featured articles, remove ads and much more - it's free.