Docker Command Cheat Sheet
Nine groups from images to Compose, each with a ready-to-use example
92 commands
docker imagesImagesList local images with their sizes.
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"docker pull nginx:alpineImagesPull an image; omitting the tag means latest.
docker pull nginx:1.27-alpine
docker build -t app:1.0 .ImagesBuild an image from the Dockerfile in the current directory.
docker build -t registry.example.com/app:1.0 .
docker build --no-cache -t app:1.0 .ImagesRebuild without cache, useful when a stale layer causes odd behavior.
docker build --no-cache -t app:1.0 .
docker build -f Dockerfile.prod .ImagesUse a Dockerfile with a non-default name.
docker build -f docker/Dockerfile.prod -t app:prod .
docker build --target builder -t app:debug .ImagesBuild only up to a stage; handy for multi-stage debugging.
docker build --target builder -t app:debug .
docker buildx build --platform linux/amd64,linux/arm64 -t app:1.0 .ImagesBuild a multi-architecture image in one go.
docker buildx build --platform linux/amd64,linux/arm64 --push -t app:1.0 .
docker tag app:1.0 registry.example.com/app:1.0ImagesTag an image with the registry path before pushing.
docker tag app:1.0 registry.example.com/team/app:1.0
docker rmi <image>ImagesRemove an image; delete dependent containers first.
docker rmi app:1.0
docker history <image>ImagesShow image layers and their sizes.
docker history --no-trunc app:1.0
docker save -o app.tar app:1.0ImagesExport an image to a tar file for offline transfer.
docker save -o app.tar app:1.0
docker load -i app.tarImagesLoad an image from a tar file.
docker load -i app.tar
docker inspect <image>ImagesInspect image metadata: env, entrypoint, exposed ports.
docker inspect --format "{{.Config.Env}}" nginxdocker run -d --name web -p 8080:80 nginxContainer lifecycleRun in the background with a port mapping.
docker run -d --name web -p 8080:80 nginx:alpine
docker run -it --rm alpine shContainer lifecycleRun a throwaway interactive container that removes itself on exit.
docker run -it --rm alpine sh
docker run -v pgdata:/var/lib/postgresql/data postgresContainer lifecycleMount a named volume so data survives container removal.
docker run -d --name pg -v pgdata:/var/lib/postgresql/data postgres:16
docker run -e TZ=Asia/Shanghai -e ENV=prod app:1.0Container lifecyclePass environment variables; wrong timezone usually starts here.
docker run -d -e TZ=Asia/Shanghai -e SPRING_PROFILES_ACTIVE=prod app:1.0
docker run --network host nginxContainer lifecycleShare the host network namespace instead of mapping ports.
docker run -d --network host nginx
docker run --restart unless-stopped -d app:1.0Container lifecycleSet a restart policy so the container comes back after a reboot.
docker run -d --restart unless-stopped app:1.0
docker run --memory 512m --cpus 1.5 app:1.0Container lifecycleCap memory and CPU so one container cannot starve the host.
docker run -d --memory 512m --cpus 1.5 app:1.0
docker psContainer lifecycleList running containers only.
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"docker ps -aContainer lifecycleList all containers including stopped ones.
docker ps -a --filter "status=exited"
docker start <ct>Container lifecycleStart an existing container with its original config.
docker start web
docker stop <ct>Container lifecycleStop gracefully: SIGTERM first, SIGKILL after the timeout.
docker stop -t 30 web
docker restart <ct>Container lifecycleRestart a container, often needed after config changes.
docker restart web
docker rm <ct>Container lifecycleRemove a stopped container.
docker rm web-old
docker rm -f <ct>Container lifecycleForce-remove a running container.
docker rm -f web
docker rename <old> <new>Container lifecycleRename a container.
docker rename web web-01
docker update --memory 1g <ct>Container lifecycleAdjust resource limits without recreating the container.
docker update --memory 1g --memory-swap 1g web
docker attach <ct>Container lifecycleAttach to PID 1; Ctrl+C also kills the container, prefer exec.
docker attach web
docker exec -it web shExec and file transferGet a shell inside a running container; use sh when bash is absent.
docker exec -it web /bin/sh
docker exec -u root -it web shExec and file transferEnter as root when the default user lacks permissions.
docker exec -u 0 -it web sh
docker exec web ls -l /etc/nginxExec and file transferRun a single command inside the container.
docker exec web nginx -t
docker exec web cat /etc/resolv.confExec and file transferContainer DNS config; check it first when resolution fails.
docker exec web cat /etc/resolv.conf
docker cp ./app.conf web:/etc/nginx/conf.d/Exec and file transferCopy a file from the host into a container.
docker cp ./app.conf web:/etc/nginx/conf.d/app.conf
docker cp web:/var/log/app.log ./Exec and file transferCopy a file out of a container.
docker cp web:/var/log/app.log ./app.log
docker diff <ct>Exec and file transferList files changed inside the container versus the image.
docker diff web
docker export <ct> -o rootfs.tarExec and file transferExport the container filesystem without image metadata.
docker export web -o web-rootfs.tar
docker network lsNetworkingList all Docker networks.
docker network ls
docker network create app-netNetworkingCreate a user-defined bridge network.
docker network create --driver bridge app-net
docker network inspect app-netNetworkingShow container IPs in a network; first stop for connectivity issues.
docker network inspect --format "{{range .Containers}}{{.Name}} {{.IPv4Address}}{{end}}" app-netdocker network connect app-net <ct>NetworkingAttach a running container to a network.
docker network connect app-net web
docker network disconnect app-net <ct>NetworkingDetach a container from a network.
docker network disconnect app-net web
docker network rm app-netNetworkingRemove a network; detach containers first.
docker network rm app-net
docker port <ct>NetworkingShow the port mappings of a container.
docker port web
docker run --network app-net --name api app:1.0NetworkingOn a user-defined network, containers resolve each other by name.
docker run -d --network app-net --name api app:1.0
docker volume lsVolumesList all volumes.
docker volume ls -f dangling=true
docker volume create pgdataVolumesCreate a named volume for easier backup and migration.
docker volume create pgdata
docker volume inspect pgdataVolumesShow where the volume lives on the host.
docker volume inspect --format "{{.Mountpoint}}" pgdatadocker run -v $(pwd)/data:/data app:1.0VolumesBind-mount a host directory so edits apply immediately.
docker run -d -v $(pwd)/data:/app/data app:1.0
docker run -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro nginxVolumesMount config read-only so the container cannot modify the host file.
docker run -d -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro nginx
docker volume rm pgdataVolumesRemove a volume; delete using containers first.
docker volume rm pgdata
docker volume pruneVolumesRemove dangling volumes not used by any container.
docker volume prune -f
docker inspect -f "{{.Mounts}}" <ct>VolumesShow what a container actually mounts, useful when configs seem ignored.
docker inspect -f "{{json .Mounts}}" webdocker compose up -dComposeStart all services in the background.
docker compose up -d
docker compose up -d --buildComposeRebuild images before starting; needed after Dockerfile changes.
docker compose up -d --build
docker compose downComposeStop and remove containers and networks, keeping volumes.
docker compose down
docker compose down -vComposeAlso remove volumes. Destructive.
docker compose down -v --remove-orphans
docker compose psComposeShow container status for this project.
docker compose ps -a
docker compose logs -f apiComposeFollow logs of one service.
docker compose logs -f --tail 100 api
docker compose exec api shComposeOpen a shell in a service container.
docker compose exec api /bin/sh
docker compose restart apiComposeRestart one service without touching the others.
docker compose restart api
docker compose pullComposePull the latest images referenced in the compose file.
docker compose pull
docker compose configComposeRender the resolved compose config to validate syntax and variables.
docker compose config
docker compose up -d --scale web=3ComposeScale to three replicas; fixed host ports no longer allowed.
docker compose up -d --scale web=3
docker compose --profile debug upComposeBring up services under a specific profile.
docker compose --profile debug up -d
docker login registry.example.comRegistriesLog in to a private registry; credentials go to ~/.docker/config.json.
docker login registry.example.com -u ci
docker logout registry.example.comRegistriesLog out and clear stored credentials.
docker logout registry.example.com
docker push registry.example.com/app:1.0RegistriesPush an image; tag it with the registry path first.
docker push registry.example.com/team/app:1.0
docker search nginxRegistriesSearch Docker Hub for images.
docker search --limit 10 nginx
docker manifest inspect nginx:latestRegistriesShow which architectures an image supports.
docker manifest inspect nginx:latest
docker buildx imagetools inspect app:1.0RegistriesInspect the manifest of a remote multi-arch image.
docker buildx imagetools inspect registry.example.com/app:1.0
docker system dfCleanupShow disk usage of images, containers, volumes, and build cache.
docker system df -v
docker system pruneCleanupRemove stopped containers, unused networks, and dangling images.
docker system prune -f
docker system prune -a --volumesCleanupAlso remove unused images and volumes. Destructive.
docker system prune -a --volumes -f
docker builder pruneCleanupClear build cache only, for when it eats your disk.
docker builder prune -a -f
docker container pruneCleanupRemove all stopped containers.
docker container prune -f --filter "until=24h"
docker image pruneCleanupRemove dangling images; -a also removes unused ones.
docker image prune -a -f
docker network pruneCleanupRemove networks with no containers attached.
docker network prune -f
docker rm -v $(docker ps -aq)CleanupRemove every container, including anonymous volumes.
docker rm -f -v $(docker ps -aq)
docker logs -f --tail 100 webDebug and resourcesFollow the last 100 log lines; the first stop for debugging.
docker logs -f --tail 100 web
docker logs --since 10m webDebug and resourcesShow logs from the last ten minutes.
docker logs --since 10m --timestamps web
docker inspect -f "{{.State.Status}}" webDebug and resourcesRead container state in a script-friendly form.
docker inspect -f "{{.State.Status}} exit={{.State.ExitCode}}" webdocker inspect -f "{{.State.ExitCode}}" <ct>Debug and resourcesCheck the exit code; 137 usually means OOM-killed.
docker inspect -f "{{.State.ExitCode}}" webdocker statsDebug and resourcesLive CPU, memory, and network usage per container.
docker stats --no-stream
docker top <ct>Debug and resourcesShow processes inside a container.
docker top web
docker eventsDebug and resourcesStream Docker events to see when containers die or restart.
docker events --filter "container=web"
docker run -it --rm --entrypoint sh app:1.0Debug and resourcesOverride the entrypoint to inspect an image that will not start.
docker run -it --rm --entrypoint sh app:1.0
docker run -it --rm --network container:web nicolaka/netshootDebug and resourcesBorrow a network-toolbox image sharing the target container network.
docker run -it --rm --network container:web nicolaka/netshoot
docker build --progress=plain -t app:1.0 .Debug and resourcesPrint full build output to locate the failing step.
docker build --progress=plain --no-cache -t app:1.0 .
docker inspect -f "{{json .HostConfig.RestartPolicy}}" <ct>Debug and resourcesVerify the restart policy is actually applied.
docker inspect -f "{{json .HostConfig.RestartPolicy}}" webdocker diff <ct> | head -50Debug and resourcesQuickly see which files changed inside a container.
docker diff web | head -50
Something broken or missing?
Send feedback