Docker Cheat sheet

ó°ƒ­ 2025-02-10

Images

Building Images

# Build an image from a Dockerfile
docker build -t image_name:tag .

# Build with no cache
docker build --no-cache -t image_name:tag .

# Build with custom Dockerfile
docker build -f custom.dockerfile -t image_name:tag .

Managing Images

# List all images
docker images

# Remove an image
docker rmi image_name:tag

# Remove all unused images
docker image prune

# Pull an image from Docker Hub
docker pull image_name:tag

# Push an image to Docker Hub
docker push image_name:tag

# Tag an image
docker tag source_image:tag target_image:tag

Containers

Running Containers

# Run a container
docker run image_name:tag

# Run with interactive terminal
docker run -it image_name:tag

# Run in detached mode
docker run -d image_name:tag

# Run with port mapping
docker run -p host_port:container_port image_name:tag

# Run with volume mounting
docker run -v host_path:container_path image_name:tag

# Run with environment variables
docker run -e VAR_NAME=value image_name:tag

# Run with custom name
docker run --name container_name image_name:tag

Managing Containers

# List running containers
docker ps

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

# Stop a container
docker stop container_id/name

# Start a stopped container
docker start container_id/name

# Restart a container
docker restart container_id/name

# Remove a container
docker rm container_id/name

# Remove all stopped containers
docker container prune

# Execute command in running container
docker exec -it container_id/name command

Network

Managing Networks

# List networks
docker network ls

# Create a network
docker network create network_name

# Remove a network
docker network rm network_name

# Connect container to network
docker network connect network_name container_id/name

# Disconnect container from network
docker network disconnect network_name container_id/name

Volumes

Managing Volumes

# List volumes
docker volume ls

# Create a volume
docker volume create volume_name

# Remove a volume
docker volume rm volume_name

# Remove all unused volumes
docker volume prune

Docker Compose

Basic Commands

# Start services
docker-compose up

# Start in detached mode
docker-compose up -d

# Stop services
docker-compose down

# Stop and remove volumes
docker-compose down -v

# View service logs
docker-compose logs

# Scale services
docker-compose up -d --scale service_name=num_instances

System & Maintenance

System Commands

# Display system-wide information
docker info

# Show Docker disk usage
docker system df

# Remove unused data
docker system prune

# Remove all unused data (including volumes)
docker system prune -a --volumes

Logging & Debugging

Logs and Inspection

# View container logs
docker logs container_id/name

# Follow container logs
docker logs -f container_id/name

# Inspect container details
docker inspect container_id/name

# View container stats (CPU, memory, etc.)
docker stats container_id/name

Best Practices

  1. Always tag your images with specific versions
  2. Use .dockerignore to exclude unnecessary files
  3. Minimize the number of layers in your Dockerfile
  4. Use multi-stage builds for smaller final images
  5. Don’t run containers as root
  6. Use volumes for persistent data
  7. Clean up unused objects regularly
  8. Use docker-compose for multi-container applications

Common Dockerfile Instructions

FROM        # Base image
WORKDIR     # Set working directory
COPY        # Copy files from host to container
ADD         # Copy files, download URLs, and extract archives
RUN         # Execute commands
ENV         # Set environment variables
EXPOSE      # Expose ports
VOLUME      # Create mount point
CMD         # Default command
ENTRYPOINT  # Configure container executable

Note: Replace container_id/name, image_name:tag, and other placeholders with your actual values when using these commands.