Skip to content

Docker Cheatsheet

Published: February 24, 2026 · Last edited: February 24, 2026

A practical reference for Docker commands you need mid-task — images, containers, Compose, volumes, and cleanup.

The 30-Second Version

# Run a container
docker run -d -p 8080:80 --name my-app nginx

# See what's running
docker ps

# Get a shell inside a running container
docker exec -it my-app bash

# Follow logs
docker logs -f my-app

# Stop and remove everything for a project
docker compose down

Images

Pull & Build

# Pull an image from Docker Hub
docker pull nginx
docker pull postgres:16

# Build from a Dockerfile in the current directory
docker build -t my-app .

# Build with a specific Dockerfile and build args
docker build -f Dockerfile.prod --build-arg ENV=production -t my-app:prod .

Inspect & List

# List all local images
docker images

# Show image history (layers)
docker history my-app

# Inspect image metadata
docker inspect my-app

Remove

# Remove a specific image
docker rmi my-app

# Remove all dangling (untagged) images
docker image prune

# Remove all unused images
docker image prune -a

Containers

Run

# Run in background (detached), map port, name the container
docker run -d -p 8080:80 --name my-app nginx

# Run interactively with a shell (removed on exit)
docker run -it --rm ubuntu bash

# Mount a local directory into the container
docker run -d -v $(pwd)/data:/app/data my-app

# Pass environment variables
docker run -d -e DATABASE_URL=postgres://... my-app

# Set memory and CPU limits
docker run -d --memory="512m" --cpus="1.0" my-app

Manage Running Containers

# List running containers
docker ps

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

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

# Pause / unpause
docker pause my-app
docker unpause my-app

Inspect & Debug

# Stream logs
docker logs -f my-app

# Show last 50 lines of logs
docker logs --tail 50 my-app

# Get a shell inside a running container
docker exec -it my-app bash

# Run a one-off command
docker exec my-app env

# Show resource usage (CPU, memory, network)
docker stats

# Show processes running inside a container
docker top my-app

# Inspect container config and state
docker inspect my-app

# Copy a file out of a container
docker cp my-app:/app/config.json ./config.json

Remove

# Remove a stopped container
docker rm my-app

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

# Remove all stopped containers
docker container prune

Docker Compose

Core Commands

# Start all services in the background
docker compose up -d

# Start and rebuild images first
docker compose up -d --build

# Stop all services (keeps containers and volumes)
docker compose stop

# Stop and remove containers, networks
docker compose down

# Stop and remove containers, networks, AND volumes
docker compose down -v

Logs & Status

# Follow logs for all services
docker compose logs -f

# Follow logs for one service
docker compose logs -f app

# Show running services
docker compose ps

Run Commands in Services

# Run a one-off command in a service (starts a new container)
docker compose run --rm app python manage.py migrate

# Exec into an already-running service container
docker compose exec app bash

# Scale a service to 3 replicas
docker compose up -d --scale worker=3

Build & Pull

# Build (or rebuild) all service images
docker compose build

# Pull latest images for all services
docker compose pull

Volumes

# List all volumes
docker volume ls

# Create a named volume
docker volume create my-data

# Inspect a volume (shows mount path)
docker volume inspect my-data

# Remove a specific volume
docker volume rm my-data

# Remove all unused volumes
docker volume prune

Networks

# List networks
docker network ls

# Create a network
docker network create my-network

# Connect a running container to a network
docker network connect my-network my-app

# Inspect a network (shows connected containers)
docker network inspect my-network

# Remove unused networks
docker network prune

System Cleanup

# Remove all stopped containers, unused networks, dangling images, and build cache
docker system prune

# Same, plus all unused images (not just dangling)
docker system prune -a

# Same, plus volumes (destructive — removes data)
docker system prune -a --volumes

# Check disk usage by Docker
docker system df

Common Patterns

Check what's using a port

# Find which container is bound to port 5432
docker ps --format "table {{.Names}}\t{{.Ports}}" | grep 5432

Get the IP address of a container

docker inspect my-app --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'

Tail logs since a specific time

docker logs --since 30m my-app
docker logs --since 2026-02-24T10:00:00 my-app

Override the entrypoint

docker run -it --entrypoint bash my-app

Save and load images (for air-gapped environments)

# Export image to tar
docker save my-app:latest | gzip > my-app.tar.gz

# Load image from tar
docker load < my-app.tar.gz

Quick Reference

Task Command
List running containers docker ps
List all containers docker ps -a
List images docker images
Pull an image docker pull <image>
Build an image docker build -t <name> .
Run a container docker run -d -p <host>:<container> <image>
Shell into container docker exec -it <name> bash
Follow logs docker logs -f <name>
Stop a container docker stop <name>
Remove a container docker rm <name>
Remove an image docker rmi <image>
Compose up docker compose up -d
Compose down docker compose down
Free up disk space docker system prune -a