Docker Commands Cheatsheet: Images, Containers, Compose and Cleanup
The Docker commands used daily: build and manage images, run and debug containers, wire volumes and networks, drive Docker Compose, and clean up disk space safely.
1. Images: Build, Tag and Share
An image is the packaged template; a container is a running instance of it. Keep images small and tagged by version, never only "latest".
| Command | What it does |
|---|---|
docker build -t myapp:1.0 . | Build an image from the Dockerfile in this folder |
docker images | List local images and their sizes |
docker pull nginx:1.27 | Download an image from a registry |
docker tag myapp:1.0 registry.example.com/myapp:1.0 | Add another name/tag to an image |
docker push registry.example.com/myapp:1.0 | Upload the image to the registry |
docker rmi myapp:1.0 | Remove an image (containers using it must go first) |
docker history myapp:1.0 | Show the layers an image is built from |
2. Containers: Run, Inspect and Debug
The daily loop: start a container detached, watch its logs, jump inside when something looks wrong, and stop it cleanly.
| Command | What it does |
|---|---|
docker run -d --name web -p 8080:80 nginx | Run detached, name it, publish host port 8080 to container port 80 |
docker ps / docker ps -a | List running containers / all containers including stopped |
docker logs -f web | Stream container logs (Ctrl+C detaches, container keeps running) |
docker exec -it web sh | Open a shell inside the running container |
docker inspect web | Full JSON detail: IP, mounts, env, state |
docker stop web / docker start web | Stop gracefully (SIGTERM, then SIGKILL) / start again |
docker rm web | Delete a stopped container (add -f to force a running one) |
docker stats | Live CPU, memory and network per container |
3. Volumes and Networks
Containers are disposable; data is not. Anything that must survive a container replacement belongs in a volume, and services find each other by name on a shared network.
# Volumes: persistent data that outlives containers
docker volume create app-data
docker volume ls
docker run -d --name db -v app-data:/var/lib/postgresql/data postgres:16
# Bind mount: a host folder, ideal for local development
docker run -d --name dev -v $(pwd):/app -w /app node:20 npm run dev
# Networks: containers on the same network resolve each other by name
docker network create backend
docker network connect backend web
docker network ls
docker network inspect backend
4. Docker Compose and Cleanup
Compose declares the whole stack in one YAML file: services, ports, volumes, environment. One command builds and starts everything; the cleanup commands at the end keep Docker from eating your disk.
# docker-compose.yml (essentials)
services:
api:
build: .
ports: ["8000:8000"]
environment: ["DATABASE_URL=postgres://db:5432/app"]
depends_on: [db]
db:
image: postgres:16
volumes: ["db-data:/var/lib/postgresql/data"]
volumes:
db-data:
docker compose up -d # build + start the stack detached
docker compose ps # status of the stack
docker compose logs -f api # follow one service
docker compose exec api sh # shell into a service
docker compose down # stop and remove (add -v to also drop volumes)
# Cleanup: see usage first, then reclaim
docker system df
docker container prune # stopped containers
docker image prune # dangling images (-a removes all unused)
docker system prune # stopped containers + unused networks + dangling images
π Associated Learning Path & Monetization Blueprints
Docker Containerization and Production Deployments
From these commands to production: multi-stage builds, hardening and deployment patterns.
Read Full Guide βPrivate On-Premise AI
Deploy self-hosted AI stacks with Docker for clients who cannot use public clouds.
Read Blueprint βNeed another cheat sheet?
We add new reference guides every week based on community requests.
Request a Cheatsheet β