Docker Cheat Sheet for DevOps Engineers | Day 20 of 90 Days of DevOps
In today’s blog, we’re crafting a Docker CLI commands cheat sheet. These commands will be categorized into: Running a New Container, Managing Containers, Managing Images, and Info & Stats.
Let’s dive into each category step by step.
Run a new container
To run a new container from an image, use the docker run
command with the name or ID of the image. For example:
# Run a container from the ubuntu image and open a shell inside it
docker run -it ubuntu bash
# Run a container from the nginx image and expose port 80 on the host machine
docker run -p 80:80 nginx
# Run a container from the alpine image in detached mode (background)
docker run -d alpine tail -f /dev/null
You can also use various flags and options to customize your container, such as:
--name
to give a custom name to your container--rm
to automatically remove the container when it exits--env
or-e
to set environment variables for your container--volume
or-v
to mount volumes or bind mounts on your container--network
or--net
to connect your container to a network--user
or-u
to run your container as a specific user--restart
to specify a restart policy for your container
For more information and options, see docker run reference.
Manage containers
To manage your existing containers, use the following commands:
docker ps
to list all running containersdocker ps -a
to list all containers (running and stopped)docker start <container>
to start a stopped containerdocker stop <container>
to stop a running containerdocker restart <container>
to restart a containerdocker kill <container>
to kill a container by sending a SIGKILL signaldocker rm <container>
to remove a stopped containerdocker rm -f <container>
to force-remove a running container
You can use the name or ID of the container as the argument for these commands. You can also use multiple arguments to perform the same action on multiple containers. For example:
# Start two containers named foo and bar
docker start foo bar
# Stop and remove all containers
docker rm -f $(docker ps -aq)
To execute commands inside a running container, use the docker exec
command with the name or ID of the container and the command you want to run. For example:
# Run a shell inside a container named my-container
docker exec -it my-container sh
# Print the working directory of a container named my-container
docker exec my-container pwd
You can use the -it
flag to attach an interactive terminal to the container, which is useful for running shells or interactive commands.
To copy files or directories between a container and the host machine, use the docker cp
command with the name or ID of the container and the source and destination paths. For example:
# Copy a file from the host machine to a container named my-container
docker cp foo.txt my-container:/tmp/foo.txt
# Copy a directory from a container named my-container to the host machine
docker cp my-container:/var/log /tmp/log
To view the logs of a container, use the docker logs
command with the name or ID of the container. For example:
# View the logs of a container named my-container
docker logs my-container
# View and follow the logs of a container named my-container
docker logs -f my-container
# View the logs of a container named my-container with timestamps
docker logs -t my-container
You can use various flags and options to customize the output of this command, such as:
-f
or--follow
to stream the logs continuously-t
or--timestamps
to add timestamps to each line--since
to show logs since a specific time or duration--tail
to show only the last N lines of logs
For more information and options, see docker logs reference.
To inspect the details of a container, such as its configuration, state, network settings, mounts, etc., use the docker inspect
command with the name or ID of the container. For example:
# Inspect a container named my-container
docker inspect my-containe
# Inspect multiple containers named foo and bar
docker inspect foo ba
# Inspect only the IP address of a container named my-container
docker inspect -f '{{.NetworkSettings.IPAddress}}' my-container
You can use filters or formats to display specific fields or values from the output, which is in JSON format. For more information and options, see docker inspect reference.
Manage images
To manage your images, use the following commands:
docker images
to list all images on your machinedocker pull <image>
to download an image from a registrydocker push <image>
to upload an image to a registrydocker rmi <image>
to remove an image from your machinedocker rmi -f <image>
to force-remove an image from your machinedocker build -t <image> <path>
to build an image from a Dockerfiledocker tag <source_image> <target_image>
to create a tag for an imagedocker history <image>
to show the history of an image
You can use the name or ID of the image as the argument for these commands. You can also use multiple arguments to perform the same action on multiple images. For example:
# Pull two images from Docker Hub
docker pull ubuntu alpine
# Remove all images
docker rmi $(docker images -q)
To save an image to a tar archive, use the docker save
command with the name or ID of the image and the output file name. For example:
# Save an image named my-image to a file named my-image.tar
docker save -o my-image.tar my-image
To load an image from a tar archive, use the docker load
command with the input file name. For example:
# Load an image from a file named my-image.tar
docker load -i my-image.tar
To export a container’s filesystem as a tar archive, use the docker export
command with the name or ID of the container and the output file name. For example:
# Export a container named my-container to a file named my-container.tar
docker export -o my-container.tar my-container
To import the contents from a tar archive to create a filesystem image, use the docker import
command with the input file name and the output image name. For example:
# Import a file named my-container.tar as an image named my-image
docker import my-container.tar my-image
Info & Stats
To display system-wide information about Docker, such as version, number of containers and images, storage driver, etc., use the docker info
command. For example:
# Display system-wide information about Docker
docker info
To display version information about Docker, such as client and server versions, API versions, Go version, OS/Arch, etc., use the docker version
command. For example:
# Display version information about Docker
docker version
To display resource usage statistics of running containers, such as CPU, memory, network, block I/O, etc., use the docker stats
command with the name or ID of the containers. For example:
# Display resource usage statistics of all running containers
docker stats
# Display resource usage statistics of two containers named foo and bar
docker stats foo bar
# Display resource usage statistics of all running containers in a table format
docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"
You can use various flags and options to customize the output of this command, such as:
--all
or-a
to show all containers (default shows just running)--no-stream
to disable streaming stats and only pull the first result--format
to pretty-print stats using a Go template
For more information and options, see docker stats reference.
Conclusion
This is the end of the Docker cheat sheet for DevOps engineers. I hope you find it helpful and handy. If you have any questions or feedback, please feel free to leave a comment below.
Thank you for reading and happy learning! 😊
P.S. If you are interested in checking out my GitHub repository and portfolio, please visit these links: GitHub and LinkedIn.
I appreciate your feedback and suggestions. Thank you! 🙏