Docker cheatsheet
This cheat sheet features the most important and commonly used Docker commands for easy reference.
Table of contents
- References
- Images
- Containers
- Run a container
- Run a container with environment variables
- Run a container with a volume mount
- List running containers
- List all containers
- Stop a container
- Stop all containers
- Start a container
- Restart a container
- Remove a container
- Remove all containers
- Stop and remove all containers and images
- Kill all running containers
- Connect to a running container
- View container logs
- Follow container logs
- Copy files from container to host
- Copy files from host to container
- Inspect
- Volumes
- Networks
- Cleanup
- Docker Compose
References
Images
Build an image
docker build -t <image_name>:<tag> .
Build with a specific Dockerfile
docker build -f <Dockerfile> -t <image_name>:<tag> .
List images
docker images
Remove an image
docker rmi <image_id>
Remove all images
docker rmi $(docker images -q)
Remove all untagged images
docker rmi $(docker images | grep '^<none>' | awk '{print $3}')
Pull an image
docker pull <image_name>:<tag>
Push an image
docker push <image_name>:<tag>
Tag an image
docker tag <image_id> <image_name>:<tag>
Containers
Run a container
docker run -d --name <container_name> -p <host_port>:<container_port> <image_name>:<tag>
Run a container with environment variables
docker run -d --name <container_name> -e KEY=VALUE <image_name>:<tag>
Run a container with a volume mount
docker run -d --name <container_name> -v <host_path>:<container_path> <image_name>:<tag>
List running containers
docker ps
List all containers
docker ps -a
Stop a container
docker stop <container_id>
Stop all containers
NOTE: This will stop ALL your containers.
docker stop $(docker ps -aq)
Start a container
docker start <container_id>
Restart a container
docker restart <container_id>
Remove a container
docker rm <container_id>
Remove all containers
NOTE: This will remove ALL your containers.
docker rm $(docker ps -aq)
Stop and remove all containers and images
NOTE: This will remove ALL your images and containers.
docker stop $(docker ps -aq) && docker rm $(docker ps -aq) && docker rmi $(docker images -q)
Kill all running containers
docker kill $(docker ps -q)
Connect to a running container
docker exec -it <container_id> /bin/bash
View container logs
docker logs <container_id>
Follow container logs
docker logs -f <container_id>
Copy files from container to host
docker cp <container_id>:<container_path> <host_path>
Copy files from host to container
docker cp <host_path> <container_id>:<container_path>
Inspect
Get IP address of a running container
docker inspect -f '' <container_id>
See all space Docker takes up
docker system df
Inspect a container
docker inspect <container_id>
Volumes
Create a volume
docker volume create <volume_name>
List volumes
docker volume ls
Remove a volume
docker volume rm <volume_name>
Remove all unused volumes
docker volume prune
Networks
Create a network
docker network create <network_name>
List networks
docker network ls
Connect a container to a network
docker network connect <network_name> <container_id>
Remove all unused networks
docker network prune
Cleanup
Remove all unused objects (containers, networks, images, cache)
docker system prune
Remove all unused objects including volumes
docker system prune --volumes
Remove all unused images (not just dangling)
docker image prune -a
Docker Compose
Start services
docker compose up -d
Stop services
docker compose down
Stop services and remove volumes
docker compose down -v
View logs
docker compose logs -f
Rebuild and start services
docker compose up -d --build
List running services
docker compose ps