Docker Cheat Sheet On this page 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 --versionDisplay Docker version 2 docker infoDisplay system-wide information about Docker 3 docker run [options] image [command] [args]Run a command in a new container 4 docker psList running containers 5 docker ps -aList all containers (running and stopped) 6 docker imagesList locally available images 7 docker pull imageDownload an image from a registry 8 docker build [options] pathBuild an image from a Dockerfile in the specified path 9 docker push imagePush an image to a registry 10 docker rmi imageRemove an image 11 docker rm containerRemove a container 12 docker exec [options] container commandRun a command in a running container 13 docker stop containerStop a running container 14 docker start containerStart a stopped container 15 docker restart containerRestart a running or stopped container 16 docker logs containerView logs from a container 17 docker network lsList Docker networks 18 docker volume lsList Docker volumes 19 docker-compose upStart services defined in a docker-compose.yml file 20 docker-compose downStop and remove containers, networks, and volumes 21 docker inspect [options] objectDisplay detailed information on one or more objects 22 docker attach containerAttach local standard input, output, and error streams to a running container 23 docker cp [options] source_path container:destination_pathCopy 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 --useCreate 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_pathCopy files/folders from source to destination 3 RUN commandExecute a command in a new layer 4 CMD ["executable", "param1", ...]Set the default command and/or parameters 5 EXPOSE portInform Docker that the app will listen on the specified network ports at runtime
Docker Compose# # Command Description 1 docker-compose psList 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 commandRun a command in a service container 4 docker-compose down --volumesStop and remove containers, networks, and volumes defined in docker-compose.yml 5 docker-compose buildBuild services defined in docker-compose.yml 6 docker-compose up -dStart services in the background 7 docker-compose scale service=numScale a service to a specific number of containers 8 docker-compose pausePause services in the docker-compose.yml 9 docker-compose unpauseUnpause services in the docker-compose.yml
Docker Swarm# # Command Description 1 docker swarm initInitialize a swarm 2 docker node lsList nodes in the swarm 3 docker service lsList services in the swarm 4 docker service ps serviceList tasks of a service in the swarm 5 docker service scale service=numScale a service to a specific number of replicas 6 docker service update [options] serviceUpdate a service with new configuration 7 docker service rm serviceRemove 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 --forceForce a node to leave the swarm
Tips and Tricks# # Tip/Trick Description 1 Container Shell Access : docker exec -it container shAccess the shell inside a running container 2 Cleanup : docker system prune -aRemove all unused containers, networks, and images 3 Inspect Container IP : docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' containerGet the IP address of a running container 4 View Container Resources : docker stats containerDisplay 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 - < DockerfileBuild an image interactively from a Dockerfile 7 Docker Prune Images : docker image prune -aRemove 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.