Docker Cheat Sheet
Docker CLI reference: images, containers, volumes, networks, Compose, and cleanup.
Docker packages an application and its dependencies into images that run as isolated containers. The commands below use current CLI conventions; names and tags are examples, not requirements.
Images
Images are immutable-ish layers identified by tags and digests. Build from a Dockerfile, pull from a registry, inspect metadata, and remove unused tags carefully.
docker pull nginx:latest
docker build -t demo/web:1.0 .
docker images
docker image inspect demo/web:1.0
docker tag demo/web:1.0 registry.example.com/demo/web:1.0
docker push registry.example.com/demo/web:1.0
docker image rm demo/web:1.0
| Command | Purpose |
|---|---|
docker image ls | List local images. |
docker history IMAGE | Show image layers. |
docker image prune | Remove dangling images. |
docker save -o image.tar IMAGE | Export an image archive. |
docker load -i image.tar | Import an image archive. |
Run and inspect containers
docker run creates and starts a container. Use --rm for disposable work, publish ports explicitly, and give long-lived containers meaningful names.
docker run --name web --rm -d -p 8080:80 nginx:latest
docker ps
docker ps -a
docker logs --follow web
docker exec -it web sh
docker inspect web
docker stop web
| Flag | Meaning |
|---|---|
-d | Detached mode. |
--rm | Remove the container when it exits. |
-p 8080:80 | Map host port 8080 to container port 80. |
-v "$PWD/data:/app/data" | Bind mount a host path. |
-e KEY=value | Set an environment variable. |
--env-file .env | Load variables from a file. |
--name web | Assign a stable container name. |
--network app-net | Attach to a user-defined network. |
Environment, mounts, and lifecycle
Bind mounts share a host path; named volumes are managed by Docker and are usually better for persistent service data. Removing a container does not automatically remove its named volumes.
docker volume create app-data
docker run -d --name db -v app-data:/var/lib/postgresql/data postgres:16
docker volume ls
docker volume inspect app-data
docker rm -f db
docker start web
docker restart web
docker rename web web-prod
docker cp web:/etc/nginx/nginx.conf ./nginx.conf
Networks
User-defined bridge networks provide container-to-container DNS by name. Containers on the same network can connect using the target container name and its internal port.
docker network create app-net
docker run -d --name api --network app-net demo/api:1.0
docker run --rm --network app-net curlimages/curl:latest http://api:3000/health
docker network inspect app-net
docker network rm app-net
| Operation | Command |
|---|---|
| List networks | docker network ls |
| Connect a running container | docker network connect app-net web |
| Disconnect a container | docker network disconnect app-net web |
| Remove unused networks | docker network prune |
Cleanup and resource usage
Prune commands can delete data. Review the target before using -a, and never assume a stopped container or volume is disposable.
docker system df
docker container prune
docker image prune -a
docker volume prune
docker network prune
docker system prune
Compose basics
Compose describes related services in YAML and manages them as one application. Run commands from the directory containing compose.yaml or pass -f explicitly.
docker compose up -d
docker compose ps
docker compose logs -f api
docker compose exec api sh
docker compose stop
docker compose down
docker compose down --volumes
| Command | Purpose |
|---|---|
docker compose config | Validate and render the resolved configuration. |
docker compose up --build | Rebuild images before starting. |
docker compose pull | Pull referenced images. |
docker compose restart SERVICE | Restart one service. |
docker compose down --remove-orphans | Stop project containers and remove orphans. |