Basic Commands

docker version

docker version      # Show Docker version information
docker -v          # Show Docker version in short format

Shows both client and server version information, helping verify your installation and client-server communication.

docker info

docker info        # Display system-wide information

Shows detailed information about your Docker installation, including number of containers, images, and system configuration.

Image Management

docker build

docker build [options] .
  -t "app/container_name"    # Name and optionally tag the image
  -f Dockerfile.dev         # Specify custom Dockerfile
  --build-arg APP_HOME=$APP_HOME    # Set build-time variables
  --no-cache               # Build without using cache
  --pull                   # Always pull newer versions of base image

Creates an image from a Dockerfile. Common scenarios:

# Build with specific tag
docker build -t myapp:1.0 .

# Build with multiple tags
docker build -t myapp:1.0 -t myapp:latest .

# Build using custom Dockerfile
docker build -f Dockerfile.prod -t myapp:production .

docker run

docker run [options] IMAGE [COMMAND] [ARG...]
  -d, --detach            # Run in background
  -e, --env KEY=VALUE     # Set environment variables
  --env-file FILE         # Read environment variables from file
  --rm                    # Remove container when it exits
  --name NAME             # Assign a name to the container
  -p, --publish HOST:CONTAINER  # Publish ports
  -v, --volume HOST:CONTAINER   # Bind mount a volume
  --network NETWORK       # Connect to a network

Common scenarios:

# Run interactive shell
docker run -it ubuntu:20.04 bash

# Run web application
docker run -d -p 80:3000 -e NODE_ENV=production --name myapp myapp:1.0

# Run with volume mount
docker run -v $(pwd):/app myapp:1.0

# Run with resource constraints
docker run --memory="512m" --cpus="1.5" myapp:1.0

Container Management

docker compose

docker compose [options] COMMAND
  up          # Create and start containers
  down        # Stop and remove containers
  ps          # List containers
  logs        # View output from containers
  exec        # Execute command in container

Common compose scenarios:

# Start services in background
docker compose up -d

# Scale specific service
docker compose up -d --scale web=3

# View logs from all services
docker compose logs -f

# Execute command in service container
docker compose exec web npm test

docker exec

docker exec [options] CONTAINER COMMAND
  -i, --interactive    # Keep STDIN open
  -t, --tty           # Allocate pseudo-TTY
  -e, --env KEY=VALUE # Set environment variables
  -w, --workdir       # Working directory inside container

Common scenarios:

# Interactive shell
docker exec -it myapp bash

# Run command with environment variable
docker exec -e NODE_ENV=test myapp npm test

# Run command in specific directory
docker exec -w /app myapp ls -la

Network Management

docker network

docker network COMMAND
  create      # Create a network
  connect     # Connect container to network
  disconnect  # Disconnect container from network
  ls          # List networks
  rm          # Remove network

Common scenarios:

# Create network with custom driver
docker network create --driver bridge mynetwork

# Connect container to network
docker network connect mynetwork mycontainer

# Inspect network
docker network inspect mynetwork

Volume Management

docker volume

docker volume COMMAND
  create      # Create a volume
  inspect     # Display volume information
  ls          # List volumes
  rm          # Remove volume
  prune       # Remove unused volumes

Common scenarios:

# Create named volume
docker volume create mydata

# Run container with named volume
docker run -v mydata:/app/data myapp:1.0

# Backup volume data
docker run --rm -v mydata:/source -v $(pwd):/backup alpine tar -czf /backup/mydata.tar.gz -C /source .

Best Practices

Production Deployments

  • Use specific tags instead of latest for production deployments
  • Implement proper logging strategies using json-file or other logging drivers
  • Use .dockerignore to exclude unnecessary files from builds

Performance

  • Leverage multi-stage builds to minimize final image size
  • Always set resource constraints in production environments
  • Regularly clean up unused resources to maintain system health

Storage and Security

  • Use named volumes for persistent data instead of bind mounts when possible
  • Run containers as non-root users
  • Use security options like --security-opt
  • Regularly scan images for vulnerabilities

Maintenance and Cleanup

System Cleanup

# Remove all unused containers, networks, images and volumes
docker system prune -a --volumes

# Show disk usage
docker system df

# Clean up just containers
docker container prune

# Clean up just images
docker image prune -a

# Clean up just volumes
docker volume prune

Image Management

# Remove images by pattern
docker images | grep "<none>" | awk '{print $3}' | xargs docker rmi

# Save image to tar file
docker save myapp:1.0 > myapp.tar

# Load image from tar file
docker load < myapp.tar

Troubleshooting Commands

# Check container logs
docker logs [options] CONTAINER
  --tail N        # Show last N lines
  --since TIME    # Show logs since timestamp
  -f, --follow    # Follow log output

# Inspect container details
docker inspect CONTAINER

# View container resource usage
docker stats [CONTAINER...]

# View container processes
docker top CONTAINER

References

0 Comments for this cheatsheet. Write yours!