The Complete Docker Cheat Sheet: A Handy Reference for Docker Images, Compose, Volumes, and More
Welcome to our comprehensive Docker cheat sheet! In this article, we have compiled a comprehensive list of essential Docker commands that every developer and DevOps enthusiast should know. Whether you are new to Docker or an experienced user, this cheat sheet will serve as your go-to reference for managing Docker images, working with Docker Compose, utilizing volumes, harnessing the power of Docker Swarm, managing Docker registries, building images, executing commands inside containers, performing clean-up operations, and configuring networking.
Docker Basics
docker pull <image> | download a Docker image from a repository |
docker images | list all Docker images on your system |
docker ps | list all running Docker containers |
docker run <image> | start a new Docker container from an image |
docker stop <container> | stop a running Docker container |
docker rm <container> | remove a stopped Docker container |
docker rmi <image> | remove a Docker image from your system |
Dockerfile
FROM
: specify the base image to use in a DockerfileRUN
: run a command during the build process of a Docker imageCMD
: specify the default command to run when a container is startedEXPOSE
: specify the port on which a container listensCOPY
: copy files or directories from the build context to the containerWORKDIR
: set the working directory for the containerENV
: set an environment variable for the container
Docker Images
docker images
: list all Docker images on your systemdocker image inspect <image>
: inspect the details of a Docker imagedocker image history <image>
: show the history of a Docker imagedocker image rm <image>
: remove a Docker image from your systemdocker image tag <source-image> <new-image-name>
: tag a Docker image with a new name and/or tag
Docker Compose
docker-compose up
: start the services defined in adocker-compose.yml
filedocker-compose down
: stop the services defined in adocker-compose.yml
filedocker-compose build
: build the Docker images defined in adocker-compose.yml
filedocker-compose ps
: list the running containers for services defined in adocker-compose.yml
file
Docker Volumes
docker volume create <name>
: create a new Docker volumedocker volume ls
: list all Docker volumes on your systemdocker volume inspect <name>
: inspect the details of a Docker volumedocker run -v <name>:<path>
: attach a Docker volume to a containerdocker run -v <host-path>:<container-path>
: mount a host directory as a Docker volume
Docker Swarm
docker swarm init
: initialize a new Docker Swarm clusterdocker swarm join
: join a Docker Swarm clusterdocker node ls
: list all nodes in a Docker Swarm clusterdocker service create
: create a new Docker servicedocker service ls
: list all Docker services in a Swarm clusterdocker service ps <service>
: list the tasks for a Docker servicedocker service scale <service>=<num-replicas>
: scale a Docker service to a specified number of replicas
Docker Registry
docker login
: login to a Docker registrydocker logout
: log out from a Docker registrydocker push <image>
: push a Docker image to a registrydocker pull <image>
: pull a Docker image from a registrydocker tag <source-image> <registry>/<repo>:<tag>
: tag a Docker image for a registry
Docker Logs
docker logs <container>
: display the logs of a running containerdocker logs -f <container>
: follow the logs of a running container in real-timedocker logs --tail <number> <container>
: display the last n lines of a container's logs
Docker Exec
docker exec -it <container> <command>
: run a command inside a running containerdocker exec -it <container> sh
: open a shell session inside a running container
Docker Build
docker build -t <image-name> .
: build a Docker image from a Dockerfile in the current directory and tag it with the given name
Docker Clean-up
docker system prune
: remove all stopped containers, unused networks, dangling images and cachesdocker container prune
: remove all stopped containersdocker image prune
: remove all unused imagesdocker volume prune
: remove all unused volumesdocker network prune
: remove all unused networks
Networking
docker network create <name>
: create a new Docker networkdocker network ls
: list all Docker networks on your systemdocker network inspect <name>
: inspect the details of a Docker networkdocker run --network <name>
: attach a container to a Docker networkdocker run -p <host-port>:<container-port>
: publish a container's port to the hostdocker run --expose <port>
: expose a port from the container to other linked containers
With step-by-step explanations and practical examples, this cheat sheet will help you navigate the vast Docker ecosystem with ease. Whether you're a developer, system administrator, or anyone working with Docker, this cheat sheet is a valuable resource that will boost your productivity and help you master the essential Docker commands.
So, dive in and explore the world of Docker with our comprehensive cheat sheet. From beginner to pro, this guide will equip you with the knowledge and commands you need to effectively work with Docker and streamline your containerization workflow.
Thank you for reading this blog. If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future.
— Happy Learning !!!