Docker Commands Cheat Sheet — From Beginner to Daily Driver
DevToolKitThe Docker commands I actually use every day, organized by how often I reach for them.
Every Day
# See running containers docker ps # See ALL containers (including stopped) docker ps -a # Start/stop/restart docker start <name> docker stop <name> docker restart <name> # View logs (follow mode) docker logs -f <name> docker logs --tail 100 <name> # Run a command in a running container docker exec -it <name> bash docker exec -it <name> sh # if bash isn't installed
Building & Running
# Build an image docker build -t myapp:latest . # Run with port mapping and env vars docker run -d \ --name myapp \ -p 8080:3000 \ -e DATABASE_URL=postgres://... \ -v /host/data:/app/data \ myapp:latest # Run and remove when done (great for testing) docker run --rm -it myapp:latest # Run with auto-restart docker run -d --restart unless-stopped myapp:latest
Docker Compose (the real way)
# Start all services docker compose up -d # Rebuild and start docker compose up -d --build # View logs for all services docker compose logs -f # Restart one service docker compose restart webapp # Stop everything docker compose down # Stop and remove volumes (nuclear) docker compose down -v # Scale a service docker compose up -d --scale worker=3
Cleanup (Save Your Disk)
# See disk usage docker system df # Remove stopped containers docker container prune -f # Remove unused images docker image prune -a -f # Remove unused volumes docker volume prune -f # Nuclear option docker system prune -a --volumes -f # Remove images older than 24h docker image prune -a --filter 'until=24h'
Debugging
# Inspect a container docker inspect <name> # See resource usage docker stats # See container processes docker top <name> # Copy files to/from container docker cp <name>:/app/logs.txt ./logs.txt docker cp ./config.json <name>:/app/config.json # See image layers docker history myapp:latest # Export container filesystem docker export <name> > backup.tar
Networking
# List networks docker network ls # Create a network docker network create mynet # Run container on specific network docker run -d --network mynet myapp # Connect running container to network docker network connect mynet <name> # Inspect network (see IPs) docker network inspect mynet
Pro tip: alias dc='docker compose' in your .bashrc. Your fingers will thank you.