This is the complete Docker reference for 2026. All essential commands in one place.
Versions covered: Docker Engine 29+, Docker Compose v2 (Compose Specification v5).
Container Commands
# Run a container
docker run nginx
# Run in detached mode (background)
docker run -d nginx
# Run interactively with a shell
docker run -it ubuntu:24.04 bash
# Run and remove when stopped
docker run --rm nginx
# Run with a name
docker run --name mywebserver nginx
# Run with port mapping (host:container)
docker run -p 8080:80 nginx
# Run with environment variables
docker run -e APP_ENV=production myapp:1.0
# Run with environment file
docker run --env-file .env myapp:1.0
# Run with volume
docker run -v mydata:/data nginx
# Run with bind mount
docker run -v /host/path:/container/path nginx
# Run with resource limits
docker run --memory 512m --cpus 1.0 myapp:1.0
# Run as non-root user
docker run --user 1001:1001 myapp:1.0
# Run with read-only filesystem
docker run --read-only myapp:1.0
# Run with network
docker run --network mynetwork myapp:1.0
# Run with restart policy
docker run --restart unless-stopped nginx
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
# Stop a container (SIGTERM)
docker stop mywebserver
# Kill a container (SIGKILL)
docker kill mywebserver
# Start a stopped container
docker start mywebserver
# Restart a container
docker restart mywebserver
# Remove a stopped container
docker rm mywebserver
# Force-remove a running container
docker rm -f mywebserver
# Remove all stopped containers
docker container prune
# View container logs
docker logs mywebserver
# Follow logs in real time
docker logs -f mywebserver
# Show last 50 lines
docker logs --tail 50 mywebserver
# Execute a command in a running container
docker exec -it mywebserver bash
# Copy files to/from a container
docker cp mywebserver:/etc/nginx/nginx.conf ./nginx.conf
docker cp ./nginx.conf mywebserver:/etc/nginx/nginx.conf
# Inspect container details (JSON)
docker inspect mywebserver
# View real-time resource usage
docker stats
# View stats for one container
docker stats mywebserver
# View running processes inside a container
docker top mywebserver
Image Commands
# Pull an image from Docker Hub
docker pull nginx
# Pull specific version
docker pull nginx:1.27
# List local images
docker images
# Build an image from Dockerfile
docker build -t myapp:1.0 .
# Build from specific Dockerfile
docker build -f Dockerfile.prod -t myapp:prod .
# Build with build args
docker build --build-arg APP_VERSION=2.0 -t myapp:2.0 .
# Tag an image
docker tag myapp:1.0 myapp:latest
docker tag myapp:1.0 kemalcodes/myapp:1.0
# Push to Docker Hub
docker push kemalcodes/myapp:1.0
# Remove an image
docker rmi nginx
# Remove all unused images
docker image prune
# Remove all unused images (including tagged but not referenced)
docker image prune -a
# View image history (layers)
docker history nginx
# Inspect image details (JSON)
docker inspect nginx
# Save image to tar file
docker save myapp:1.0 -o myapp.tar
# Load image from tar file
docker load -i myapp.tar
Volume Commands
# Create a named volume
docker volume create mydata
# List all volumes
docker volume ls
# Inspect a volume
docker volume inspect mydata
# Remove a volume
docker volume rm mydata
# Remove all unused volumes
docker volume prune
Network Commands
# List all networks
docker network ls
# Create a network
docker network create mynetwork
# Create with specific driver
docker network create --driver bridge mynetwork
# Inspect a network
docker network inspect mynetwork
# Connect a container to a network
docker network connect mynetwork mycontainer
# Disconnect a container from a network
docker network disconnect mynetwork mycontainer
# Remove a network
docker network rm mynetwork
# Remove all unused networks
docker network prune
Docker Compose Commands
Always use docker compose (v2 plugin), not docker-compose (v1, deprecated).
# Start all services (build if needed)
docker compose up
# Start in detached mode
docker compose up -d
# Force rebuild images
docker compose up --build
# Start with watch mode (hot reload)
docker compose watch
docker compose up --watch
# Start with a profile
docker compose --profile debug up
# Stop all services (keep volumes)
docker compose down
# Stop and remove volumes
docker compose down -v
# Stop and remove everything
docker compose down --rmi all -v
# View running services
docker compose ps
# View logs
docker compose logs
# Follow logs
docker compose logs -f
# Follow logs for one service
docker compose logs -f api
# Execute a command in a service
docker compose exec api bash
# Run a one-off command
docker compose run --rm api python manage.py migrate
# Build images only
docker compose build
# Pull latest images
docker compose pull
# Scale a service
docker compose up --scale api=3
# Pause all services
docker compose pause
# Unpause all services
docker compose unpause
# Restart services
docker compose restart
# Validate the Compose file
docker compose config
System Commands
# View Docker system information
docker info
# View Docker version
docker version
# Check disk usage
docker system df
# Remove all unused resources
docker system prune
# Remove everything including volumes
docker system prune --volumes
# View real-time events
docker events
# Log into Docker Hub
docker login
# Log out
docker logout
Useful One-liners
# Stop all running containers
docker stop $(docker ps -q)
# Remove all stopped containers
docker rm $(docker ps -aq)
# Remove all images
docker rmi $(docker images -q)
# Show container IP addresses
docker inspect -f '{{.Name}} {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' \
$(docker ps -q)
# Follow logs from multiple containers
docker compose logs -f api db
# Get a shell in the latest container
docker exec -it $(docker ps -l -q) bash
# Build and immediately run
docker build -t myapp . && docker run --rm myapp
# Copy files from a container to host
docker cp mycontainer:/app/output.log .
docker run Flags Reference
| Flag | Example | Description |
|---|---|---|
-d | -d | Detached mode (background) |
-it | -it | Interactive + allocate TTY |
--rm | --rm | Remove container when stopped |
-p | -p 8080:80 | Port mapping (host:container) |
-v | -v mydata:/data | Volume mount |
--name | --name myapp | Name the container |
-e | -e APP=prod | Set environment variable |
--env-file | --env-file .env | Load env vars from file |
--network | --network mynet | Connect to network |
--restart | --restart unless-stopped | Restart policy |
--memory | --memory 512m | Memory limit |
--cpus | --cpus 1.0 | CPU limit |
--read-only | --read-only | Read-only filesystem |
--user | --user 1001:1001 | Run as user |
--cap-drop | --cap-drop ALL | Drop Linux capabilities |
--security-opt | --security-opt no-new-privileges | Security options |
Dockerfile Instructions Reference
| Instruction | Example | Description |
|---|---|---|
FROM | FROM python:3.13-slim | Base image |
WORKDIR | WORKDIR /app | Set working directory |
COPY | COPY . . | Copy files from host |
ADD | ADD app.tar.gz /app | Copy + extract archives |
RUN | RUN pip install flask | Run command at build time |
ENV | ENV PORT=8080 | Set environment variable |
ARG | ARG BUILD_VERSION=1.0 | Build-time variable |
EXPOSE | EXPOSE 8080 | Document listened port |
CMD | CMD ["python", "app.py"] | Default run command |
ENTRYPOINT | ENTRYPOINT ["python"] | Fixed run command |
USER | USER appuser | Switch to non-root user |
VOLUME | VOLUME /data | Declare a mount point |
HEALTHCHECK | HEALTHCHECK CMD curl -f http://localhost/health | Health check command |
LABEL | LABEL version="1.0" | Add metadata |
ONBUILD | ONBUILD COPY . . | Run when used as base |
Restart Policies
| Policy | Behavior |
|---|---|
no | Never restart (default) |
on-failure | Restart only on non-zero exit |
always | Always restart |
unless-stopped | Restart unless manually stopped |
Multi-stage Build Template
# Build stage
FROM golang:1.24-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o myapp .
# Production stage
FROM scratch
COPY --from=builder /app/myapp /myapp
EXPOSE 8080
CMD ["/myapp"]
Docker Compose Template
# docker-compose.yml
services:
api:
build: .
ports:
- "8080:8080"
environment:
DATABASE_URL: postgresql://alex:${DB_PASSWORD}@db:5432/myapp
depends_on:
db:
condition: service_healthy
db:
image: postgres:17
environment:
POSTGRES_USER: alex
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: myapp
volumes:
- db_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U alex -d myapp"]
interval: 10s
retries: 5
volumes:
db_data:
Related Articles
- Docker Tutorial #1: What is Docker?
- Docker Tutorial #2: Installing Docker
- Docker Tutorial #3: Docker Images and Containers
- Docker Tutorial #4: Dockerfile Deep Dive
- Docker Tutorial #5: Docker Compose
- Docker Tutorial #6: Volumes and Networking
- Docker Tutorial #7: Multi-stage Builds
- Docker Tutorial #8: Docker for Development
- Docker Tutorial #9: Docker Security
- Kubernetes Tutorial #1: What is Kubernetes?