Docker Cheat Sheet for DevOps Engineers | Day 20 of 90 Days of DevOps

👩💻 As a seasoned software engineer with 7+ years of experience, my expertise includes:
- 🌐 Developing web applications using Javascript (frontend and backend)
- 💼 Working across diverse industries like construction, finance, marketing, and entrepreneurship
- 🎓 Holding a postgraduate degree in Master of Computer Applications from the University of Pune
🔧 My technical prowess lies in:
- ⚛️ Leading Javascript frameworks: React, Node.js, and Express.js
- 🚀 Delivering robust and scalable web applications with the best software engineering practices
🌱 Excitingly, I've started my DevOps learning journey with #90DaysOfDevOps to enhance my skills in this area.
🧠 Beyond technical skills, I offer:
- 💡 Exceptional problem-solving abilities and a strong aptitude for learning new technologies
- 🤝 A collaborative mindset, making me a valuable team player in diverse environments
🚀 My passion is in delivering high-quality software solutions that solve real-world problems. If you're seeking a skilled and experienced software engineer for top-notch web applications, let's connect and explore potential opportunities together! 🤝
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:
--nameto give a custom name to your container--rmto automatically remove the container when it exits--envor-eto set environment variables for your container--volumeor-vto mount volumes or bind mounts on your container--networkor--netto connect your container to a network--useror-uto run your container as a specific user--restartto 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 psto list all running containersdocker ps -ato 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:
-for--followto stream the logs continuously-tor--timestampsto add timestamps to each line--sinceto show logs since a specific time or duration--tailto 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-container
# Inspect multiple containers named foo and bar
docker inspect foo bar
# 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 imagesto 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:
--allor-ato show all containers (default shows just running)--no-streamto disable streaming stats and only pull the first result--formatto 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! 🙏




