← Back to Home

Docker Cheat Sheet

Essential Docker commands for container management

docker run
Create and start a new container from an image. Use -d for detached mode, -p for port mapping, -v for volume mounting, --name to assign a name, and -e for environment variables.

docker run -d -p 80:80 --name my-nginx nginx

Keywords: create, start, container, detached, port, volume, environment

docker build
Build a Docker image from a Dockerfile. Use -t to tag the image, -f to specify a different Dockerfile, and --no-cache to build without using cache.

docker build -t myapp:latest .

Keywords: build, image, dockerfile, tag, cache

docker ps
List containers. Shows running containers by default. Use -a to show all containers (including stopped), -q for quiet mode (only IDs), and --filter to filter results.

docker ps -a --filter status=exited

Keywords: list, containers, running, stopped, filter, status

docker images
List all Docker images on the local system. Use -a to show all images including intermediate ones, -q for quiet mode, and --filter to filter results.

docker images --filter dangling=true

Keywords: list, images, local, dangling, filter

docker stop
Stop one or more running containers gracefully. Docker sends SIGTERM first, then SIGKILL after grace period. Use -t to specify timeout.

docker stop my-container another-container

Keywords: stop, graceful, sigterm, sigkill, timeout

docker rm
Remove one or more containers. Container must be stopped first unless using -f flag. Use -v to remove associated volumes.

docker rm -f my-container

Keywords: remove, delete, container, force, volume

docker rmi
Remove one or more images from local storage. Use -f to force removal even if containers are using the image.

docker rmi -f image_id

Keywords: remove, delete, image, force, local

docker exec
Execute a command inside a running container. Use -it for interactive terminal, -d for detached mode, -u to specify user, and -w for working directory.

docker exec -it my-container /bin/bash

Keywords: execute, command, interactive, terminal, bash, shell

docker logs
Fetch and display logs from a container. Use -f to follow log output, --tail to show last N lines, --since for time filtering, and -t for timestamps.

docker logs -f --tail 100 my-container

Keywords: logs, output, follow, tail, timestamp, debug

docker pull
Download an image from a Docker registry (Docker Hub by default). Specify tag after colon, defaults to 'latest' if no tag specified.

docker pull ubuntu:20.04

Keywords: download, pull, registry, hub, tag, latest

docker push
Upload an image to a Docker registry. Image must be tagged with registry URL. Requires authentication for private registries.

docker push myregistry.com/myapp:v1.0

Keywords: upload, push, registry, tag, authentication

docker network ls
List all Docker networks. Shows bridge, host, and none networks by default, plus any custom networks created.

docker network ls

Keywords: network, list, bridge, host, custom

docker volume ls
List all Docker volumes. Volumes persist data beyond container lifecycle and can be shared between containers.

docker volume ls

Keywords: volume, list, persist, data, shared

docker inspect
Display detailed information about Docker objects (containers, images, volumes, networks). Returns JSON formatted data.

docker inspect my-container

Keywords: inspect, details, information, json, metadata

docker-compose up
Create and start containers defined in docker-compose.yml. Use -d for detached mode, --build to rebuild images, and --scale to scale services.

docker-compose up -d --build

Keywords: compose, multi-container, yaml, orchestration, scale

docker system prune
Clean up unused Docker objects. Remove stopped containers, unused networks, dangling images, and build cache.

docker system prune -a --volumes

Keywords: cleanup, prune, disk, space, unused

docker stats
Display live resource usage statistics for containers. Monitor CPU, memory, network, and disk I/O in real-time.

docker stats --no-stream

Keywords: stats, monitoring, cpu, memory, performance

docker cp
Copy files between container and host filesystem. Transfer files in both directions without stopping container.

docker cp container:/app/logs ./logs

Keywords: copy, files, transfer, host, container

docker commit
Create new image from container's changes. Save current state of container as a new image layer.

docker commit my-container my-image:v2

Keywords: commit, save, state, image, layer

docker save
Save image to tar archive. Export images for offline transfer or backup purposes.

docker save -o myapp.tar myapp:latest

Keywords: save, export, tar, backup, offline

docker load
Load image from tar archive. Import previously saved images from file.

docker load -i myapp.tar

Keywords: load, import, tar, restore, file

docker port
List port mappings for container. Show which host ports are mapped to container ports.

docker port my-container

Keywords: port, mapping, network, expose

docker top
Display running processes inside container. Similar to 'ps' command but for container processes.

docker top my-container

Keywords: top, processes, ps, running

docker history
Show image layer history. View how image was built and size of each layer.

docker history nginx:latest

Keywords: history, layers, build, size

docker update
Update container resource limits. Modify CPU, memory limits of running containers.

docker update --memory 512m my-container

Keywords: update, resources, limits, memory, cpu

docker wait
Wait for container to stop and return exit code. Useful in scripts for synchronization.

docker wait my-container

Keywords: wait, exit, code, synchronization, script