As a DevOps engineer, mastering Docker is non-negotiable! Here's your comprehensive guide to essential Docker commands that will streamline your workflow and elevate your efficiency. We'll dive deep into each command, exploring their options, use cases, and best practices.
π Docker Basics: Foundation Commands
Let's start with the fundamental commands every DevOps engineer should know:
# Verify Docker installation
docker --version
# Output example:
# Docker version 24.0.7, build afdd53b4d3
# Get a detailed overview of your Docker system
docker info
# This shows:
# - Container statistics
# - Storage driver details
# - Runtime information
# - Network settings
# - Hardware configuration
# - Security settings
# Example output sections:
# Containers: 5
# Running: 3
# Paused: 0
# Stopped: 2
# Images: 47
# Access command-specific guidance
docker help
# Or get help for specific commands:
docker run --help
π Container Lifecycle Management: Detailed Operations
Master these essential container management commands:
# Launch containers with various options
docker run [OPTIONS] IMAGE [COMMAND]
# Common options:
# -d: Run container in background
# -p HOST:CONTAINER: Port mapping
# -v HOST:CONTAINER: Volume mapping
# --name: Assign container name
# --network: Connect to network
# --rm: Remove container when it exits
# -e: Set environment variables
# Example:
docker run -d \
--name my-nginx \
-p 80:80 \
-v /host/content:/usr/share/nginx/html \
-e NGINX_HOST=example.com \
nginx:latest
# View running containers
docker ps
# Shows all running containers with:
# - Container ID
# - Image
# - Command
# - Created time
# - Status
# - Ports
# - Names
# View all containers (including stopped)
docker ps -a
# Additional flags:
# -q: Only display IDs
# -s: Display total file sizes
# --format: Custom output format
# Example:
docker ps -a --format "table {{.ID}}\t{{.Image}}\t{{.Status}}"
# Manage container states
docker stop CONTAINER
# Gracefully stops container (SIGTERM, then SIGKILL)
# Example: docker stop my-nginx
docker start CONTAINER
# Starts a stopped container
# Example: docker start my-nginx
docker restart CONTAINER
# Combination of stop and start
# Example: docker restart my-nginx
# Remove unwanted containers
docker rm CONTAINER
# Options:
# -f: Force removal of running container
# -v: Remove associated volumes
# Example:
docker rm -f $(docker ps -aq) # Remove all containers
# Force-stop misbehaving containers
docker kill CONTAINER
# Sends SIGKILL signal immediately
# Example: docker kill my-nginx
π Image Management: Comprehensive Control
Detailed image management commands for building and maintaining your Docker environment:
# View local images
docker images
# Shows:
# - Repository
# - Tag
# - Image ID
# - Created
# - Size
# Additional flags:
# -a: Show all images
# --digests: Show digests
# --format: Custom output format
# Example:
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
# Fetch images from a registry
docker pull IMAGE
# Options:
# -a: Pull all tagged images
# --platform: Specify platform
# Example:
docker pull nginx:latest --platform linux/amd64
# Build custom images
docker build -t TAG .
# Options:
# --no-cache: Build from scratch
# --build-arg: Set build-time variables
# --target: Specify target build stage
# Example:
docker build -t myapp:1.0 \
--build-arg VERSION=1.0 \
--no-cache \
.
# Clean up unused images
docker rmi IMAGE
# Options:
# -f: Force removal
# Example:
docker rmi $(docker images -q -f "dangling=true")
π Networking: Advanced Configuration
Comprehensive networking commands for container connectivity:
# Explore networks
docker network ls
# Shows:
# - Network ID
# - Name
# - Driver
# - Scope
# Create a new network
docker network create NETWORK
# Options:
# --driver: Specify network driver
# --subnet: Specify subnet
# --gateway: Specify gateway
# --ip-range: Specify IP range
# Example:
docker network create \
--driver overlay \
--subnet=10.0.9.0/24 \
--gateway=10.0.9.1 \
my-network
# Manage container connections
docker network connect NETWORK CONTAINER
# Options:
# --alias: Add network-scoped alias
# --ip: Specify IP address
# Example:
docker network connect \
--alias db \
my-network \
my-container
docker network disconnect NETWORK CONTAINER
# Force option available:
# -f: Force disconnect
π Volumes: Data Persistence
Complete volume management for persistent data:
# Check your volumes
docker volume ls
# Options:
# -f: Filter volumes
# --format: Custom output format
# Example:
docker volume ls -f "dangling=true"
# Add persistent storage
docker volume create VOLUME
# Options:
# --driver: Specify volume driver
# --opt: Set driver options
# Example:
docker volume create \
--driver local \
--opt type=nfs \
--opt o=addr=192.168.1.1,rw \
--opt device=:/path/to/dir \
my-volume
# Remove unused volumes
docker volume rm VOLUME
# Example for cleaning all unused volumes:
docker volume prune -f
π Docker Compose: Multi-Container Orchestration
Detailed Docker Compose commands for service management:
# Start or stop services
docker-compose up
# Options:
# -d: Detached mode
# --build: Build images before starting
# --scale: Scale services
# Example:
docker-compose up -d --build --scale web=3
docker-compose down
# Options:
# --volumes: Remove named volumes
# --rmi all: Remove all images
# Example:
docker-compose down --volumes --rmi all
# Rebuild service images
docker-compose build
# Options:
# --no-cache: Build without cache
# --pull: Always pull newer images
# Example:
docker-compose build --no-cache --pull
# Debug with service logs
docker-compose logs
# Options:
# -f: Follow log output
# --tail: Number of lines to show
# Example:
docker-compose logs -f --tail=100 web
π Inspection & Debugging: Advanced Tools
Comprehensive debugging and inspection commands:
# Gather detailed insights
docker inspect CONTAINER/IMAGE
# Options:
# -f: Format output
# Example:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container
# Access container logs
docker logs CONTAINER
# Options:
# -f: Follow log output
# --since: Show logs since timestamp
# --until: Show logs before timestamp
# --tail: Number of lines to show
# Example:
docker logs \
--since 2024-01-01T00:00:00Z \
--tail 1000 \
-f \
my-container
# Dive into running containers
docker exec -it CONTAINER bash
# Options:
# -e: Set environment variables
# -u: Specify user
# Example:
docker exec -it \
-e DEBUG=true \
-u root \
my-container \
bash
Production Best Practices
- Resource Management:
# Set resource constraints
docker run -d \
--memory="2g" \
--memory-swap="4g" \
--cpus="2.5" \
--pids-limit=100 \
your-image
- Logging Configuration:
# Configure logging drivers
docker run -d \
--log-driver json-file \
--log-opts max-size=10m \
--log-opts max-file=3 \
your-image
- Security Hardening:
# Run with security options
docker run -d \
--security-opt no-new-privileges \
--cap-drop ALL \
--cap-add NET_BIND_SERVICE \
your-image
Health Monitoring and Maintenance
# Monitor container health
docker run -d \
--health-cmd="curl -f http://localhost/ || exit 1" \
--health-interval=30s \
--health-retries=3 \
--health-timeout=5s \
your-image
# Clean up system
docker system prune -a --volumes \
--filter "until=24h"
Conclusion
Master these commands, and you're on your way to streamlining workflows and automating smarter. Remember that effective Docker usage isn't just about knowing the commands β it's about understanding when and how to use them effectively. Let's build, ship, and run with confidence!
Tags: #Docker #DevOps #CloudEngineering #Containerization #TechTutorial
Top comments (0)