Docker has roughly a million commands and flags. You don't need most of them. In practice, you'll use about 30 commands on a regular basis, and those 30 will cover 95% of your daily workflow. This cheat sheet is those 30 commands, organized by what you're actually trying to do - not alphabetically, not by some abstract category. Bookmark it, reference it, get back to work.
1. Images - Building and Managing
Images are your starting point. You build them, pull them, tag them, and eventually clean them up when they eat your disk space.
docker build
Build an image from a Dockerfile.
docker build -t myapp:latest .
docker build -t myapp:latest -f Dockerfile.prod .
docker build -t myapp:latest --no-cache .
docker build -t myapp:latest --build-arg NODE_ENV=production --build-arg API_KEY=abc123 .
docker pull / docker push
Get images from and push images to a registry.
docker pull node:20-alpine
docker pull --platform linux/amd64 node:20-alpine
docker push myregistry.com/myapp:latest
docker images
List your local images.
docker images
docker images node
docker images -q
docker rmi / docker image prune
Remove images you don't need.
docker rmi myapp:latest
docker rmi -f myapp:latest
docker image prune
docker image prune -a
docker tag
Tag an image for pushing to a registry or versioning.
docker tag myapp:latest myregistry.com/myapp:latest
docker tag myapp:latest myapp:v1.2.3
Multi-stage Build Example
This is one of the most useful Docker patterns. Build in one stage, run in another - keeps your final image small.
# Build stage
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Production stage
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
EXPOSE 3000
CMD ["node", "dist/index.js"]
docker build -t myapp:latest .
docker build -t myapp:dev --target builder .
2. Containers - Running and Managing
This is where you'll spend most of your time. Running containers, checking on them, stopping them, and figuring out why they crashed.
docker run
The big one. This is the command you'll use more than any other.
docker run -d --name myapp myapp:latest
docker run -d -p 3000:3000 myapp:latest
docker run -d -v $(pwd):/app myapp:latest
docker run -d -e NODE_ENV=production -e DB_HOST=localhost myapp:latest
docker run -d --env-file .env myapp:latest
docker run -it node:20-alpine sh
docker run --rm myapp:latest
docker run -d --network mynetwork myapp:latest
docker run -d \
--name myapp \
-p 3000:3000 \
-v $(pwd)/data:/app/data \
-e NODE_ENV=production \
--env-file .env \
--network mynetwork \
--restart unless-stopped \
myapp:latest
docker ps
See what's running.
docker ps
docker ps -a
docker ps -q
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
docker stop / start / restart
Control container lifecycle.
docker stop myapp
docker stop -t 5 myapp
docker start myapp
docker restart myapp
docker stop $(docker ps -q)
docker rm / docker container prune
Remove containers.
docker rm myapp
docker rm -f myapp
docker container prune
docker rm $(docker ps -a -q)
docker exec
Run commands inside a running container. This is your go-to for debugging.
docker exec -it myapp sh
docker exec -it myapp bash
docker exec myapp ls /app
docker exec -u root myapp cat /etc/passwd
docker exec -e DEBUG=true myapp node script.js
docker logs
See what your container is printing.
docker logs myapp
docker logs -f myapp
docker logs --tail 100 myapp
docker logs --since 2024-01-01T00:00:00 myapp
docker logs --since 30m myapp
docker logs -f --tail 50 myapp
docker cp
Copy files between your machine and a container.
docker cp myapp:/app/config.json ./config.json
docker cp ./fix.js myapp:/app/fix.js
docker cp myapp:/app/logs ./logs
3. Debugging and Inspection
When things go wrong (and they will), these commands help you figure out why.
docker inspect
Get detailed info about a container or image. The output is JSON, so use --format to pull out what you need.
docker inspect myapp
docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' myapp
docker inspect --format '{{.Config.Env}}' myapp
docker inspect --format '{{.RestartCount}}' myapp
docker inspect --format '{{json .Mounts}}' myapp | jq
docker stats
Real-time resource usage. Like top but for containers.
docker stats
docker stats myapp
docker stats --no-stream
docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"
docker top
See processes running inside a container.
docker top myapp
docker top myapp -o pid,user,%cpu,%mem,command
docker diff
See what files changed in a container compared to its image.
docker diff myapp
docker history
See the layers that make up an image. Useful for understanding why an image is so big.
docker history myapp:latest
docker history --no-trunc myapp:latest
Debugging a Crashing Container
When a container keeps crashing, here's the workflow:
docker logs myapp
docker inspect --format '{{.State.ExitCode}}' myapp
docker run -it myapp:latest sh
docker run -it --entrypoint sh myapp:latest
docker stats --no-stream myapp
docker events --since 10m --filter container=myapp
4. Volumes and Data
Volumes are how you persist data and share files between your machine and containers.
Volume Commands
docker volume create mydata
docker volume ls
docker volume inspect mydata
docker volume rm mydata
docker volume prune
Named Volumes vs Bind Mounts vs tmpfs
These are the three ways to handle data in Docker. Each has its use case.
docker run -d -v mydata:/var/lib/postgresql/data postgres:16
docker run -d -v $(pwd)/src:/app/src myapp:latest
docker run -d --tmpfs /app/tmp myapp:latest
Common Volume Patterns
docker run -d \
--name postgres \
-v pgdata:/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=secret \
postgres:16
docker run -d \
-v $(pwd):/app \
-v /app/node_modules \
myapp:dev
docker run -d --name writer -v shared:/data alpine sh -c "echo hello > /data/msg"
docker run --rm -v shared:/data alpine cat /data/msg
Note the second pattern above - the -v /app/node_modules line creates an anonymous volume that prevents your host's node_modules from overwriting the container's. This is a common gotcha in development setups.
5. Networks
Networks let containers talk to each other. You'll mostly deal with this in Docker Compose, but it's good to know the basics.
Network Commands
docker network create mynetwork
docker network ls
docker network inspect mynetwork
docker network rm mynetwork
docker network connect mynetwork myapp
docker network disconnect mynetwork myapp
Network Types
docker network create --driver bridge mynetwork
docker run -d --name api --network mynetwork myapp:latest
docker run -d --name db --network mynetwork postgres:16
docker run -d --network host myapp:latest
docker run -d --network none myapp:latest
The key thing to remember: containers on the same user-defined bridge network can reach each other by container name. That's how your app container talks to your database container.
6. Docker Compose - The Daily Driver
Docker Compose is what you'll use 90% of the time in development. It defines your entire stack in one file and manages it with simple commands.
docker compose up
Start your stack.
docker compose up -d
docker compose up -d --build
docker compose up -d --force-recreate
docker compose up -d api db
docker compose down
Stop and remove everything.
docker compose down
docker compose down -v
docker compose down --rmi local
docker compose ps / logs
Check on your stack.
docker compose ps
docker compose logs
docker compose logs -f api
docker compose logs --tail 50 api
docker compose exec
Run commands in a running service.
docker compose exec api sh
docker compose exec api npm run migrate
docker compose exec -u root api sh
docker compose build
Build or rebuild services.
docker compose build
docker compose build --no-cache
docker compose build api
Example docker-compose.yml
A typical web app with Node.js, Postgres, and Redis:
version: "3.8"
services:
api:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=development
- DATABASE_URL=postgres://postgres:secret@db:5432/myapp
- REDIS_URL=redis://redis:6379
volumes:
- .:/app
- /app/node_modules
depends_on:
- db
- redis
restart: unless-stopped
db:
image: postgres:16-alpine
ports:
- "5432:5432"
environment:
- POSTGRES_DB=myapp
- POSTGRES_PASSWORD=secret
volumes:
- pgdata:/var/lib/postgresql/data
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
pgdata:
docker compose up -d
docker compose ps
docker compose logs -f api
docker compose exec api npm run migrate
docker compose down
7. Cleanup - Reclaiming Disk Space
Docker will eat your disk alive if you don't clean up periodically. Here's how to take it back.
docker system df
See what's using space.
docker system df
docker system df -v
docker system prune
The all-in-one cleanup command.
docker system prune
docker system prune -a
docker system prune -a --volumes
docker system prune -a -f
Targeted Cleanup
Sometimes you want to clean up just one type of resource.
docker container prune
docker image prune
docker image prune -a
docker volume prune
docker network prune
The Nuclear Option
When you want to start completely fresh:
docker stop $(docker ps -q) 2>/dev/null; docker system prune -a --volumes -f
This will destroy all your data volumes too, so be careful. But sometimes you just need a clean slate.
Quick Reference Table
| What You Want to Do | Command |
|---|
| Build an image | docker build -t name:tag . |
| Run a container | docker run -d -p 3000:3000 --name app image |
| List running containers | docker ps |
| List all containers | docker ps -a |
| Stop a container | docker stop app |
| Remove a container | docker rm app |
| View logs | docker logs -f app |
| Shell into a container | docker exec -it app sh |
| Run a command in a container | docker exec app ls /app |
| Copy files out | docker cp app:/path ./local |
| Copy files in | docker cp ./local app:/path |
| List images | docker images |
| Remove an image | docker rmi image:tag |
| Pull an image | docker pull image:tag |
| Tag an image | docker tag image:tag newname:tag |
| Check resource usage | docker stats |
| Inspect a container | docker inspect app |
| Create a volume | docker volume create name |
| Create a network | docker network create name |
| Start Compose stack | docker compose up -d |
| Stop Compose stack | docker compose down |
| Rebuild Compose stack | docker compose up -d --build |
| View Compose logs | docker compose logs -f service |
| Shell into Compose service | docker compose exec service sh |
| See disk usage | docker system df |
| Clean everything | docker system prune -a --volumes |
That's the whole cheat sheet. These commands cover the vast majority of what you'll do with Docker day to day. Keep this bookmarked, and you'll spend less time searching Stack Overflow and more time actually building things.