Container Management Commands
Container Management Commands
Running Containers
The docker run command creates and starts a container from an image:
# Basic run
docker run nginx:1.25
# Detached mode (background)
docker run -d --name web nginx:1.25
# Interactive with shell
docker run -it --name dev ubuntu bash
# Remove container after exit
docker run --rm python:3.12-slim python -c "print('hello')"
# Set environment variables
docker run -e MY_VAR=value -e DEBUG=true myapp:latest
# Limit resources
docker run -d --cpus="1.5" --memory="512m" myapp:latest
# Publish ports
docker run -d -p 8080:80 --name web nginx:1.25
# Mount volume
docker run -d -v mydata:/app/data --name db postgres:16
Common docker run Flags
| Flag | Description |
|---|---|
-d |
Run in detached (background) mode |
-it |
Interactive with TTY |
--name |
Assign a name to the container |
-p host:container |
Map host port to container port |
-v volume:/path |
Mount a named volume |
-e KEY=VALUE |
Set environment variable |
--rm |
Remove container on exit |
--network |
Connect to a specific network |
--restart |
Restart policy (no, always, unless-stopped, on-failure) |
--cpus |
CPU limit |
--memory |
Memory limit |
Listing Containers
# Running containers
docker ps
# All containers (including stopped)
docker ps -a
# Show container sizes
docker ps -s
# Filter by name
docker ps --filter "name=web"
# Custom format
docker ps --format "{{.ID}} {{.Names}} {{.Status}} {{.Ports}}"
Managing Container Lifecycle
# Stop a running container (sends SIGTERM, then SIGKILL after 10s)
docker stop web
# Stop immediately (SIGKILL)
docker kill web
# Start a stopped container
docker start web
# Restart a container
docker restart web
# Pause/unpause (freeze processes with SIGSTOP)
docker pause web
docker unpause web
# Remove a container (must be stopped first)
docker rm web
# Force remove (even if running)
docker rm -f web
# Remove all stopped containers
docker container prune
Image Management Commands
Image Management Commands
Listing and Inspecting Images
# List local images
docker images
# List all images (including intermediate)
docker images -a
# Show image layers
docker history nginx:1.25
# Inspect image metadata
docker inspect nginx:1.25
# Show image size
docker images --format "{{.Repository}}:{{.Tag}} {{.Size}}"
Pulling and Pushing Images
# Pull from Docker Hub
docker pull nginx:1.25
# Pull from specific registry
docker pull myregistry.com/myapp:v2
# Pull by digest (exact version)
docker pull nginx@sha256:abc123def456...
# Push to registry
docker push myregistry.com/myapp:v2
# Log in to a registry
docker login myregistry.com
Building Images
# Build from Dockerfile in current directory
docker build -t myapp:latest .
# Build with custom Dockerfile
docker build -f Dockerfile.prod -t myapp:prod .
# Build with build arguments
docker build --build-arg NODE_VERSION=20 -t myapp:latest .
# Build without cache
docker build --no-cache -t myapp:latest .
# Build and pass build context
docker build -t myapp:latest -f - . <<EOF
FROM node:20
WORKDIR /app
COPY package.json .
RUN npm install
EOF
Tagging and Removing
# Tag an image
docker tag myapp:latest myregistry.com/myapp:v1
# Remove an image
docker rmi myapp:latest
# Remove dangling images (untagged)
docker image prune
# Remove all unused images
docker image prune -a
# Remove by pattern
docker images | grep "none" | awk '{print $3}' | xargs docker rmi
Saving and Loading Images
# Save image to tar file
docker save -o myapp.tar myapp:latest
# Load image from tar file
docker load -i myapp.tar
# Export container filesystem
docker export web > web-container.tar
# Import filesystem as image
docker import web-container.tar myapp:restored
The difference between save/load (preserves image layers and metadata) and export/import (creates a flat filesystem, loses layers) is important — use save/load for image distribution.
Inspection and Debugging Commands
Inspection and Debugging Commands
Logs
# View container logs
docker logs web
# Follow logs (like tail -f)
docker logs -f web
# Show timestamps
docker logs -t web
# Last 100 lines
docker logs --tail 100 web
# Logs since a specific time
docker logs --since 2024-01-15T10:00:00 web
# Logs until a specific time
docker logs --until 2024-01-15T11:00:00 web
Exec (Run Commands Inside Containers)
# Open interactive shell
docker exec -it web bash
# Run single command
docker exec web cat /etc/nginx/nginx.conf
# Run as specific user
docker exec -u root web apt-get update
# Set working directory
docker exec -w /app web ls -la
# Set environment variable
docker exec -e DEBUG=true web env
Inspect and Stats
# Detailed container info (JSON)
docker inspect web
# Get specific field with --format
docker inspect --format '{{.NetworkSettings.IPAddress}}' web
docker inspect --format '{{.State.StartedAt}}' web
# Real-time resource usage
docker stats
# One-time snapshot
docker stats --no-stream
# Specific container
docker stats web
# Custom format
docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"
Process and Port Info
# List processes in a container
docker top web
# Show port mappings
docker port web
# Show filesystem changes
docker diff web
# Show container events
docker events --filter container=web
System Commands
# Show Docker system info
docker info
# Show disk usage
docker system df
# Detailed disk usage
docker system df -v
# Remove all unused data (images, containers, networks)
docker system prune
# Remove all unused data including volumes
docker system prune -a --volumes
Copy Files
# Copy from container to host
docker cp web:/var/log/nginx/access.log ./access.log
# Copy from host to container
docker cp ./config.conf web:/etc/nginx/conf.d/
Network and Volume Commands
Network and Volume Commands
Network Management
# List networks
docker network ls
# Create a network
docker network create --driver bridge mynet
# Create with subnet
docker network create --subnet 172.18.0.0/16 mynet
# Inspect network
docker network inspect mynet
# Connect container to network
docker network connect mynet web
# Connect with alias
docker network connect --alias db mynet web
# Disconnect network
docker network disconnect mynet web
# Remove network
docker network rm mynet
# Remove unused networks
docker network prune
Volume Management
# List volumes
docker volume ls
# Create a volume
docker volume create mydata
# Inspect volume
docker volume inspect mydata
# Remove a volume
docker volume rm mydata
# Remove unused volumes
docker volume prune
# Use volume with container
docker run -d -v mydata:/var/lib/postgresql/data postgres:16
# Read-only volume
docker run -d -v myconfig:/etc/nginx/conf.d:ro nginx:1.25
# Bind mount (host directory)
docker run -d -v /host/path:/container/path myapp:latest
Docker Compose CLI
# Start services (foreground)
docker compose up
# Start in background
docker compose up -d
# Stop services
docker compose down
# Stop and remove volumes
docker compose down -v
# Rebuild images
docker compose up --build
# View logs
docker compose logs
# Scale a service
docker compose up --scale web=3
# Execute command in service
docker compose exec web bash