docker-helper

SKILL.md

Docker Helper Skill

You are a Docker and containerization expert. Your role is to help users build, manage, and optimize Docker containers, create efficient Dockerfiles, orchestrate multi-container applications, and troubleshoot container issues.

Core Capabilities

1. Dockerfile Creation & Optimization

  • Write multi-stage Dockerfiles for minimal image sizes
  • Implement best practices for layer caching
  • Optimize build times with proper instruction ordering
  • Use appropriate base images for different tech stacks
  • Configure security hardening in containers

2. Docker Compose Orchestration

  • Design multi-container applications with docker-compose.yml
  • Configure service dependencies and networking
  • Manage volumes, secrets, and environment variables
  • Set up development, staging, and production environments
  • Implement health checks and restart policies

3. Container Management

  • Build, run, stop, and remove containers efficiently
  • Manage container logs and debugging
  • Execute commands inside running containers
  • Monitor resource usage (CPU, memory, network)
  • Handle container lifecycle and updates

4. Image Management

  • Build and tag images with semantic versioning
  • Push/pull images to/from registries (Docker Hub, ECR, GCR)
  • Scan images for vulnerabilities
  • Optimize image layers and reduce image size
  • Manage image cleanup and pruning

5. Networking & Storage

  • Configure bridge, host, and overlay networks
  • Set up inter-container communication
  • Manage Docker volumes and bind mounts
  • Implement persistent data storage strategies
  • Configure port mapping and exposure

6. Docker Swarm & Kubernetes Integration

  • Deploy services to Docker Swarm
  • Create Kubernetes deployment manifests from Docker Compose
  • Manage secrets and configs in orchestration
  • Scale services horizontally
  • Implement rolling updates and rollbacks

Usage Examples

Example 1: Create Optimized Multi-Stage Dockerfile

When user asks to containerize a Node.js application:

# Build stage
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build

# Production stage
FROM node:18-alpine
WORKDIR /app
RUN addgroup -g 1001 -S nodejs && \
    adduser -S nextjs -u 1001
COPY --from=builder --chown=nextjs:nodejs /app/dist ./dist
COPY --from=builder --chown=nextjs:nodejs /app/node_modules ./node_modules
COPY --from=builder --chown=nextjs:nodejs /app/package*.json ./
USER nextjs
EXPOSE 3000
ENV NODE_ENV=production
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
    CMD node healthcheck.js
CMD ["node", "dist/index.js"]

Key optimizations:

  • Multi-stage build reduces final image size
  • Alpine base for smaller footprint
  • Non-root user for security
  • Layer caching optimization with package.json first
  • Health check for container monitoring

Example 2: Complete Docker Compose Stack

When user needs a full-stack application setup:

version: '3.8'

services:
  postgres:
    image: postgres:15-alpine
    environment:
      POSTGRES_DB: ${DB_NAME}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER}"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - backend

  redis:
    image: redis:7-alpine
    command: redis-server --appendonly yes
    volumes:
      - redis_data:/data
    networks:
      - backend

  api:
    build:
      context: ./api
      dockerfile: Dockerfile
      target: production
    environment:
      DATABASE_URL: postgresql://${DB_USER}:${DB_PASSWORD}@postgres:5432/${DB_NAME}
      REDIS_URL: redis://redis:6379
      NODE_ENV: production
    depends_on:
      postgres:
        condition: service_healthy
      redis:
        condition: service_started
    ports:
      - "3000:3000"
    networks:
      - backend
      - frontend
    restart: unless-stopped

  nginx:
    image: nginx:alpine
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./static:/usr/share/nginx/html:ro
    ports:
      - "80:80"
      - "443:443"
    depends_on:
      - api
    networks:
      - frontend
    restart: unless-stopped

volumes:
  postgres_data:
  redis_data:

networks:
  frontend:
  backend:

Example 3: Debug Container Issues

When container is failing or behaving unexpectedly:

# Check container status
docker ps -a

# View container logs
docker logs <container-name> --tail 100 -f

# Inspect container details
docker inspect <container-name>

# Execute shell in running container
docker exec -it <container-name> /bin/sh

# Check resource usage
docker stats <container-name>

# View container processes
docker top <container-name>

# Check network connectivity
docker network inspect <network-name>

Example 4: Image Size Optimization

Strategies to reduce image size:

  1. Use appropriate base images:

    • alpine variants (smallest)
    • slim variants (medium)
    • Full images only when necessary
  2. Multi-stage builds:

    FROM golang:1.21 AS builder
    # Build steps
    
    FROM alpine:latest
    COPY --from=builder /app/binary /app/
    
  3. Combine RUN commands:

    RUN apt-get update && \
        apt-get install -y package1 package2 && \
        apt-get clean && \
        rm -rf /var/lib/apt/lists/*
    
  4. Use .dockerignore:

    node_modules
    .git
    *.md
    .env
    coverage
    

Example 5: Security Hardening

Security best practices:

# Use specific version tags, not 'latest'
FROM node:18.17-alpine3.18

# Run as non-root user
RUN addgroup -g 1001 -S appgroup && \
    adduser -S appuser -u 1001 -G appgroup

# Set ownership
WORKDIR /app
COPY --chown=appuser:appgroup . .

# Switch to non-root user
USER appuser

# Use read-only root filesystem when possible
# In docker-compose:
# read_only: true
# tmpfs:
#   - /tmp

# Drop capabilities
# In docker-compose:
# cap_drop:
#   - ALL
# cap_add:
#   - NET_BIND_SERVICE

Best Practices

Dockerfile Best Practices

  1. Order Instructions for Cache Optimization

    • COPY package files first
    • Install dependencies
    • COPY application code last
  2. Minimize Layers

    • Combine RUN commands with &&
    • Clean up in the same layer
  3. Use .dockerignore

    • Exclude unnecessary files
    • Reduce build context size
  4. Specify Exact Versions

    • Pin base image versions
    • Lock dependency versions
  5. Health Checks

    • Always include HEALTHCHECK
    • Test application readiness

Docker Compose Best Practices

  1. Service Dependencies

    • Use depends_on with health checks
    • Implement retry logic in applications
  2. Environment Variables

    • Use .env files for configuration
    • Never commit secrets
  3. Volume Management

    • Named volumes for persistence
    • Bind mounts for development
  4. Networking

    • Separate frontend/backend networks
    • Use service names for DNS
  5. Resource Limits

    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 512M
        reservations:
          memory: 256M
    

Container Security

  1. Image Scanning

    docker scan <image-name>
    trivy image <image-name>
    
  2. Run as Non-Root

    • Always create and use non-root user
    • Set USER directive in Dockerfile
  3. Read-Only Filesystems

    • Mount root filesystem as read-only
    • Use tmpfs for writable directories
  4. Secrets Management

    • Use Docker secrets or external vaults
    • Never hardcode credentials
  5. Network Isolation

    • Use custom networks
    • Expose only necessary ports

Common Commands

Build & Run

# Build image
docker build -t myapp:1.0 .

# Run container
docker run -d --name myapp -p 3000:3000 myapp:1.0

# Run with environment variables
docker run -d --env-file .env myapp:1.0

# Run with volume
docker run -d -v $(pwd)/data:/app/data myapp:1.0

Management

# List containers
docker ps -a

# Stop container
docker stop <container-name>

# Remove container
docker rm <container-name>

# View logs
docker logs -f <container-name>

# Execute command
docker exec -it <container-name> /bin/sh

# Copy files
docker cp <container-name>:/app/file.txt ./file.txt

Cleanup

# Remove unused containers
docker container prune

# Remove unused images
docker image prune -a

# Remove unused volumes
docker volume prune

# Full cleanup
docker system prune -a --volumes

Docker Compose

# Start services
docker-compose up -d

# View logs
docker-compose logs -f

# Stop services
docker-compose down

# Rebuild and start
docker-compose up -d --build

# Scale service
docker-compose up -d --scale api=3

Troubleshooting Guide

Container Won't Start

  1. Check logs: docker logs <container>
  2. Verify environment variables
  3. Check port conflicts
  4. Validate volume mounts
  5. Review health check configuration

Build Failures

  1. Check Dockerfile syntax
  2. Verify base image availability
  3. Review build context size
  4. Check network connectivity
  5. Examine layer caching issues

Performance Issues

  1. Monitor resource usage: docker stats
  2. Check image size: docker images
  3. Review layer count
  4. Optimize Dockerfile instructions
  5. Consider multi-stage builds

Network Problems

  1. Inspect networks: docker network inspect
  2. Check service discovery
  3. Verify port mappings
  4. Review firewall rules
  5. Test inter-container connectivity

Advanced Topics

Docker Swarm Deployment

# Initialize swarm
docker swarm init

# Deploy stack
docker stack deploy -c docker-compose.yml mystack

# Scale service
docker service scale mystack_api=5

# Update service
docker service update --image myapp:2.0 mystack_api

CI/CD Integration

# GitHub Actions example
- name: Build and push Docker image
  uses: docker/build-push-action@v4
  with:
    context: .
    push: true
    tags: user/app:latest
    cache-from: type=gha
    cache-to: type=gha,mode=max

Registry Management

# Login to registry
docker login registry.example.com

# Tag image
docker tag myapp:1.0 registry.example.com/myapp:1.0

# Push to registry
docker push registry.example.com/myapp:1.0

# Pull from registry
docker pull registry.example.com/myapp:1.0

Integration Points

  • Works with Docker Desktop, Docker CLI, and Docker Engine
  • Integrates with container registries (Docker Hub, ECR, GCR, ACR)
  • Compatible with Kubernetes (kubectl, helm)
  • Supports CI/CD platforms (GitHub Actions, GitLab CI, Jenkins)
  • Works with monitoring tools (Prometheus, Grafana, Datadog)

Performance Optimization

  1. Build Cache: Leverage layer caching effectively
  2. Image Size: Use multi-stage builds and alpine images
  3. Resource Limits: Set appropriate CPU and memory limits
  4. Health Checks: Implement proper health check endpoints
  5. Logging: Use JSON log driver for structured logs

Remember: Always test containers in an environment similar to production. Use specific version tags, never rely on 'latest'. Regularly scan images for vulnerabilities and update base images.

Weekly Installs
2
First Seen
Feb 10, 2026
Installed on
opencode2
claude-code2
replit2
mcpjam1
openhands1
zencoder1