All cheat sheets

Docker Cheat Sheet

Docker CLI reference: images, containers, volumes, networks, Compose, and cleanup.

Containers
docker
containers
devops

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.

bash
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
Table
CommandPurpose
docker image lsList local images.
docker history IMAGEShow image layers.
docker image pruneRemove dangling images.
docker save -o image.tar IMAGEExport an image archive.
docker load -i image.tarImport 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.

bash
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
Table
FlagMeaning
-dDetached mode.
--rmRemove the container when it exits.
-p 8080:80Map host port 8080 to container port 80.
-v "$PWD/data:/app/data"Bind mount a host path.
-e KEY=valueSet an environment variable.
--env-file .envLoad variables from a file.
--name webAssign a stable container name.
--network app-netAttach 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.

bash
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
bash
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.

bash
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
Table
OperationCommand
List networksdocker network ls
Connect a running containerdocker network connect app-net web
Disconnect a containerdocker network disconnect app-net web
Remove unused networksdocker 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.

bash
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.

bash
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
Table
CommandPurpose
docker compose configValidate and render the resolved configuration.
docker compose up --buildRebuild images before starting.
docker compose pullPull referenced images.
docker compose restart SERVICERestart one service.
docker compose down --remove-orphansStop project containers and remove orphans.

References