Docker Cheat Sheet
This cheatsheet provides a quick reference for common Docker commands and concepts to help you streamline your containerized workflows.
Introduction
Docker is a powerful platform for automating the deployment, scaling, and management of applications in lightweight, portable containers. This cheatsheet provides a quick reference for common Docker commands and concepts to help you streamline your containerized workflows.
Commands
# | Command | Description |
---|
1 | docker --version | Display Docker version |
2 | docker info | Display system-wide information about Docker |
3 | docker run [options] image [command] [args] | Run a command in a new container |
4 | docker ps | List running containers |
5 | docker ps -a | List all containers (running and stopped) |
6 | docker images | List locally available images |
7 | docker pull image | Download an image from a registry |
8 | docker build [options] path | Build an image from a Dockerfile in the specified path |
9 | docker push image | Push an image to a registry |
10 | docker rmi image | Remove an image |
11 | docker rm container | Remove a container |
12 | docker exec [options] container command | Run a command in a running container |
13 | docker stop container | Stop a running container |
14 | docker start container | Start a stopped container |
15 | docker restart container | Restart a running or stopped container |
16 | docker logs container | View logs from a container |
17 | docker network ls | List Docker networks |
18 | docker volume ls | List Docker volumes |
19 | docker-compose up | Start services defined in a docker-compose.yml file |
20 | docker-compose down | Stop and remove containers, networks, and volumes |
21 | docker inspect [options] object | Display detailed information on one or more objects |
22 | docker attach container | Attach local standard input, output, and error streams to a running container |
23 | docker cp [options] source_path container:destination_path | Copy files/folders between a container and the local filesystem |
24 | docker commit [options] container [repository[:tag]] | Create a new image from a container’s changes |
25 | docker buildx create --use | Create and use a new builder instance with BuildKit support |
Dockerfile Directives
# | Directive | Description |
---|
1 | FROM image [AS name] | Set the base image for subsequent instructions |
2 | COPY source_path destination_path | Copy files/folders from source to destination |
3 | RUN command | Execute a command in a new layer |
4 | CMD ["executable", "param1", ...] | Set the default command and/or parameters |
5 | EXPOSE port | Inform Docker that the app will listen on the specified network ports at runtime |
Docker Compose
# | Command | Description |
---|
1 | docker-compose ps | List containers defined in docker-compose.yml |
2 | docker-compose logs [service] | View logs for a specific service in the docker-compose.yml |
3 | docker-compose exec service command | Run a command in a service container |
4 | docker-compose down --volumes | Stop and remove containers, networks, and volumes defined in docker-compose.yml |
5 | docker-compose build | Build services defined in docker-compose.yml |
6 | docker-compose up -d | Start services in the background |
7 | docker-compose scale service=num | Scale a service to a specific number of containers |
8 | docker-compose pause | Pause services in the docker-compose.yml |
9 | docker-compose unpause | Unpause services in the docker-compose.yml |
Docker Swarm
# | Command | Description |
---|
1 | docker swarm init | Initialize a swarm |
2 | docker node ls | List nodes in the swarm |
3 | docker service ls | List services in the swarm |
4 | docker service ps service | List tasks of a service in the swarm |
5 | docker service scale service=num | Scale a service to a specific number of replicas |
6 | docker service update [options] service | Update a service with new configuration |
7 | docker service rm service | Remove a service from the swarm |
8 | docker swarm join-token [worker/manager] | Display the join token for worker or manager nodes |
9 | docker swarm leave --force | Force a node to leave the swarm |
Tips and Tricks
# | Tip/Trick | Description |
---|
1 | Container Shell Access: docker exec -it container sh | Access the shell inside a running container |
2 | Cleanup: docker system prune -a | Remove all unused containers, networks, and images |
3 | Inspect Container IP: docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container | Get the IP address of a running container |
4 | View Container Resources: docker stats container | Display a live stream of container resource usage |
5 | Docker Contexts: docker context use [context] | Switch between Docker contexts |
6 | Interactive Build: docker build -t image:tag - < Dockerfile | Build an image interactively from a Dockerfile |
7 | Docker Prune Images: docker image prune -a | Remove all dangling and unused images |
8 | List Container IPs: docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq) | List container names and their IPs |
This cheatsheet covers a wide range of Docker commands, Dockerfile directives, Docker Compose, Docker Swarm, and includes some useful tips and tricks. Whether you’re a beginner or an experienced Docker user, this cheatsheet aims to make your containerization workflows more efficient and productive.