PureTools

Docker Commands: The Complete Cheatsheet

PureTools Team· 9 min read
Docker Commands: The Complete Cheatsheet

Docker Commands: The Complete Cheatsheet

Docker has hundreds of commands and flags. You don't need to memorize them all — but you need fast access to the ones that matter. This is the reference I wish I had when I started.

Containers: The Basics

# Run a container (detached, with name)
docker run -d --name my-app -p 8080:80 nginx

# List running containers
docker ps

# List ALL containers (including stopped)
docker ps -a

# Stop / start / restart
docker stop my-app
docker start my-app
docker restart my-app

# Remove a stopped container
docker rm my-app

# Force remove a running container
docker rm -f my-app

Logs and Debugging

# View logs
docker logs my-app

# Follow logs in real-time (like tail -f)
docker logs -f my-app

# Last 100 lines
docker logs --tail 100 my-app

# Open a shell inside a running container
docker exec -it my-app sh

# Run a one-off command
docker exec my-app cat /etc/nginx/nginx.conf

Images

# Build from Dockerfile
docker build -t my-app:latest .

# Build with no cache
docker build --no-cache -t my-app:latest .

# List images
docker images

# Remove an image
docker rmi my-app:latest

# Pull from registry
docker pull postgres:16-alpine

Volumes

# Create a named volume
docker volume create my-data

# Run with volume
docker run -v my-data:/var/lib/postgresql/data postgres:16

# Bind mount (host directory)
docker run -v $(pwd):/app node:22-alpine

# List / remove volumes
docker volume ls
docker volume rm my-data

Docker Compose

# Start all services
docker compose up -d

# Stop and remove
docker compose down

# Rebuild and start
docker compose up -d --build

# View logs
docker compose logs -f

# Execute command in service
docker compose exec web sh

System Cleanup (the lifesaver)

# Show disk usage
docker system df

# Remove dangling images (safe)
docker image prune -f

# Remove ALL unused data (containers, images, volumes)
docker system prune -a --volumes

# Remove stopped containers
docker container prune

That last section is critical. Docker accumulates cruft fast — old images, stopped containers, build cache. I've seen servers hit 100% disk because nobody ran docker system prune. Set up a weekly cron job.

Useful Flags

FlagMeaning
-dDetached (background)
-itInteractive + TTY (for shell access)
-p 8080:80Map host port 8080 to container port 80
-vMount volume
-e KEY=valSet environment variable
--rmRemove container when it exits
--nameGive the container a name

Interactive reference: Docker Cheatsheet — searchable, filterable, click to copy.