---
title: "Docker"
description: "Docker is a containerization platform for building, shipping, and running applications in isolated environments. This cheatsheet covers essential Docker CLI commands."
author: "Mohammad Abu Mattar"
canonical: https://mkabumattar.com/cheatsheets/docker
---

# Docker

Docker is a containerization platform that packages applications with their dependencies into isolated, portable environments called containers. It enables developers to build, ship, and run applications consistently across different systems.

## Key Benefits

- **Consistency**: Run the same environment everywhere (dev, test, production)
- **Isolation**: Applications are isolated from each other and the host system
- **Portability**: Easily deploy containerized applications to any system with Docker
- **Efficiency**: Lightweight compared to virtual machines, fast startup times
- **Scalability**: Easy to scale applications horizontally by running multiple containers

## Quick Start

**Install Docker**: Follow the installation guide in the Getting Started section.

**Run your first container**: `docker run -d -p 8080:80 nginx:latest`

**Access the service**: Open http://localhost:8080 in your browser.

## Common Workflows

1. **Development**: Use containers to match production environment locally
2. **Building Images**: Create Dockerfiles and build images for your applications
3. **Running Services**: Run containers with proper networking and storage configuration
4. **Scaling**: Use container orchestration (Docker Swarm or Kubernetes) for production deployments

Browse the sections above to learn Docker commands, networking, storage, and best practices for containerized applications.

## Getting Started

Fundamental Docker concepts and basic setup for beginners.

### What is Docker

Introduction to Docker and containerization concepts.

**Keywords:** containerization, images, containers, isolation, docker-daemon

#### Docker overview

```bash
# Docker is a containerization platform that packages applications
# with their dependencies into isolated, portable containers.

# Key concepts:
# - Image: Blueprint/template for creating containers
# - Container: Running instance of an image
# - Registry: Repository for storing images (Docker Hub)
# - Daemon: Background service running Docker
```

_exec_
```bash
docker --version
```

_output_
```bash
Docker version 24.0.0, build abcd1234
```

Docker is a lightweight virtualization platform that uses containerization to isolate applications and their dependencies.

- Containers are more lightweight than virtual machines.
- Docker uses images as templates to create containers.
- All containers run on the same kernel but are isolated from each other.

#### Docker client-server architecture

```bash
# Docker uses a client-server architecture:
# 1. Client: CLI that sends commands to the daemon
# 2. Server: Daemon that manages containers and images
# 3. Registries: Stores images (public or private)

# Communication flow:
# Docker CLI → Docker Daemon → Container Runtime → Containers
```

_exec_
```bash
docker info
```

_output_
```bash
Client:
 Version: 24.0.0
Server:
 Containers: 3
 Running: 1
 Images: 15
```

Explains the client-server architecture of Docker and how commands flow through the system.

- The Docker daemon must be running for CLI commands to work.
- Multiple clients can connect to one daemon.

#### Container vs image

```bash
# Image: Read-only template containing application code and dependencies
# - Similar to a class in object-oriented programming
# - Built from Dockerfile instructions
# - Immutable and portable

# Container: Running instance of an image
# - Similar to an object in object-oriented programming
# - Has writable layer on top of image
# - Each container is isolated with own filesystem, network, processes
```

_exec_
```bash
docker images && docker ps
```

_output_
```bash
REPOSITORY   TAG      IMAGE ID
ubuntu       20.04    1234567

CONTAINER ID  IMAGE   STATUS
abcd1234      ubuntu  Up 2 hours
```

Demonstrates the relationship between images and containers with concrete examples.

- One image can create multiple containers.
- Containers are ephemeral and data is lost when stopped (unless using volumes).

**Best practices:**

- Always use specific image versions/tags instead of 'latest'.
- Understand the difference between images and containers before proceeding.

**Common errors:**

- **Docker daemon not running**: Start Docker daemon with `systemctl start docker` or Docker Desktop.

### Installation Setup

Installing Docker and verifying the installation.

**Keywords:** install, setup, docker-desktop, verify

#### Install Docker on Linux

```bash
# Update package manager
sudo apt-get update

# Install Docker package
sudo apt-get install -y docker.io

# Start Docker daemon
sudo systemctl start docker

# Enable Docker to start on boot
sudo systemctl enable docker
```

_exec_
```bash
sudo docker --version
```

_output_
```bash
Docker version 24.0.0, build abcd1234
```

Installation steps for Docker on Ubuntu/Debian-based Linux systems.

- Requires Ubuntu 16.04 LTS or newer.
- Using `sudo` is necessary for Docker commands unless configured otherwise.

#### Post-installation setup

```bash
# Run Docker without sudo (optional)
sudo usermod -aG docker $USER

# Apply group membership
newgrp docker

# Verify installation
docker run hello-world
```

_exec_
```bash
docker run hello-world
```

_output_
```bash
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
```

Post-installation configuration to run Docker without sudo and verify the setup.

- Adding user to docker group allows running without sudo.
- Must logout and login again for group changes to take effect.

#### Docker Desktop installation (macOS/Windows)

```bash
# macOS: Install using Homebrew
brew install --cask docker

# Or download from: https://www.docker.com/products/docker-desktop

# Windows: Download Docker Desktop installer from official site
# Then run installer and enable WSL 2 integration

# After installation, verify
docker --version
```

_exec_
```bash
docker --version && docker run hello-world
```

_output_
```bash
Docker version 24.0.0, build abcd1234
Hello from Docker!
```

Installation instructions for Docker Desktop on macOS and Windows systems.

- Docker Desktop includes Docker engine, CLI, and Docker Compose.
- Windows requires WSL 2 (Windows Subsystem for Linux 2) for optimal performance.

**Best practices:**

- Enable WSL 2 on Windows for better performance.
- Run hello-world to verify successful installation.

**Common errors:**

- **Permission denied while trying to connect to Docker daemon**: Add user to docker group or use sudo with Docker commands.

### Hello World

Running your first Docker container.

**Keywords:** hello-world, run, first-container, basic-usage

#### Run hello-world image

```bash
docker run hello-world
```

_exec_
```bash
docker run hello-world
```

_output_
```bash
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.
```

Demonstrates the basic workflow of pulling an image and running a container.

- Docker downloads the image from Docker Hub if it's not already present.
- The container runs and exits automatically for hello-world.

#### Run interactive shell container

```bash
docker run -it ubuntu:22.04 /bin/bash
```

_exec_
```bash
docker run -it ubuntu:22.04 /bin/bash
```

_input_
```bash
ls -la
exit
```

_output_
```bash
total 32
drwxr-xr-x   1 root root 4096 Jan 10 12:00 .
drwxr-xr-x   1 root root 4096 Jan 10 12:00 ..
-rwxr-xr-x   1 root root    0 Jan 10 12:00 .dockerenv
...
```

Runs an interactive shell in an Ubuntu container, allowing you to explore the container filesystem.

- `-it` flags enable interactive terminal access.
- Type `exit` to leave the container.

#### Run container with custom command

```bash
docker run --name my-python-app python:3.11 python --version
```

_exec_
```bash
docker run --name my-python-app python:3.11 python --version
```

_output_
```bash
Python 3.11.5
```

Runs a specific command in the container and then exits.

- `--name` assigns a friendly name to the container.
- Container exits after command completes.

**Best practices:**

- Always use specific image versions instead of just 'latest'.
- Use meaningful container names for easy identification.

**Common errors:**

- **Image not found locally**: Docker will automatically pull from Docker Hub; ensure you have internet connection.

## Build & Images

Creating, managing, and working with Docker images.

### Docker Build

Creating Docker images from Dockerfiles.

**Keywords:** build, dockerfile, image-creation, layers, tags

#### Basic Docker build

```bash
# Create a Dockerfile
cat > Dockerfile << 'EOF'
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y curl
COPY . /app
WORKDIR /app
CMD ["echo", "Hello from Docker!"]
EOF

# Build the image
docker build -t my-app:1.0 .
```

_exec_
```bash
docker build -t my-app:1.0 .
```

_output_
```bash
Sending build context to Docker daemon  2.048kB
Step 1/5 : FROM ubuntu:22.04
Step 2/5 : RUN apt-get update && apt-get install -y curl
Step 3/5 : COPY . /app
Step 4/5 : WORKDIR /app
Step 5/5 : CMD ["echo", "Hello from Docker!"]
Successfully built abc123def456
Successfully tagged my-app:1.0
```

Demonstrates basic Docker image building with tag naming.

- Build context includes all files in current directory.
- Tag format is `repository:tag` or `registry/repository:tag`.
- Each RUN instruction creates a layer.

#### Build with build arguments

```bash
# Dockerfile with build arguments
cat > Dockerfile << 'EOF'
FROM python:3.11
ARG APP_VERSION=1.0
ARG APP_ENV=production
RUN echo "Building version $APP_VERSION for $APP_ENV"
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
EOF

# Build with custom arguments
docker build \
  --build-arg APP_VERSION=2.5 \
  --build-arg APP_ENV=staging \
  -t my-python-app:2.5 .
```

_exec_
```bash
docker build --build-arg APP_VERSION=2.5 -t my-python-app:2.5 .
```

_output_
```bash
Sending build context to Docker daemon
Step 1/7 : FROM python:3.11
Step 2/7 : ARG APP_VERSION=1.0
Step 3/7 : ARG APP_ENV=production
Step 4/7 : RUN echo "Building version 2.5 for staging"
Building version 2.5 for staging
Successfully tagged my-python-app:2.5
```

Demonstrates build-time arguments for customizing image builds.

- ARG values can be overridden during build time.
- Build arguments are only available during build, not in running containers.

#### Multi-stage Docker build

```bash
cat > Dockerfile << 'EOF'
# Build stage
FROM golang:1.21 as builder
WORKDIR /app
COPY . .
RUN go build -o myapp

# Runtime stage
FROM alpine:latest
WORKDIR /root/
COPY --from=builder /app/myapp .
CMD ["./myapp"]
EOF

docker build -t go-optimized:latest .
```

_exec_
```bash
docker build -t go-optimized:latest .
```

_output_
```bash
Step 1/8 : FROM golang:1.21 as builder
Step 2/8 : WORKDIR /app
Step 3/8 : COPY . .
Step 4/8 : RUN go build -o myapp
Step 5/8 : FROM alpine:latest
Step 6/8 : WORKDIR /root/
Step 7/8 : COPY --from=builder /app/myapp .
Step 8/8 : CMD ["./myapp"]
Successfully tagged go-optimized:latest
```

Multi-stage builds reduce final image size by using temporary build stages.

- Only final stage is included in the image.
- Significantly reduces image size for compiled applications.

**Best practices:**

- Use specific base image versions.
- Minimize layers by combining RUN commands.
- Use multi-stage builds for smaller production images.

**Common errors:**

- **COPY/ADD path outside build context**: Ensure files to copy are within the build context directory.

### Managing Images

Listing, inspecting, and managing Docker images.

**Keywords:** images, list, inspect, tags, history

#### List all images

```bash
# List all local images
docker images

# List with all details including dangling images
docker images -a
```

_exec_
```bash
docker images
```

_output_
```bash
REPOSITORY          TAG      IMAGE ID       CREATED       SIZE
ubuntu              22.04    6b038c8a0437   2 weeks ago   77.9MB
python              3.11     abc123def456   2 weeks ago   920MB
nginx               latest   def456abc123   1 week ago    187MB
my-app              1.0      xyz789abc123   3 days ago    250MB
```

Displays all Docker images and their metadata.

- IMAGE ID is the unique identifier for the image.
- SIZE is the uncompressed size of the image.

#### Inspect image details

```bash
# Get detailed information about an image
docker inspect ubuntu:22.04

# Get specific information using jq
docker inspect ubuntu:22.04 | jq '.[0].Config.Env'
```

_exec_
```bash
docker inspect ubuntu:22.04
```

_output_
```bash
[
  {
    "Id": "sha256:6b038c8a043...",
    "RepoTags": ["ubuntu:22.04"],
    "Size": 77880000,
    "Architecture": "amd64",
    "Config": {
      "Hostname": "",
      "Env": ["PATH=/usr/local/sbin:/usr/local/bin:..."]
    }
  }
]
```

Shows detailed JSON metadata about a specific image.

- Output is in JSON format.
- Includes environment variables, exposed ports, and more.

#### View image history

```bash
# Show image build history (layers)
docker history my-app:1.0

# Show human-readable format
docker history --human my-app:1.0
```

_exec_
```bash
docker history my-app:1.0
```

_output_
```bash
IMAGE          CREATED       CREATED BY                      SIZE
abc123def456   3 hours ago   /bin/sh -c #(nop)  CMD ["pytho   0B
def456abc123   3 hours ago   /bin/sh -c pip install -r req   45MB
ghi789def012   3 hours ago   /bin/sh -c #(nop) COPY dir:...  2.5MB
python:3.11    2 weeks ago   /bin/sh -c #(nop)  CMD ["pyth   0B
```

Displays the build history showing each layer and command.

- Each row represents a layer in the image.
- Helps understand what commands created each layer.

**Best practices:**

- Use docker images -q to get only image IDs for scripting.
- Tag images meaningfully with version numbers.

**Common errors:**

- **Image with tag not found**: Check spelling and ensure image exists locally or on registry.

### Remove Images

Deleting Docker images and managing disk space.

**Keywords:** remove, rmi, delete, cleanup, dangling

#### Remove single image by ID

```bash
# Remove image by ID
docker rmi abc123def456

# Force remove even if in use
docker rmi -f abc123def456
```

_exec_
```bash
docker rmi abc123def456
```

_output_
```bash
Untagged: my-app:1.0
Deleted: sha256:abc123def456...
Deleted: sha256:def456abc123...
```

Removes a Docker image by its ID.

- Cannot remove image if running containers use it.
- Use `-f` flag to force remove, but may orphan containers.

#### Remove image by repository name

```bash
# Remove specific image tag
docker rmi ubuntu:22.04

# Remove all tags of repository
docker rmi ubuntu
```

_exec_
```bash
docker rmi python:3.11
```

_output_
```bash
Untagged: python:3.11
Deleted: sha256:abc123def456...
Deleted: sha256:def456abc123...
```

Removes Docker images by repository and tag name.

- Remove containers first before removing images they use.

#### Remove dangling images

```bash
# Find dangling images (untagged, unused layers)
docker images -f dangling=true

# Remove all dangling images
docker image prune -a

# Remove with confirmation
docker image prune -a --force
```

_exec_
```bash
docker image prune -a
```

_output_
```bash
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: old-app:1.0@sha256:...
total reclaimed space: 245.3MB
```

Cleans up unused and dangling images to free disk space.

- Dangling images have no repository or tag reference.
- This operation is irreversible.

**Best practices:**

- Remove unused images regularly to save disk space.
- Always stop and remove containers before removing images.

**Common errors:**

- **Image is in use by running container**: Stop and remove the container first using `docker stop` and `docker rm`.

## Run & Containers

Creating and running Docker containers with various options.

### Docker Run

Creating and starting containers with the run command.

**Keywords:** run, container-creation, options, ports, volumes, environment

#### Basic container run

```bash
# Run container and keep it running
docker run -d --name my-web nginx:latest

# Run with interactive terminal
docker run -it ubuntu:22.04 /bin/bash

# Run and remove after exit
docker run --rm python:3.11 python --version
```

_exec_
```bash
docker run -d --name my-web nginx:latest
```

_output_
```bash
1a2b3c4d5e6f7g8h9i0j (container ID)
```

Demonstrates basic run options for different scenarios.

- `-d` runs container in detached (background) mode.
- `-it` allows interactive terminal access.
- `--rm` automatically removes container when it exits.

#### Run with port mapping and volumes

```bash
# Map container port to host port
docker run -d -p 8080:80 --name my-web \
  -v /var/www/html:/usr/share/nginx/html \
  nginx:latest

# Multiple port mappings
docker run -d -p 80:80 -p 443:443 \
  --name my-secure-web nginx:latest
```

_exec_
```bash
docker run -d -p 8080:80 --name my-web nginx:latest
```

_output_
```bash
abc123def456ghi789jkl
```

Maps container ports to host and mounts volumes.

- Format is `-p host_port:container_port`.
- `-v` mounts host directory into container.
- Must use absolute paths for volume mounting.

#### Run with environment variables and resource limits

```bash
# Set environment variables
docker run -d --name my-app \
  -e DATABASE_URL=postgres://db:5432 \
  -e APP_ENV=production \
  -e LOG_LEVEL=info \
  my-app:1.0

# Set resource limits
docker run -d --name my-limited-app \
  -m 512m \
  --cpus="0.5" \
  my-app:1.0
```

_exec_
```bash
docker run -d -e APP_ENV=production my-app:1.0
```

_output_
```bash
xyz123abc456def789ghi
```

Demonstrates environment variables and resource constraints.

- `-e` sets environment variables in the container.
- `-m` limits memory in MB or GB.
- `--cpus` limits CPU resources.

**Best practices:**

- Always use specific image versions, not 'latest'.
- Mount volumes for persistent data.
- Set resource limits to prevent system overload.

**Common errors:**

- **Port is already allocated**: Use a different host port or stop the container using that port.

### Docker Create

Creating containers without starting them immediately.

**Keywords:** create, container-creation, prepare, configuration

#### Create container configuration

```bash
# Create container without starting
docker create --name my-db \
  -e MYSQL_ROOT_PASSWORD=secret \
  -p 3306:3306 \
  -v db-data:/var/lib/mysql \
  mysql:8.0

# Verify it's created but not running
docker ps -a
```

_exec_
```bash
docker create --name my-db mysql:8.0
```

_output_
```bash
abc123def456ghi789jkl (container ID)
```

Separates container creation from startup for more control.

- Container is created but in stopped state.
- Useful for configuring containers before starting.

#### Create with advanced networking

```bash
# Create network
docker network create my-network

# Create container on specific network
docker create --name web-server \
  --network my-network \
  --network-alias web \
  -p 80:80 \
  nginx:latest

# Create another container on same network
docker create --name app-server \
  --network my-network \
  my-app:1.0
```

_exec_
```bash
docker create --network my-network --name web-server nginx:latest
```

_output_
```bash
def456ghi789jkl123abc
```

Creates containers on specific networks for container-to-container communication.

- Containers on same network can communicate using container name.
- Network aliases provide DNS resolution within the network.

#### Create with health check

```bash
# Create with health check
docker create --name healthy-app \
  --health-cmd="curl -f http://localhost:8080/health || exit 1" \
  --health-interval=30s \
  --health-timeout=10s \
  --health-retries=3 \
  my-app:1.0
```

_exec_
```bash
docker create --health-cmd="curl localhost:8080" my-app:1.0
```

_output_
```bash
ghi789jkl123abc456def
```

Creates container with health check for monitoring availability.

- Health check runs at specified intervals.
- Docker marks container as unhealthy after retries fail.

**Best practices:**

- Use create when you need to configure before starting.
- Add health checks for critical services.

**Common errors:**

- **Container name already exists**: Use unique name or remove existing container with `docker rm`.

### Docker Exec

Running commands inside running containers.

**Keywords:** exec, running-command, interactive, debugging, inspection

#### Execute command in running container

```bash
# Execute single command
docker exec my-web ls -la /var/www/html

# Execute with output
docker exec my-db mysql -u root -p$MYSQL_ROOT_PASSWORD -e "SHOW DATABASES;"
```

_exec_
```bash
docker exec my-web ls -la /var/www/html
```

_output_
```bash
total 24
drwxr-xr-x   1 root root 4096 Jan 10 12:00 .
drwxr-xr-x   1 root root 4096 Jan 10 12:00 ..
-rw-r--r--   1 root root 1234 Jan 10 12:00 index.html
```

Executes a command inside a running container without entering shell.

- Container must be in running state.
- Useful for quick debugging and inspection.

#### Interactive shell access

```bash
# Enter interactive shell
docker exec -it my-app /bin/bash

# Once inside, run commands
# $ ps aux
# $ cat /var/log/app.log
# $ exit
```

_exec_
```bash
docker exec -it my-app /bin/bash
```

_input_
```bash
id
pwd
```

_output_
```bash
uid=0(root) gid=0(root) groups=0(root)
/app
```

Provides interactive shell access for debugging and exploration.

- `-it` enables interactive terminal mode.
- Type `exit` to leave the shell.

#### Execute with user and working directory

```bash
# Execute as specific user
docker exec -u appuser my-app whoami

# Execute in specific directory
docker exec -w /var/log my-app tail -f app.log

# Execute in background
docker exec -d my-app python script.py
```

_exec_
```bash
docker exec -u appuser my-app whoami
```

_output_
```bash
appuser
```

Demonstrates user, directory, and background execution options.

- `-u` specifies user for command execution.
- `-w` sets working directory for command.
- `-d` runs command in background.

**Best practices:**

- Use exec for quick one-off commands and debugging.
- Always verify container is running before exec.

**Common errors:**

- **Container is not running**: Start container with `docker start container-name` first.

## Container Management

Managing container lifecycle, status, and logs.

### Start & Stop Containers

Starting, stopping, and restarting containers.

**Keywords:** start, stop, restart, pause, unpause

#### Start and stop containers

```bash
# Stop a running container gracefully
docker stop my-web

# Start a stopped container
docker start my-web

# Restart a container (stop then start)
docker restart my-web
```

_exec_
```bash
docker stop my-web && docker start my-web
```

_output_
```bash
my-web
my-web
```

Basic container lifecycle operations.

- `stop` sends SIGTERM signal, gives 10 seconds to shutdown gracefully.
- `start` restarts stopped container with same configuration.

#### Stop with timeout and force kill

```bash
# Stop with custom timeout before killing
docker stop -t 30 my-web

# Force kill container immediately
docker kill my-web

# Kill all running containers
docker kill $(docker ps -q)
```

_exec_
```bash
docker stop -t 30 my-web
```

_output_
```bash
my-web
```

Advanced stop options including timeout and force kill.

- `-t` specifies seconds to wait before killing (default 10).
- `kill` sends SIGKILL immediately, no graceful shutdown.

#### Pause and unpause containers

```bash
# Pause all processes in container
docker pause my-app

# Resume paused container
docker unpause my-app

# Check pause status
docker inspect -f '{{.State.Paused}}' my-app
```

_exec_
```bash
docker pause my-app && docker unpause my-app
```

_output_
```bash
my-app
my-app
```

Temporarily pause container processes without stopping container.

- Paused containers still consume memory.
- Useful for resource management without full stop.

**Best practices:**

- Use `stop` for graceful shutdown of services.
- Use `kill` only when necessary.

**Common errors:**

- **Container no response to stop signal**: Use `docker kill` to force termination.

### List Containers

Listing and filtering containers.

**Keywords:** ps, list, filter, status, format

#### List running containers

```bash
# List running containers
docker ps

# List all containers (running and stopped)
docker ps -a

# List only container IDs
docker ps -q
```

_exec_
```bash
docker ps
```

_output_
```bash
CONTAINER ID   IMAGE       COMMAND   CREATED      STATUS      PORTS
abc123def456   nginx:latest "nginx..." 2 hours ago  Up 2 hours  0.0.0.0:8080->80/tcp
def456ghi789   mysql:8.0   "docker..." 1 hour ago   Up 1 hour   3306/tcp
```

Lists Docker containers with various output options.

- Default `ps` shows only running containers.
- `-a` includes stopped containers.
- `-q` shows only IDs for scripting.

#### Filter containers by status and labels

```bash
# Filter by status
docker ps -a -f status=exited
docker ps -a -f status=running

# Filter by label
docker ps -f label=environment=production

# Filter by image
docker ps -f ancestor=nginx:latest
```

_exec_
```bash
docker ps -a -f status=exited
```

_output_
```bash
CONTAINER ID   IMAGE        STATUS
xyz123abc456   ubuntu:20.04 Exited (0)
ghi789jkl012   python:3.11  Exited (1)
```

Advanced filtering options for container queries.

- Multiple filters can be combined with `-f`.
- Labels must be set when creating containers.

#### Custom output formatting

```bash
# Show custom columns
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"

# JSON format
docker ps --format json

# Simple list of names
docker ps --no-trunc --format "{{.Names}}"
```

_exec_
```bash
docker ps --format "table {{.Names}}\t{{.Status}}"
```

_output_
```bash
NAMES           STATUS
my-web          Up 2 hours
my-db           Up 1 hour
```

Custom formatting options for better readability.

- `--format` accepts template variables.
- Useful for scripting and automation.

**Best practices:**

- Use `docker ps -q` in scripts to get only IDs.
- Filter containers for easier management in large deployments.

**Common errors:**

- **Container not found**: Run `docker ps -a` to check if container exists.

### Docker Logs

Viewing and monitoring container logs.

**Keywords:** logs, output, monitoring, troubleshooting, tail

#### View container logs

```bash
# View all logs
docker logs my-web

# View last 50 lines
docker logs --tail 50 my-web

# View logs with timestamps
docker logs -t my-web
```

_exec_
```bash
docker logs my-web
```

_output_
```bash
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to start nginx
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
2025-01-10T12:00:00.123Z [notice] starting server...
2025-01-10T12:00:00.456Z [notice] listen() to 0.0.0.0:80
```

Displays container output and logs.

- Logs are from container's stdout and stderr.
- Can view logs even after container stops.

#### Follow logs in real-time

```bash
# Stream logs in real-time (like tail -f)
docker logs -f my-web

# Stream with timestamps
docker logs -f -t my-web

# Stream last 10 lines with follow
docker logs --tail 10 -f my-web
# Press Ctrl+C to exit
```

_exec_
```bash
docker logs -f my-web
```

_output_
```bash
2025-01-10T12:00:05.123Z GET / HTTP/1.1 200
2025-01-10T12:00:06.456Z GET /api/users HTTP/1.1 200
2025-01-10T12:00:07.789Z GET /style.css HTTP/1.1 304
(continue streaming...)
```

Real-time log streaming for monitoring container activity.

- `-f` follows log output continuously.
- Press Ctrl+C to stop streaming.

#### Filter and search logs

```bash
# Show logs since specific time
docker logs --since 2025-01-10T12:00:00 my-web

# Show logs from last hour
docker logs --since 1h my-web

# Search for specific patterns
docker logs my-web | grep ERROR
docker logs my-web | grep -i exception
```

_exec_
```bash
docker logs my-web | grep ERROR
```

_output_
```bash
2025-01-10T12:00:15.123Z [ERROR] Connection timeout
2025-01-10T12:00:30.456Z [ERROR] Database unavailable
```

Filters and searches logs for specific patterns.

- `--since` accepts timestamps or duration.
- Combine with grep for sophisticated searches.

**Best practices:**

- Use `-f` for real-time monitoring.
- Redirect logs to files for analysis.
- Set appropriate logging drivers for production.

**Common errors:**

- **No logs for container**: Container may have exited; check with `docker ps -a`.

## Networking & Volumes

Container networking, port mapping, and persistent storage.

### Port Mapping & Networking

Managing ports, networks, and container communication.

**Keywords:** ports, networking, linking, dns, expose

#### Port mapping

```bash
# Map single port
docker run -d -p 8080:80 --name my-web nginx:latest

# Map multiple ports
docker run -d -p 80:80 -p 443:443 -p 3000:3000 my-app:1.0

# Map to specific host interface
docker run -d -p 127.0.0.1:8080:80 my-web

# Dynamic port mapping (OS assigns port)
docker run -d -P nginx:latest
```

_exec_
```bash
docker run -d -p 8080:80 nginx:latest
```

_output_
```bash
abc123def456ghi789jkl
```

Maps container ports to host for external access.

- Format: `-p host_port:container_port`.
- `-P` auto-assigns high ports above 32768.
- Access locally: `http://localhost:8080`.

#### Docker networks and container linking

```bash
# Create custom bridge network
docker network create my-app-network

# Run containers on network
docker run -d --name web \
  --network my-app-network \
  -p 8080:80 \
  nginx:latest

docker run -d --name db \
  --network my-app-network \
  -e MYSQL_ROOT_PASSWORD=secret \
  mysql:8.0

# Containers can communicate using names
docker exec web curl http://db:3306
```

_exec_
```bash
docker network create my-app-network
```

_output_
```bash
abc123def456ghi789jklmnopq
```

Creates custom networks for container-to-container communication.

- Containers on same network can communicate using names.
- Better than deprecated `--link` option.

#### Inspect and manage networks

```bash
# List networks
docker network ls

# Inspect network details
docker network inspect my-app-network

# Connect container to network
docker network connect my-app-network container-name

# Disconnect from network
docker network disconnect my-app-network container-name
```

_exec_
```bash
docker network ls
```

_output_
```bash
NETWORK ID     NAME             DRIVER    SCOPE
abc123def456   bridge           bridge    local
def456ghi789   host             host      local
ghi789jkl012   none             null      local
jkl012mno345   my-app-network   bridge    local
```

Manages and inspects Docker networks.

- bridge: Default, isolated network.
- host: Uses host network.
- none: No networking.

**Best practices:**

- Use custom networks instead of --link.
- Keep services on same network only if needed.

**Common errors:**

- **Port already in use**: Use different host port or stop container using it.

### Docker Volumes

Volume management for persistent data storage.

**Keywords:** volumes, storage, persistent, data, mount

#### Create and manage volumes

```bash
# Create named volume
docker volume create db-data

# List volumes
docker volume ls

# Inspect volume
docker volume inspect db-data

# Remove volume
docker volume rm db-data
```

_exec_
```bash
docker volume create db-data
```

_output_
```bash
db-data
```

Creates and manages named volumes for persistent storage.

- Named volumes are stored on host at `/var/lib/docker/volumes/`.
- Volumes persist even when containers are removed.

#### Mount volumes in containers

```bash
# Mount named volume
docker run -d -v db-data:/var/lib/mysql \
  --name my-db mysql:8.0

# Bind mount from host directory
docker run -d -v /data/app:/app \
  --name my-app my-app:1.0

# Read-only mount
docker run -d -v db-data:/data:ro \
  --name web-app my-web:1.0
```

_exec_
```bash
docker run -d -v db-data:/var/lib/mysql mysql:8.0
```

_output_
```bash
xyz123abc456def789ghi
```

Mounts volumes and bind mounts in containers.

- Format: `-v volume_name:/container_path`.
- Use absolute paths for bind mounts.
- Add `:ro` for read-only mount.

#### Share data between containers

```bash
# Create shared volume
docker volume create shared-data

# Container 1 writes to volume
docker run -d -v shared-data:/data \
  --name writer my-writer:1.0

# Container 2 reads from same volume
docker run -d -v shared-data:/shared \
  --name reader my-reader:1.0

# Verify data is shared
docker exec writer sh -c "echo 'shared data' > /data/file.txt"
docker exec reader cat /shared/file.txt
```

_exec_
```bash
docker volume create shared-data
```

_output_
```bash
shared-data
```

Uses volumes to share data between multiple containers.

- Multiple containers can mount same volume.
- Useful for shared configuration and data.

**Best practices:**

- Use named volumes for managed data storage.
- Use bind mounts for development environments.
- Always backup important volumes.

**Common errors:**

- **Volume already exists**: Use existing volume or remove with `docker volume rm`.

## Cleanup & System

Removing containers, cleaning unused resources, and system management.

### Container & Image Cleanup

Removing containers and managing disk space.

**Keywords:** cleanup, remove, prune, dangling, disk-space

#### Remove containers

```bash
# Remove stopped container
docker rm container-name

# Remove running container (force)
docker rm -f container-name

# Remove multiple containers
docker rm container1 container2 container3

# Remove all stopped containers
docker container prune
```

_exec_
```bash
docker rm stopped-container
```

_output_
```bash
stopped-container
```

Removes Docker containers to free resources.

- Use `-f` to force remove running containers.
- Cannot remove without -f if container is running.

#### Prune system resources

```bash
# Remove dangling images, containers, volumes, networks
docker system prune

# Also remove unused images (not just dangling)
docker system prune -a

# Include volumes in cleanup
docker system prune -a --volumes
```

_exec_
```bash
docker system prune -a
```

_output_
```bash
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all dangling images
  - all build cache

Total reclaimed space: 2.5GB
```

Comprehensive cleanup of unused Docker resources.

- `prune` is irreversible; review what will be removed.
- Use `-a` to remove all unused images.

#### Cleanup specific resources

```bash
# Remove dangling images only
docker image prune

# Remove unused volumes
docker volume prune

# Stop all containers and remove them
docker stop $(docker ps -q)
docker rm $(docker ps -a -q)

# Remove images matching pattern
docker rmi $(docker images | grep 'old-app' | awk '{print $3}')
```

_exec_
```bash
docker image prune
```

_output_
```bash
WARNING! This will remove all dangling images.
Total reclaimed space: 512MB
```

Targeted cleanup of specific resource types.

- Can be more selective than system prune.
- Useful for specific cleanup scenarios.

**Best practices:**

- Regularly clean up unused resources.
- Use `docker system df` to check usage before cleanup.
- Create cleanup scripts for automation.

**Common errors:**

- **Container has dependent child images**: Remove containers first, then images.

### System Management

Monitoring Docker system health and resource usage.

**Keywords:** system, monitoring, statistics, resource-usage, events

#### Monitor Docker disk usage

```bash
# Show Docker disk usage
docker system df

# Show detailed info
docker system df -v

# Show system information
docker system info
```

_exec_
```bash
docker system df
```

_output_
```bash
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          15        3         2.5GB     1.8GB
Containers      8         2         256MB     200MB
Local Volumes   12        4         512MB     100MB
```

Shows Docker's resource consumption and disk usage.

- TOTAL: Total count of resources.
- ACTIVE: Currently in use.
- RECLAIMABLE: Space that could be freed.

#### Monitor container statistics

```bash
# Show container resource usage
docker stats

# Show stats for specific containers
docker stats my-web my-db

# Show without streaming (one snapshot)
docker stats --no-stream
```

_exec_
```bash
docker stats --no-stream
```

_output_
```bash
CONTAINER   CPU %    MEM USAGE    MEM %    NET I/O
my-web      0.25%    45MB         5%       12MB/8MB
my-db       2.15%    185MB        18%      150MB/120MB
```

Shows real-time resource usage for running containers.

- CPU %, Memory, Network I/O statistics.
- Useful for identifying resource hogs.

#### Get system events

```bash
# Show system events in real-time
docker system events

# Filter events for containers
docker system events --filter type=container

# Filter for specific container
docker system events --filter container=my-web
```

_exec_
```bash
docker system events --filter type=container
```

_output_
```bash
2025-01-10T12:00:15.123Z container create abc123def456
2025-01-10T12:00:16.456Z container start abc123def456
2025-01-10T12:00:45.789Z container stop abc123def456
```

Monitors Docker system events in real-time.

- Events include create, start, stop, delete, etc.
- Filter by type (container, image, volume, etc.).

**Best practices:**

- Monitor docker stats regularly for resource issues.
- Use docker system df to prevent disk space issues.
- Set resource limits on containers.

**Common errors:**

- **Docker using excessive disk space**: Run `docker system prune` to clean up unused resources.
