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

#CommandDescription
1docker --versionDisplay Docker version
2docker infoDisplay system-wide information about Docker
3docker run [options] image [command] [args]Run a command in a new container
4docker psList running containers
5docker ps -aList all containers (running and stopped)
6docker imagesList locally available images
7docker pull imageDownload an image from a registry
8docker build [options] pathBuild an image from a Dockerfile in the specified path
9docker push imagePush an image to a registry
10docker rmi imageRemove an image
11docker rm containerRemove a container
12docker exec [options] container commandRun a command in a running container
13docker stop containerStop a running container
14docker start containerStart a stopped container
15docker restart containerRestart a running or stopped container
16docker logs containerView logs from a container
17docker network lsList Docker networks
18docker volume lsList Docker volumes
19docker-compose upStart services defined in a docker-compose.yml file
20docker-compose downStop and remove containers, networks, and volumes
21docker inspect [options] objectDisplay detailed information on one or more objects
22docker attach containerAttach local standard input, output, and error streams to a running container
23docker cp [options] source_path container:destination_pathCopy files/folders between a container and the local filesystem
24docker commit [options] container [repository[:tag]]Create a new image from a container’s changes
25docker buildx create --useCreate and use a new builder instance with BuildKit support

Dockerfile Directives

#DirectiveDescription
1FROM image [AS name]Set the base image for subsequent instructions
2COPY source_path destination_pathCopy files/folders from source to destination
3RUN commandExecute a command in a new layer
4CMD ["executable", "param1", ...]Set the default command and/or parameters
5EXPOSE portInform Docker that the app will listen on the specified network ports at runtime

Docker Compose

#CommandDescription
1docker-compose psList containers defined in docker-compose.yml
2docker-compose logs [service]View logs for a specific service in the docker-compose.yml
3docker-compose exec service commandRun a command in a service container
4docker-compose down --volumesStop and remove containers, networks, and volumes defined in docker-compose.yml
5docker-compose buildBuild services defined in docker-compose.yml
6docker-compose up -dStart services in the background
7docker-compose scale service=numScale a service to a specific number of containers
8docker-compose pausePause services in the docker-compose.yml
9docker-compose unpauseUnpause services in the docker-compose.yml

Docker Swarm

#CommandDescription
1docker swarm initInitialize a swarm
2docker node lsList nodes in the swarm
3docker service lsList services in the swarm
4docker service ps serviceList tasks of a service in the swarm
5docker service scale service=numScale a service to a specific number of replicas
6docker service update [options] serviceUpdate a service with new configuration
7docker service rm serviceRemove a service from the swarm
8docker swarm join-token [worker/manager]Display the join token for worker or manager nodes
9docker swarm leave --forceForce a node to leave the swarm

Tips and Tricks

#Tip/TrickDescription
1Container Shell Access: docker exec -it container shAccess the shell inside a running container
2Cleanup: docker system prune -aRemove all unused containers, networks, and images
3Inspect Container IP: docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' containerGet the IP address of a running container
4View Container Resources: docker stats containerDisplay a live stream of container resource usage
5Docker Contexts: docker context use [context]Switch between Docker contexts
6Interactive Build: docker build -t image:tag - < DockerfileBuild an image interactively from a Dockerfile
7Docker Prune Images: docker image prune -aRemove all dangling and unused images
8List 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.