Skip to main content

Docker and Docker Compose Cheatsheet

·729 words·4 mins·
Photo by Venti Views on Unsplash
This cheatsheet is a quick reference for common Docker and Docker Compose tasks. It covers image and container lifecycle management, inspection and cleanup, storage and networking, and the most useful Compose commands for local development and small production deployments.

Version Check
#

docker version
docker info
docker compose version

Images
#

docker pull nginx:latest                    # Download an image
docker images                               # List local images
docker image ls                             # Equivalent modern form
docker rmi nginx:latest                     # Remove an image
docker build -t myapp:latest .              # Build from Dockerfile
docker build --no-cache -t myapp:latest .   # Rebuild without cache

Containers
#

docker run hello-world                      # Run a one-shot container
docker run -it ubuntu bash                  # Interactive shell
docker run -d --name web -p 8080:80 nginx   # Detached container with port mapping
docker run --rm alpine echo hello           # Remove container on exit
docker ps                                   # List running containers
docker ps -a                                # List all containers
docker stop web                             # Stop a container
docker start web                            # Start a stopped container
docker restart web                          # Restart a container
docker rm web                               # Remove a container
docker exec -it web sh                      # Open a shell in a running container

Inspect And Debug
#

docker logs web                             # Show container logs
docker logs -f web                          # Follow logs
docker inspect web                          # Detailed container metadata
docker stats                                # Live resource usage
docker top web                              # Processes inside a container
docker events                               # Stream Docker events

Environment And Runtime Flags
#

docker run -e APP_ENV=prod myapp            # Set environment variables
docker run --env-file .env myapp            # Load environment variables from file
docker run -v "$PWD":/app myapp             # Bind mount current directory
docker run -v data:/var/lib/data myapp      # Named volume mount
docker run --network mynet myapp            # Join a custom network
docker run --restart unless-stopped myapp   # Restart policy
docker run --name api --memory 512m myapp   # Memory limit

Volumes
#

docker volume ls                            # List volumes
docker volume create data                   # Create a volume
docker volume inspect data                  # Inspect a volume
docker volume rm data                       # Remove a volume
docker system df                            # Show Docker disk usage

Networks
#

docker network ls                           # List networks
docker network create mynet                 # Create a bridge network
docker network inspect mynet                # Inspect a network
docker network connect mynet web            # Connect container to network
docker network disconnect mynet web         # Disconnect container from network

Dockerfile Basics
#

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]
docker build -t my-python-app .
docker run --rm my-python-app

Docker Compose Basics
#

services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
    volumes:
      - ./site:/usr/share/nginx/html:ro
  db:
    image: postgres:16
    environment:
      POSTGRES_DB: app
      POSTGRES_USER: app
      POSTGRES_PASSWORD: secret
docker compose up                          # Start services
docker compose up -d                       # Start in background
docker compose up --build                  # Rebuild images and start
docker compose down                        # Stop and remove resources
docker compose down -v                     # Also remove volumes
docker compose ps                          # Show service status
docker compose logs                        # View logs
docker compose logs -f web                 # Follow logs for one service
docker compose exec web sh                 # Open a shell in a service container
docker compose run --rm web sh             # Run one-off command in a new container

Compose Project Management
#

docker compose config                       # Render the effective configuration
docker compose config --services            # List services
docker compose config --volumes             # List named volumes
docker compose pull                         # Pull service images
docker compose build                        # Build service images
docker compose restart                      # Restart services
docker compose stop                         # Stop services without removing them

Compose Overrides And Environment
#

docker compose -f compose.yaml -f compose.override.yaml up -d
docker compose --env-file .env up -d
COMPOSE_PROJECT_NAME=myapp docker compose up -d

Cleanup
#

docker container prune                      # Remove stopped containers
docker image prune                          # Remove dangling images
docker volume prune                         # Remove unused volumes
docker network prune                        # Remove unused networks
docker system prune                         # Clean up unused objects
docker system prune -a --volumes            # Aggressive cleanup

Handy Tips
#

  • Use named volumes for persistent state and bind mounts for active development.
  • Add --rm to throwaway containers so they do not clutter docker ps -a.
  • Use docker compose config before deployment to catch YAML and interpolation issues early.
  • Treat docker system prune -a --volumes carefully; it can delete data you still need.