← Home

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 image
docker run -d <image>Run container in detached (background) mode
docker run -it <image> bashRun container interactively with a shell (TTY attached)
docker run --name my-app <image>Run container with a custom name
docker run -p 8080:80 <image>Map host port 8080 to container port 80
docker run -v /host:/container <image>Bind-mount a host directory into the container
docker run --rm <image>Automatically remove container when it exits
docker run -e KEY=value <image>Set an environment variable inside the container
docker run --env-file .env <image>Load environment variables from a file
docker run --network my-net <image>Connect the container to a specific network
docker start <container>Start a stopped container
docker stop <container>Gracefully stop a running container (SIGTERM, then SIGKILL)
docker restart <container>Stop and then start a container
docker rm <container>Remove a stopped container
docker rm -f <container>Force remove a running container
docker kill <container>Send SIGKILL to immediately terminate a container
docker pause <container>Freeze all processes in a container (SIGSTOP)
docker unpause <container>Resume a paused container
docker rename <old> <new>Rename an existing container
docker wait <container>Block until a container stops, then print exit code
docker update --memory 512m <c>Update resource limits on a running container
docker create <image>Create a container without starting it

Container Inspection

docker psList running containers
docker ps -aList all containers including stopped ones
docker ps -qList only container IDs (useful for scripting)
docker ps --format '{{.Names}}'Custom output format — show only container names
docker logs <container>Show stdout/stderr logs from a container
docker logs -f <container>Follow logs in real time (like tail -f)
docker logs --tail 100 <container>Show only the last 100 log lines
docker logs --since 1h <container>Show logs from the last hour
docker top <container>Display running processes inside a container
docker statsLive stream of CPU, memory, I/O for all running containers
docker stats --no-streamShow resource usage snapshot (no live updates)
docker inspect <container>Return detailed JSON metadata about a container
docker inspect -f '{{.State.Status}}' <c>Extract a specific field using Go templates
docker port <container>Show port mappings for a container
docker diff <container>Show filesystem changes since the container started
docker exec -it <container> bashOpen an interactive shell inside a running container
docker exec <container> <cmd>Run a one-off command inside a running container
docker cp <c>:/path /host/pathCopy files from a container to the host
docker cp /host/path <c>:/pathCopy files from the host into a container
docker attach <container>Attach stdin/stdout/stderr to a running container

Images

docker build -t myapp:v1 .Build an image from a Dockerfile in current directory
docker build -f Dockerfile.prod .Build using a specific Dockerfile
docker build --no-cache -t myapp .Build without using layer cache
docker build --target stage .Build up to a specific multi-stage target
docker build --build-arg KEY=val .Pass build-time variables to Dockerfile ARG
docker pull <image>:<tag>Download an image from a registry
docker pull --all-tags <image>Pull all tagged versions of an image
docker push <image>:<tag>Upload an image to a registry
docker imagesList all locally stored images
docker images -aList all images including intermediate layers
docker rmi <image>Remove a local image
docker rmi $(docker images -q)Remove all local images
docker tag <image> <new:tag>Create a new tag (alias) for an image
docker history <image>Show the layer history and size of each layer
docker save -o backup.tar <image>Export an image to a tar archive
docker load -i backup.tarImport an image from a tar archive
docker import archive.tar myimageCreate an image from a filesystem archive
docker image inspect <image>Show detailed metadata for an image
docker buildx build --platform linux/amd64,linux/arm64 .Multi-platform build with Buildx

Docker Compose

docker compose upCreate and start all services defined in compose file
docker compose up -dStart services in detached (background) mode
docker compose up --buildRebuild images before starting services
docker compose up <service>Start only a specific service and its dependencies
docker compose downStop and remove containers, networks created by up
docker compose down -vStop services and also remove named volumes
docker compose down --rmi allStop services and remove all related images
docker compose buildBuild or rebuild all service images
docker compose build --no-cacheBuild without using the Docker layer cache
docker compose logsShow combined logs from all services
docker compose logs -f <service>Follow logs from a specific service in real time
docker compose psList containers managed by the current compose project
docker compose exec <svc> bashOpen a shell inside a running service container
docker compose run <svc> <cmd>Run a one-off command in a new service container
docker compose pullPull latest images for all services
docker compose restart <service>Restart a specific service
docker compose configValidate and display the resolved compose file
docker compose stopStop services without removing containers
docker compose -f prod.yml up -dUse a custom compose file
docker compose --profile debug upStart services matching a specific profile

Volumes & Networks

docker volume create my-volCreate a named volume for persistent data
docker volume lsList all volumes
docker volume inspect my-volShow detailed volume metadata (mount point, driver)
docker volume rm my-volRemove a specific volume
docker volume pruneRemove all unused volumes
docker run -v my-vol:/data <image>Mount a named volume into a container
docker run --mount type=bind,src=.,dst=/app <img>Bind mount with explicit syntax
docker run --tmpfs /tmp <image>Mount a temporary filesystem in memory
docker network create my-netCreate a user-defined bridge network
docker network create --driver overlay netCreate an overlay network for Swarm services
docker network lsList all networks
docker network inspect my-netShow network config, connected containers, IP ranges
docker network rm my-netRemove a network
docker network connect my-net <c>Attach a running container to a network
docker network disconnect my-net <c>Detach a container from a network
docker network pruneRemove all unused networks

Registry & Repository

docker loginAuthenticate with Docker Hub (prompts for credentials)
docker login registry.example.comLog in to a private registry
docker login -u user -p tokenLog in with username and password/token inline
docker logoutRemove stored credentials for Docker Hub
docker logout registry.example.comLog out from a specific registry
docker search nginxSearch Docker Hub for images matching a term
docker search --filter is-official=true nginxSearch for only official images
docker pull nginx:alpinePull a specific tagged image from the registry
docker tag myapp registry.io/myapp:v1Tag a local image for a private registry
docker push registry.io/myapp:v1Push an image to a private registry
docker manifest inspect <image>View the manifest (platforms, digests) for an image

System & Cleanup

docker system pruneRemove all stopped containers, unused networks, dangling images
docker system prune -aRemove everything including unused images (reclaim disk)
docker system prune -a --volumesFull cleanup including unused volumes
docker system dfShow Docker disk usage by images, containers, volumes
docker system df -vVerbose disk usage with per-item breakdown
docker system infoDisplay system-wide Docker configuration and stats
docker versionShow Docker client and server version info
docker image pruneRemove dangling (untagged) images
docker image prune -aRemove all images not used by any container
docker container pruneRemove all stopped containers
docker volume pruneRemove all volumes not used by any container
docker network pruneRemove all networks not used by any container
docker builder pruneRemove build cache to free disk space
docker context lsList available Docker contexts (local, remote, cloud)
docker context use <name>Switch to a different Docker context

FAQ

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.

Related Resources