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:
docker run -it ubuntu bash
docker run -p 80:80 nginx
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 containers
docker ps -a to list all containers (running and stopped)
docker start <container> to start a stopped container
docker stop <container> to stop a running container
docker restart <container> to restart a container
docker kill <container> to kill a container by sending a SIGKILL signal
docker rm <container> to remove a stopped container
docker 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:
docker start foo bar
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:
docker exec -it my-container sh
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:
docker cp foo.txt my-container:/tmp/foo.txt
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:
docker logs my-container
docker logs -f my-container
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:
docker inspect my-container
docker inspect foo bar
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 machine
docker pull <image> to download an image from a registry
docker push <image> to upload an image to a registry
docker rmi <image> to remove an image from your machine
docker rmi -f <image> to force-remove an image from your machine
docker build -t <image> <path> to build an image from a Dockerfile
docker tag <source_image> <target_image> to create a tag for an image
docker 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:
docker pull ubuntu alpine
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:
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:
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:
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:
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:
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:
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:
docker stats
docker stats foo bar
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! 🙏