Docker CLI Commands Reference¶
Complete reference for Docker command-line interface. Use this for quick lookup of syntax and options.
Container Management¶
docker run¶
Create and start a container.
| Option | Description | Example |
|---|---|---|
-d | Detached mode (background) | docker run -d nginx |
-it | Interactive terminal | docker run -it ubuntu bash |
--name | Container name | docker run --name myapp node |
-p HOST:CONTAINER | Port mapping | docker run -p 8080:3000 app |
-e KEY=VALUE | Environment variable | docker run -e DB_HOST=localhost |
-v HOST:CONTAINER | Volume mount | docker run -v /data:/app/data |
--rm | Auto-remove on exit | docker run --rm python script.py |
--network | Connect to network | docker run --network mynet nginx |
--link | Link to another container | docker run --link db:database app |
-m | Memory limit | docker run -m 512m app |
--cpus | CPU limit | docker run --cpus 1.5 app |
Common Examples:
# Run interactive shell in Ubuntu
docker run -it ubuntu:22.04 bash
# Run detached Flask app on port 5000
docker run -d -p 5000:5000 -e FLASK_ENV=production myflask:1.0
# Run with volume mount
docker run -d -v /home/data:/app/data -p 8080:8080 myapp
# Run with multiple environment variables
docker run -d \
-e DB_HOST=postgres \
-e DB_USER=admin \
-e DB_PASS=secret \
-p 3000:3000 \
myapp:latest
# Run with resource limits
docker run -d \
-m 1g \
--cpus 2 \
--memory-swap 2g \
-p 8000:8000 \
resource-heavy-app
docker ps¶
List running containers.
| Option | Description |
|---|---|
-a | Show all containers (including stopped) |
-q | Only show container IDs |
--filter | Filter results |
--format | Custom output format |
-s | Show container size |
Examples:
# List running containers
docker ps
# List all containers
docker ps -a
# Show container sizes
docker ps -s
# List only container IDs
docker ps -q
# Filter by status
docker ps -a --filter "status=exited"
# Custom format
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"
# Find containers by name
docker ps --filter "name=myapp"
docker start / stop / restart¶
Control container lifecycle.
docker start CONTAINER # Start stopped container
docker stop CONTAINER # Stop running container
docker restart CONTAINER # Restart container
docker kill CONTAINER # Force stop (SIGKILL)
docker pause CONTAINER # Pause container
docker unpause CONTAINER # Resume container
Examples:
# Start stopped container
docker start old-container-123
# Stop gracefully (15 second timeout)
docker stop myapp
# Force stop immediately
docker kill runaway-process
# Restart container
docker restart myapp
# Pause long operation
docker pause backup-job
docker rm / docker rmi¶
Remove containers and images.
docker rm CONTAINER # Remove stopped container
docker rm -f CONTAINER # Force remove (even if running)
docker rmi IMAGE # Remove image
Examples:
# Remove stopped container
docker rm container-123
# Remove all stopped containers
docker rm $(docker ps -aq)
# Remove running container forcefully
docker rm -f myapp
# Remove image
docker rmi myapp:1.0
# Remove dangling images (unused)
docker image prune
Inspection & Debugging¶
docker logs¶
View container output/logs.
| Option | Description | Example |
|---|---|---|
-f | Follow logs (tail -f) | docker logs -f myapp |
--tail N | Last N lines | docker logs --tail 100 myapp |
-t | Show timestamps | docker logs -t myapp |
--since | Logs since time | docker logs --since 10m myapp |
--until | Logs until time | docker logs --until 1h myapp |
Examples:
# View all logs
docker logs myapp
# Follow logs in real-time
docker logs -f myapp
# Last 50 lines with timestamps
docker logs -t --tail 50 myapp
# Logs from last 10 minutes
docker logs --since 10m myapp
# Logs from last 2 hours
docker logs --until 2h myapp
# Live tail with filtering
docker logs -f myapp | grep ERROR
docker exec¶
Execute command in running container.
| Option | Description | Example |
|---|---|---|
-it | Interactive terminal | docker exec -it myapp bash |
-u | Run as user | docker exec -u root myapp |
-w | Working directory | docker exec -w /app myapp |
-e | Environment variable | docker exec -e VAR=value myapp |
Examples:
# Interactive shell in container
docker exec -it myapp bash
# Run Python command
docker exec myapp python -c "print('hello')"
# Check database connection
docker exec myapp mysql -h db -u root -p password
# Restart application service
docker exec -u root myapp systemctl restart app
# Check file in container
docker exec myapp cat /etc/config.conf
# Run with env var
docker exec -e NODE_ENV=production myapp npm run build
docker inspect¶
View detailed container/image information.
Examples:
# Inspect container
docker inspect myapp
# Get IP address
docker inspect -f '{{.NetworkSettings.IPAddress}}' myapp
# Get environment variables
docker inspect -f '{{json .Config.Env}}' myapp | jq
# Get mounted volumes
docker inspect -f '{{json .Mounts}}' myapp | jq
# Check if container is running
docker inspect -f '{{.State.Running}}' myapp
docker stats¶
Monitor container resource usage.
Examples:
# Real-time stats for all containers
docker stats
# Stats for specific container
docker stats myapp
# Stats without streaming
docker stats --no-stream
# Show memory in GB
docker stats --format "table {{.Container}}\t{{.MemUsage}}"
docker top¶
Show running processes in container.
Example:
# Show processes
docker top myapp
# Similar to 'ps aux' inside container
# Returns PID, USER, CPU %, MEM %, COMMAND
Image Management¶
docker build¶
Build image from Dockerfile.
| Option | Description | Example |
|---|---|---|
-t | Tag image | docker build -t myapp:1.0 . |
-f | Dockerfile path | docker build -f Dockerfile.prod . |
--build-arg | Build arguments | docker build --build-arg NODE_ENV=prod . |
--no-cache | Don't use cache | docker build --no-cache . |
--target | Multi-stage target | docker build --target production . |
Examples:
# Basic build
docker build -t myapp:1.0 .
# Build with custom Dockerfile
docker build -f Dockerfile.prod -t myapp:prod .
# Build without cache
docker build --no-cache -t myapp:latest .
# Build with arguments
docker build \
--build-arg NODE_ENV=production \
--build-arg API_URL=https://api.example.com \
-t myapp:1.0 .
# Multi-stage build target
docker build --target production -t myapp:slim .
docker tag¶
Create alias for image.
Examples:
# Tag local image
docker tag myapp:1.0 myapp:latest
# Tag for registry
docker tag myapp:1.0 registry.example.com/myapp:1.0
docker push / docker pull¶
Push to and pull from registries.
Examples:
# Pull from Docker Hub
docker pull nginx:1.21
# Pull from private registry
docker pull registry.example.com/myapp:1.0
# Push to Docker Hub
docker push myusername/myapp:1.0
# Push to registry with version tags
docker push registry.example.com/myapp:1.0
docker push registry.example.com/myapp:latest
docker images¶
List local images.
| Option | Description |
|---|---|
-a | Show all images (including intermediate) |
-q | Show only image IDs |
--digests | Show digest (SHA256) |
--filter | Filter results |
Examples:
# List images
docker images
# Show image digests
docker images --digests
# Filter by repository
docker images myapp
# Show dangling images
docker images -f "dangling=true"
# Get image size
docker images --format "table {{.Repository}}\t{{.Size}}"
Docker Compose Commands¶
docker-compose up¶
Start services defined in docker-compose.yml.
| Option | Description |
|---|---|
-d | Detached mode |
--build | Build images before starting |
-f | Compose file path |
--scale | Scale service replicas |
Examples:
# Start all services
docker-compose up
# Detached
docker-compose up -d
# Build and start
docker-compose up --build
# Rebuild specific service
docker-compose up --build web
# Scale service
docker-compose up -d --scale worker=3
# Custom compose file
docker-compose -f docker-compose.prod.yml up
docker-compose down¶
Stop and remove services.
Examples:
# Stop containers (keep volumes)
docker-compose down
# Stop and remove volumes
docker-compose down -v
# Remove images
docker-compose down --rmi all
docker-compose logs¶
View service logs.
Examples:
# All service logs
docker-compose logs
# Follow logs
docker-compose logs -f
# Web service logs
docker-compose logs web
# Last 100 lines
docker-compose logs --tail 100
docker-compose exec¶
Execute command in service container.
Examples:
# Interactive shell
docker-compose exec web bash
# Run Django command
docker-compose exec web python manage.py migrate
# Run as specific user
docker-compose exec -u root web apt-get update
docker-compose ps¶
List services.
docker-compose restart / stop / start¶
Manage service state.
docker-compose restart [SERVICE...]
docker-compose stop [SERVICE...]
docker-compose start [SERVICE...]
Network Commands¶
docker network¶
docker network create NAME # Create network
docker network ls # List networks
docker network rm NAME # Remove network
docker network connect NETWORK CONTAINER # Connect container
docker network disconnect NETWORK CONTAINER # Disconnect container
docker network inspect NETWORK # Inspect network
Examples:
# Create bridge network
docker network create mynet
# Run containers on network
docker run -d --network mynet --name app1 myapp
docker run -d --network mynet --name app2 myapp
# Containers can resolve each other by name
docker exec app1 ping app2
# Connect running container
docker network connect mynet running-container
# Inspect network
docker network inspect mynet
Volume Commands¶
docker volume¶
docker volume create NAME # Create volume
docker volume ls # List volumes
docker volume rm NAME # Remove volume
docker volume inspect NAME # Inspect volume
docker volume prune # Remove unused volumes
Examples:
# Create named volume
docker volume create dbdata
# Use in container
docker run -v dbdata:/var/lib/postgresql/data postgres
# Inspect volume
docker volume inspect dbdata
# Clean up unused volumes
docker volume prune
System & Maintenance¶
docker system¶
System-level operations.
docker system df # Disk usage
docker system prune # Remove unused objects
docker system prune -a # Remove all unused (including images)
docker version / info¶
Common Command Patterns¶
Find and Remove¶
# Remove all stopped containers
docker rm $(docker ps -aq --filter "status=exited")
# Remove all dangling images
docker image prune
# Remove specific image by ID
docker rmi $(docker images | grep "none" | awk '{print $3}')
Monitoring¶
# Watch container logs
docker logs -f myapp
# Monitor resources
docker stats myapp
# Check container processes
docker top myapp
Debugging¶
# Shell into container
docker exec -it myapp bash
# View environment
docker exec myapp env
# Check network connectivity
docker exec myapp ping 8.8.8.8
# Inspect configuration
docker inspect myapp | jq '.Config'
For best practices and examples, see Docker Best Practices
For tutorials, see Docker Journey
For questions, see Contributing Guide