Docker is a powerful platform for building, packaging, distributing, and running applications in lightweight, isolated containers. I put together this cheatsheet because, with so many Docker commands to remember, I often found myself forgetting the exact syntax or mixing up options. This guide collects the essential and advanced commands I use most frequently, along with clear explanations and real-world examples. My goal is to make it easy to quickly refresh your memory, understand what each command does, and apply it confidently. Whether you're working with container lifecycles, networking, volumes, or multi-service setups with Docker Compose, I've organized everything in a clean, structured format that's easy to scan and use.
1. Docker System / Info Commands
docker --version
docker version
docker info
2. Docker Image Commands
docker pull <image>
docker pull nginx
docker pull <image>:<tag>
docker pull node:20
docker images
-a: show intermediate layers.--digests: display image digests.
docker images --digests
docker build .
docker build .
docker build -t <name> .
-t: label the built image.-f: specify a custom Dockerfile.--build-arg: define build-time variables.
docker build -t myapp:latest .
docker rmi <image>
-f: force removal.
docker rmi nginx
3. Docker Container Commands
docker run <image>
docker run nginx
docker run -it <image>
-i: interactive mode.-t: allocate terminal.
docker run -it ubuntu bash
docker run -d <image>
-d: detached mode.
docker run -d redis
docker run -p host:container <image>
-p: publish port.
docker run -p 8080:80 nginx
docker logs <container>
-f: follow logs.--tail N: show last N lines.
docker logs -f api
docker exec -it <container> sh
-i: interactive.-t: TTY.
docker exec -it alpine sh
4. Docker Volume Commands
docker volume create <name>
docker volume create dbdata
docker volume ls
docker volume inspect <name>
docker volume inspect dbdata
docker volume rm <name>
docker volume rm dbdata
docker volume prune
-f: skip confirmation prompt.
docker volume prune -f
5. Docker Network Commands
docker network ls
bridge, host, none,
and user-defined networks.
docker network create <name>
docker network create backend
docker network inspect <name>
docker network inspect backend
docker network connect <network> <container>
docker network connect backend api
docker network disconnect <network> <container>
docker network disconnect backend api
6. Docker Registry Commands
docker login
-u: username.-p: password (avoid using inline; prefer prompt).
docker login ghcr.io
docker logout
docker logout ghcr.io
docker tag <source> <target>
docker tag myapp ghcr.io/me/myapp:v1
docker push <image>
docker push ghcr.io/me/myapp:v1
7. Docker Compose Commands
docker compose up
docker-compose.yml or compose.yml.
Runs in foreground unless detached.
-d: run services in detached mode.--build: rebuild images before launching.
docker compose up --build
docker compose down
docker compose up.
-v: remove volumes defined in the stack.--rmi all: remove all images built by Compose.
docker compose down -v
docker compose ps
docker compose logs
-f: follow logs live.--tail: limit the number of lines shown.
docker compose logs -f api
docker compose build
--no-cache: build without using the layer cache.--pull: attempt pulling newer base images.
docker compose build --no-cache
docker compose exec <service> sh
-no-tty: disable TTY allocation.
docker compose exec app sh
docker compose restart
docker compose restart api
8. Docker System Cleanup
docker system df
docker system df
docker system prune
-f: skip confirmation.--volumes: also remove unused volumes.
docker system prune -f
docker system prune -a
-a: remove all unused images.-f: skip confirmation.
docker system prune -a -f
docker container prune
-f: skip prompt.
docker container prune -f
docker image prune -a
-a: remove all unused images.-f: skip confirmation.
docker image prune -a -f
docker volume prune
-f: skip prompt.
docker volume prune -f
9. Advanced Docker Commands
docker save -o file.tar <image>
-o: output file.
docker save -o app.tar myapp:latest
docker load -i file.tar
docker save.
Restores all tags and layers.
-i: input tar file.
docker load -i app.tar
docker export <container> > file.tar
docker export api > api.tar
docker import file.tar
docker export or third-party rootfs bundles.
docker import api.tar
docker stats
--no-stream: output only one snapshot instead of continuous streaming.
docker stats
docker top <container>
docker top api
10. Docker Context Commands
docker context ls
docker context ls
docker context create <name>
docker context create dev-server
docker context use <name>
docker context use dev-server
