Docker Commands Cheatsheet
Every essential Docker command in one place — container lifecycle, images, Compose, volumes, networks, registries, and cleanup. Bookmark this page and never search for a command again.
Container Lifecycle
docker run <image>Create and start a new container from an imagedocker run -d <image>Run container in detached (background) modedocker run -it <image> bashRun container interactively with a shell (TTY attached)docker run --name my-app <image>Run container with a custom namedocker run -p 8080:80 <image>Map host port 8080 to container port 80docker run -v /host:/container <image>Bind-mount a host directory into the containerdocker run --rm <image>Automatically remove container when it exitsdocker run -e KEY=value <image>Set an environment variable inside the containerdocker run --env-file .env <image>Load environment variables from a filedocker run --network my-net <image>Connect the container to a specific networkdocker start <container>Start a stopped containerdocker stop <container>Gracefully stop a running container (SIGTERM, then SIGKILL)docker restart <container>Stop and then start a containerdocker rm <container>Remove a stopped containerdocker rm -f <container>Force remove a running containerdocker kill <container>Send SIGKILL to immediately terminate a containerdocker pause <container>Freeze all processes in a container (SIGSTOP)docker unpause <container>Resume a paused containerdocker rename <old> <new>Rename an existing containerdocker wait <container>Block until a container stops, then print exit codedocker update --memory 512m <c>Update resource limits on a running containerdocker create <image>Create a container without starting itContainer Inspection
docker psList running containersdocker ps -aList all containers including stopped onesdocker ps -qList only container IDs (useful for scripting)docker ps --format '{{.Names}}'Custom output format — show only container namesdocker logs <container>Show stdout/stderr logs from a containerdocker logs -f <container>Follow logs in real time (like tail -f)docker logs --tail 100 <container>Show only the last 100 log linesdocker logs --since 1h <container>Show logs from the last hourdocker top <container>Display running processes inside a containerdocker statsLive stream of CPU, memory, I/O for all running containersdocker stats --no-streamShow resource usage snapshot (no live updates)docker inspect <container>Return detailed JSON metadata about a containerdocker inspect -f '{{.State.Status}}' <c>Extract a specific field using Go templatesdocker port <container>Show port mappings for a containerdocker diff <container>Show filesystem changes since the container starteddocker exec -it <container> bashOpen an interactive shell inside a running containerdocker exec <container> <cmd>Run a one-off command inside a running containerdocker cp <c>:/path /host/pathCopy files from a container to the hostdocker cp /host/path <c>:/pathCopy files from the host into a containerdocker attach <container>Attach stdin/stdout/stderr to a running containerImages
docker build -t myapp:v1 .Build an image from a Dockerfile in current directorydocker build -f Dockerfile.prod .Build using a specific Dockerfiledocker build --no-cache -t myapp .Build without using layer cachedocker build --target stage .Build up to a specific multi-stage targetdocker build --build-arg KEY=val .Pass build-time variables to Dockerfile ARGdocker pull <image>:<tag>Download an image from a registrydocker pull --all-tags <image>Pull all tagged versions of an imagedocker push <image>:<tag>Upload an image to a registrydocker imagesList all locally stored imagesdocker images -aList all images including intermediate layersdocker rmi <image>Remove a local imagedocker rmi $(docker images -q)Remove all local imagesdocker tag <image> <new:tag>Create a new tag (alias) for an imagedocker history <image>Show the layer history and size of each layerdocker save -o backup.tar <image>Export an image to a tar archivedocker load -i backup.tarImport an image from a tar archivedocker import archive.tar myimageCreate an image from a filesystem archivedocker image inspect <image>Show detailed metadata for an imagedocker buildx build --platform linux/amd64,linux/arm64 .Multi-platform build with BuildxDocker Compose
docker compose upCreate and start all services defined in compose filedocker compose up -dStart services in detached (background) modedocker compose up --buildRebuild images before starting servicesdocker compose up <service>Start only a specific service and its dependenciesdocker compose downStop and remove containers, networks created by updocker compose down -vStop services and also remove named volumesdocker compose down --rmi allStop services and remove all related imagesdocker compose buildBuild or rebuild all service imagesdocker compose build --no-cacheBuild without using the Docker layer cachedocker compose logsShow combined logs from all servicesdocker compose logs -f <service>Follow logs from a specific service in real timedocker compose psList containers managed by the current compose projectdocker compose exec <svc> bashOpen a shell inside a running service containerdocker compose run <svc> <cmd>Run a one-off command in a new service containerdocker compose pullPull latest images for all servicesdocker compose restart <service>Restart a specific servicedocker compose configValidate and display the resolved compose filedocker compose stopStop services without removing containersdocker compose -f prod.yml up -dUse a custom compose filedocker compose --profile debug upStart services matching a specific profileVolumes & Networks
docker volume create my-volCreate a named volume for persistent datadocker volume lsList all volumesdocker volume inspect my-volShow detailed volume metadata (mount point, driver)docker volume rm my-volRemove a specific volumedocker volume pruneRemove all unused volumesdocker run -v my-vol:/data <image>Mount a named volume into a containerdocker run --mount type=bind,src=.,dst=/app <img>Bind mount with explicit syntaxdocker run --tmpfs /tmp <image>Mount a temporary filesystem in memorydocker network create my-netCreate a user-defined bridge networkdocker network create --driver overlay netCreate an overlay network for Swarm servicesdocker network lsList all networksdocker network inspect my-netShow network config, connected containers, IP rangesdocker network rm my-netRemove a networkdocker network connect my-net <c>Attach a running container to a networkdocker network disconnect my-net <c>Detach a container from a networkdocker network pruneRemove all unused networksRegistry & Repository
docker loginAuthenticate with Docker Hub (prompts for credentials)docker login registry.example.comLog in to a private registrydocker login -u user -p tokenLog in with username and password/token inlinedocker logoutRemove stored credentials for Docker Hubdocker logout registry.example.comLog out from a specific registrydocker search nginxSearch Docker Hub for images matching a termdocker search --filter is-official=true nginxSearch for only official imagesdocker pull nginx:alpinePull a specific tagged image from the registrydocker tag myapp registry.io/myapp:v1Tag a local image for a private registrydocker push registry.io/myapp:v1Push an image to a private registrydocker manifest inspect <image>View the manifest (platforms, digests) for an imageSystem & Cleanup
docker system pruneRemove all stopped containers, unused networks, dangling imagesdocker system prune -aRemove everything including unused images (reclaim disk)docker system prune -a --volumesFull cleanup including unused volumesdocker system dfShow Docker disk usage by images, containers, volumesdocker system df -vVerbose disk usage with per-item breakdowndocker system infoDisplay system-wide Docker configuration and statsdocker versionShow Docker client and server version infodocker image pruneRemove dangling (untagged) imagesdocker image prune -aRemove all images not used by any containerdocker container pruneRemove all stopped containersdocker volume pruneRemove all volumes not used by any containerdocker network pruneRemove all networks not used by any containerdocker builder pruneRemove build cache to free disk spacedocker context lsList available Docker contexts (local, remote, cloud)docker context use <name>Switch to a different Docker contextFAQ
What is the difference between docker run and docker exec?
docker run creates and starts a new container from an image. docker exec runs a command inside an already running container. Use run to launch new workloads and exec to debug or interact with existing ones — for example, docker exec -it my-container bash to open a shell.
How do I persist data when a container is removed?
Use Docker volumes or bind mounts. Named volumes (docker volume create my-data, then -v my-data:/app/data) are managed by Docker and persist independently of containers. Bind mounts (-v /host/path:/container/path) map a specific host directory. Both survive container removal.
What is the difference between COPY and ADD in a Dockerfile?
Both copy files into the image, but ADD has extra features: it can auto-extract tar archives and fetch URLs. COPY is simpler and more transparent. Best practice is to use COPY unless you specifically need tar extraction. For downloading files, use RUN curl or RUN wget instead of ADD.
How do I reduce Docker image size?
Use multi-stage builds to discard build dependencies from the final image. Start from slim or alpine base images. Combine RUN commands to reduce layers, add a .dockerignore file to exclude unnecessary files, and clean up package manager caches (apt-get clean, rm -rf /var/lib/apt/lists/*) within the same RUN layer.