Table of contents
This cheat sheet covers all the basic and advanced commands for Docker, Docker Compose, and Docker Swarm that are essential for DevOps engineers.
Docker Basics
Images
Images are the blueprints for Docker containers.
Build
Build an image from a Dockerfile:
docker build -t <image-name> .
List
List all the images present on the Docker host:
docker image ls
Remove
Remove an image:
docker image rm <image-id>
Prune
Remove all unused images:
docker image prune
Save
Save an image to a tar file:
docker image save -o <filename>.tar <image-name>
Load
Load an image from a tar file:
docker image load -i <filename>.tar
Tag
Tag an image:
docker image tag <image-id> <new-image-name>
Containers
Containers are instances of Docker images.
Run
Run a container from an image:
docker run <image-name>
Run with Port Forwarding
Run a container from an image with port forwarding:
docker run -p <host-port>:<container-port> <image-name>
Run with Volume Mounting
Run a container from an image with volume mounting:
docker run -v <host-path>:<container-path> <image-name>
List
List all the containers present on the Docker host:
docker ps -a
Start
Start a stopped container:
docker start <container-id>
Stop
Stop a running container:
docker stop <container-id>
Remove
Remove a container:
docker rm <container-id>
Prune
Remove all stopped containers:
docker container prune
Exec
Run a command inside a running container:
docker exec -it <container-id> <command>
Logs
View the logs of a container:
docker logs <container-id>
Stats
View the real-time resource usage of a container:
docker stats <container-id>
Volumes
Volumes are persistent data storage mechanisms that allow data to persist across container restarts and can be shared between multiple containers.
Create
Create a new volume:
docker volume create <volume-name>
List
List all the volumes present on the Docker host:
docker volume ls
Inspect
Inspect a specific volume to view the details, including the mountpoint and driver:
docker volume inspect <volume-name>
Mount
Mount a volume to a specific container:
docker run -v <volume-name>:<container-mountpoint> <image-name>
Remove
Remove a volume:
docker volume rm <volume-name>
Prune
Delete all the unused volumes present on the Docker host:
docker volume prune
Backup
Back up a volume:
docker run --rm -v <volume-name>:/volume -v $(pwd):/backup <image-name> tar -czvf /backup/<volume-name>-backup.tar.gz /volume
Restore
Restore a volume from a backup:
docker run --rm -v <volume-name>:/volume -v $(pwd):/backup busybox tar -xzvf /backup/<volume-name>-backup.tar.gz
Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications.
Basic Usage
Define
Define a multi-container Docker application in a `docker-compose.yml
file:
version: '3'
services:
web:
build: .
ports:
- "80:80"
db:
image: postgres
volumes:
- db-data:/var/lib/postgresql/data
volumes:
db-data:
Start
Start a multi-container Docker application defined in a docker-compose.yml
file:
docker-compose up
Stop
Stop a running multi-container Docker application:
docker-compose down
List
List all running multi-container Docker applications:
docker-compose ps
Advanced Usage
Build
Build the images for a multi-container Docker application defined in a docker-compose.yml
file:
docker-compose build
Scale
Scale a specific service in a multi-container Docker application:
docker-compose up --scale <service-name>=<number-of-instances>
Exec
Run a command inside a specific container in a multi-container Docker application:
docker-compose exec <service-name> <command>
Logs
View the logs of a specific service in a multi-container Docker application:
docker-compose logs <service-name>
Restart
Restart a specific service in a multi-container Docker application:
docker-compose restart <service-name>
Stop and Remove
Stop and remove all containers, networks, and volumes associated with a multi-container Docker application:
docker-compose down --volumes --remove-orphans
Docker Swarm
Docker Swarm is a native Docker tool for container orchestration.
Basic Usage
Initialize
Initialize a Docker Swarm:
docker swarm init
Join
Join a Docker Swarm as a worker:
docker swarm join --token <token> <manager-ip-address>
Deploy
Deploy a Docker stack:
docker stack deploy -c <compose-file> <stack-name>
List
List all the services running in a Docker Swarm:
docker service ls
Scale
Scale a specific service in a Docker Swarm:
docker service scale <service-name>=<number-of-instances>
Remove
Remove a Docker stack:
docker stack rm <stack-name>
Leave
Leave a Docker Swarm:
docker swarm leave --force
Advanced Usage
Update
Update a specific service in a Docker Swarm:
docker service update <service-name> --image <new-image-name>
Logs
View the logs of a specific service in a Docker Swarm:
docker service logs <service-name>
Drain
Drain a node in a Docker Swarm:
docker node update --availability drain <node-id>
Promote
Promote a worker node to a manager node in a Docker Swarm:
docker node promote <node-id>
Demote
Demote a manager node to a worker node in a Docker Swarm:
docker node demote <node-id>
Inspect
Inspect a specific node in a Docker Swarm:
docker node inspect <node-id>
Visualize
Visualize a Docker Swarm using the Docker Swarm Visualizer:
docker run -it -d -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock dockersamples/visualizer