Docker Cheatsheet
Every Docker command for building, running, and debugging containers: from image management to Compose, with syntax and real use cases.100 commands · 6 sections
Docker packages applications into portable containers. This cheatsheet covers the full workflow: building and managing images, running and stopping containers, attaching shells and logs for debugging, volumes and networks, Docker Compose for multi-container apps, and system cleanup.
Every command shows its real syntax followed by the use case: when and why you reach for it.
Image Management18
docker imagesdocker image ls --filter "dangling=true"docker pull <image>docker build -t <name>:<tag> .docker build --no-cache -t <name> .docker build --target <stage> -t <name> .docker build --platform linux/amd64 -t <name> .docker tag <image> <name>:<tag>docker push <name>:<tag>docker rmi <image>docker image prunedocker image prune -adocker history <image>docker inspect <image>docker save -o myapp.tar <image>docker load -i myapp.tardocker search <term>docker manifest inspect <image>Container Lifecycle29
docker run <image>docker run -it <image> /bin/bashdocker run -d --name <name> <image>docker run -p 8080:80 <image>docker run -p 127.0.0.1:5432:5432 <image>docker run --env VAR=value <image>docker run --env-file .env <image>docker run --rm <image>docker run --restart unless-stopped <image>docker run --memory 512m --cpus 1 <image>docker run --network host <image>docker run --network <network> <image>docker run --init <image>docker psdocker ps -adocker ps --filter "status=exited"docker start <name>docker stop <name>docker stop $(docker ps -q)docker restart <name>docker kill <name>docker rm <name>docker rm -f $(docker ps -aq)docker container prunedocker top <name>docker statsdocker cp <name>:/path /host/pathdocker cp /host/file <name>:/pathdocker wait <name>Debugging & Shell12
docker logs <name>docker logs -f <name>docker logs --tail 100 <name>docker logs --since 5m <name>docker exec -it <name> /bin/shdocker exec <name> <cmd>docker exec -it <name> /bin/bash -lc "command"docker attach <name>docker port <name>docker diff <name>docker eventsdocker system events --filter "container=web"Volumes & Networks15
docker volume create <name>docker run -v <name>:/data <image>docker run -v $(pwd):/app <image>docker run -v /host/path:/container/path:ro <image>docker run --tmpfs /tmp <image>docker volume lsdocker volume inspect <name>docker volume rm <name>docker volume prunedocker network create <name>docker network lsdocker network connect <net> <container>docker network disconnect <net> <container>docker network inspect <net>docker network pruneDocker Compose16
docker compose updocker compose up -ddocker compose up --builddocker compose -f docker-compose.prod.yml up -ddocker compose downdocker compose down -vdocker compose psdocker compose logs -fdocker compose exec <service> <cmd>docker compose exec <service> /bin/shdocker compose restart <service>docker compose pulldocker compose builddocker compose configdocker compose topdocker compose scale worker=3System & Cleanup10
docker system dfdocker system prunedocker system prune -a --volumesdocker builder prunedocker infodocker versiondocker logindocker logoutdocker context lsdocker context use <name>Docker Cheatsheet
Every Docker command for building, running, and debugging containers: from image management to Compose, with syntax and real use cases.
Docker packages applications into portable containers. This cheatsheet covers the full workflow: building and managing images, running and stopping containers, attaching shells and logs for debugging, volumes and networks, Docker Compose for multi-container apps, and system cleanup.
Every command shows its real syntax followed by the use case: when and why you reach for it.
Image Management
docker images: List all local images with tags, sizes, and creation dates.docker image ls --filter "dangling=true": Find untagged (dangling) images: leftover build layers to clean.docker pull <image>: Download an image from a registry.docker build -t <name>:<tag> .: Build an image from a Dockerfile in the current directory and tag it.docker build --no-cache -t <name> .: Rebuild ignoring the layer cache: force fresh RUN steps, e.g. after apt changes.docker build --target <stage> -t <name> .: Build up to a specific multi-stage build target: dev stage for development images.docker build --platform linux/amd64 -t <name> .: Build for a different platform: cross-compile images for ARM or x86 from any host.docker tag <image> <name>:<tag>: Add a new tag to an existing image: tag your build before pushing.docker push <name>:<tag>: Upload an image to a registry (Docker Hub, GHCR, ECR...).docker rmi <image>: Remove an image locally (stop its containers first).docker image prune: Remove dangling images to reclaim disk.docker image prune -a: Remove ALL unused images, not just dangling ones.docker history <image>: Show the layer build history of an image: inspect what it contains.docker inspect <image>: Show detailed image metadata as JSON: config, env, ports, labels.docker save -o myapp.tar <image>: Export an image to a tar file: move images between machines without a registry.docker load -i myapp.tar: Import an image from a tar file created with docker save.docker search <term>: Search Docker Hub for images.docker manifest inspect <image>: Show which platforms an image supports: check ARM/amd64 availability.Container Lifecycle
docker run <image>: Create and start a container from an image.docker run -it <image> /bin/bash: Run an interactive shell inside a container: explore the image or test commands.docker run -d --name <name> <image>: Run a container in the background with a stable name.docker run -p 8080:80 <image>: Publish container port 80 to host port 8080: expose your app.docker run -p 127.0.0.1:5432:5432 <image>: Bind a port to localhost only: keep databases private, never exposed publicly.docker run --env VAR=value <image>: Pass environment variables to the container.docker run --env-file .env <image>: Load environment variables from a file: keep secrets out of your shell history.docker run --rm <image>: Automatically remove the container when it exits: perfect for one-off jobs.docker run --restart unless-stopped <image>: Auto-restart the container unless you explicitly stop it: for services.docker run --memory 512m --cpus 1 <image>: Limit container memory and CPU: protect your host from runaway processes.docker run --network host <image>: Share the host network: no port mapping needed, container uses host ports directly.docker run --network <network> <image>: Attach the container to a custom network: container-to-container DNS by name.docker run --init <image>: Use tini as PID 1 so Ctrl+C and signals work properly: fixes zombie processes.docker ps: List running containers.docker ps -a: List ALL containers including stopped ones: find that container you lost.docker ps --filter "status=exited": List only exited containers: cleanup candidates.docker start <name>: Restart an existing stopped container: keeps its state and filesystem.docker stop <name>: Gracefully stop a container (SIGTERM, then SIGKILL after timeout).docker stop $(docker ps -q): Stop every running container at once.docker restart <name>: Stop and start a container: apply new restart policy or clear a stuck state.docker kill <name>: Immediately kill a container without grace period: for unresponsive ones.docker rm <name>: Delete a stopped container.docker rm -f $(docker ps -aq): Force-remove every container: nuclear reset of local containers.docker container prune: Remove all stopped containers.docker top <name>: Show the processes running inside a container: like ps for containers.docker stats: Live CPU/memory/network usage of running containers: find the hungry one.docker cp <name>:/path /host/path: Copy files out of a container: pull logs or artifacts.docker cp /host/file <name>:/path: Copy files into a running container: quick config drops.docker wait <name>: Block until a container exits and print its exit code: scripting friendly.Debugging & Shell
docker logs <name>: Show container logs: the first stop when debugging.docker logs -f <name>: Follow logs live, like tail -f.docker logs --tail 100 <name>: Show only the last 100 lines: skip the startup noise.docker logs --since 5m <name>: Show logs from the last 5 minutes: after an incident.docker exec -it <name> /bin/sh: Open an interactive shell inside a running container: alpine images need sh.docker exec <name> <cmd>: Run a single command inside a container: check env or run a script.docker exec -it <name> /bin/bash -lc "command": Run a command with a login shell so PATH and env are fully loaded.docker attach <name>: Attach to a container's STDIN/STDOUT: watch an interactive process.docker port <name>: Show the published port mappings of a container.docker diff <name>: Show files changed inside a container since it started: find unexpected writes.docker events: Stream all container/image events in real time: observe lifecycle.docker system events --filter "container=web": Watch events for a single container: debug restarts.Volumes & Networks
docker volume create <name>: Create a named volume for persistent data.docker run -v <name>:/data <image>: Mount a named volume at /data: survives container removal.docker run -v $(pwd):/app <image>: Mount the current directory into the container: live development without rebuilding.docker run -v /host/path:/container/path:ro <image>: Mount a host path read-only: share config that must not change.docker run --tmpfs /tmp <image>: Mount an ephemeral in-memory tmpfs: fast scratch space, nothing persists.docker volume ls: List all volumes.docker volume inspect <name>: Show volume details: mountpoint on the host.docker volume rm <name>: Delete a volume: permanently removes its data.docker volume prune: Remove all volumes not used by any container: be careful, data is gone.docker network create <name>: Create a user-defined bridge network: containers get DNS by name.docker network ls: List all networks.docker network connect <net> <container>: Attach a running container to another network: connect app to database.docker network disconnect <net> <container>: Detach a container from a network.docker network inspect <net>: Show a network's containers and configuration.docker network prune: Remove unused networks.Docker Compose
docker compose up: Create and start all services defined in docker-compose.yml (foreground).docker compose up -d: Start services in the background.docker compose up --build: Rebuild images before starting: apply Dockerfile changes.docker compose -f docker-compose.prod.yml up -d: Use a specific compose file: env-specific overrides.docker compose down: Stop and remove services, networks, and (with -v) volumes.docker compose down -v: Bring everything down INCLUDING volumes: full reset of local data.docker compose ps: List the status of compose services.docker compose logs -f: Follow logs of all services: or specify one: docker compose logs -f web.docker compose exec <service> <cmd>: Run a command in a running service container.docker compose exec <service> /bin/sh: Open a shell in a service container.docker compose restart <service>: Restart one service without touching the others.docker compose pull: Pull the latest images for all services.docker compose build: Build images for all services without starting them.docker compose config: Validate and print the resolved compose configuration: catch YAML errors.docker compose top: Show running processes of compose services.docker compose scale worker=3: Scale a service to N replicas (classic compose).System & Cleanup
docker system df: Show disk usage of images, containers, volumes, and build cache.docker system prune: Remove stopped containers, unused networks, and dangling images.docker system prune -a --volumes: Remove everything unused: images, cache, volumes. Full cleanup.docker builder prune: Clean the build cache: often the biggest disk hog.docker info: Show system-wide Docker info: storage driver, resources, registries.docker version: Show client and server versions: check daemon connectivity.docker login: Authenticate to a registry before pushing.docker logout: Sign out of a registry.docker context ls: List Docker contexts: switch between local and remote daemons.docker context use <name>: Switch to another Docker context, e.g. a remote server daemon.Frequently asked questions
What is the difference between docker run and docker start?
docker run creates and starts a new container from an image with its configuration. docker start restarts an existing, stopped container with its saved state. Use run for new containers and start for reusing old ones.
How do I remove unused Docker resources?
Run docker system prune -a to remove all stopped containers, unused networks, and dangling images. Add --volumes to also remove unused volumes. Use docker image prune for images only.
How do I access a running container?
Use docker exec -it <container> bash (or sh) to open an interactive shell inside a running container. For logs use docker logs -f <container>, and docker inspect <container> to view low-level configuration.
What is the difference between docker build and docker compose?
docker build creates an image from a Dockerfile. docker compose defines and runs multi-container applications from a docker-compose.yml file, building, networking, and starting services together.