Instalar com Codex ou Claude Copie este prompt, cole no Codex, Claude ou outro assistente e deixe que ele revise a página da skill e instale para você.
Um comando direto ignora o prompt de revisão. Verifique a origem antes de executá-lo.
Instruções da origem · Visualização somente leitura
name
docker
version
1.0.0
description
Complete Docker containerization patterns for development and production workflows
author
workspace-hub
category
devtools
capabilities
["Dockerfile best practices and multi-stage builds","Docker Compose orchestration and networking","Volume management and data persistence","Development vs production configurations","Container debugging and optimization","Registry management and image distribution"]
Master Docker containerization for consistent, reproducible development and production environments. This skill covers Dockerfile best practices, multi-stage builds, Docker Compose orchestration, and production-ready configurations.
When to Use This Skill
USE when:
Building reproducible development environments
Creating consistent CI/CD pipelines
Deploying microservices architectures
Isolating application dependencies
Packaging applications for distribution
Setting up local development with multiple services
Need portable environments across teams
DON'T USE when:
Simple scripts that don't need isolation
Applications that require direct hardware access
Environments where containers aren't permitted
Tasks better suited for virtual machines (full OS isolation)
# Or: docker run --rm -i hadolint/hadolint < Dockerfile
# Image analyzer (inspect layers)
# macOS
# Or: docker run --rm -it wagoodman/dive:latest <image>
# Build with BuildKit (enhanced features)
export
Core Capabilities
1. Basic Dockerfile Patterns
Simple Application Dockerfile:
# Base image with specific version
FROM python:3.12-slim
# Set working directory
WORKDIR /app
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=1
# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
# Copy dependency files first (better caching)
COPY requirements.txt .
# Install Python dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy application code
COPY . .
# Create non-root user
RUN useradd --create-home appuser && chown -R appuser:appuser /app
USER appuser
# Expose port
EXPOSE 8000
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8000/health || exit 1
# Default command
CMD ["python", "main.py"]
Node.js Application Dockerfile:
FROM node:20-alpine
WORKDIR /app
# Copy package files
COPY package*.json ./
# Install dependencies
RUN npm ci --only=production
# Copy application
COPY . .
# Non-root user (alpine already has 'node' user)
USER node
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s \
CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1
CMD ["node", "server.js"]
2. Multi-Stage Builds
Python Multi-Stage Build:
# Stage 1: Build dependencies
FROM python:3.12-slim AS builder
WORKDIR /app
# Install build dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
# Create virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Stage 2: Production image
FROM python:3.12-slim AS production
WORKDIR /app
# Install runtime dependencies only
RUN apt-get update && apt-get install -y --no-install-recommends \
libpq5 \
curl \
&& rm -rf /var/lib/apt/lists/*
# Copy virtual environment from builder
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
# Copy application code
COPY . .
# Create non-root user
RUN useradd --create-home --shell /bin/bash appuser \
&& chown -R appuser:appuser /app
USER appuser
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
CMD curl -f http://localhost:8000/health || exit 1
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "app:app"]
Node.js Multi-Stage Build:
# Stage 1: Install dependencies
FROM node:20-alpine AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci
# Stage 2: Build application
FROM node:20-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build
# Stage 3: Production image
FROM node:20-alpine AS production
WORKDIR /app
ENV NODE_ENV=production
# Copy only production dependencies
COPY --from=deps /app/node_modules ./node_modules
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/package*.json ./
USER node
EXPOSE 3000
CMD ["node", "dist/server.js"]
Go Multi-Stage Build (minimal image):
# Stage 1: Build
FROM golang:1.22-alpine AS builder
WORKDIR /app
# Download dependencies
COPY go.mod go.sum ./
RUN go mod download
# Copy source and build
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o /app/main .
# Stage 2: Minimal production image
FROM scratch
# Copy SSL certificates for HTTPS
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
# Copy binary
COPY --from=builder /app/main /main
EXPOSE 8080
ENTRYPOINT ["/main"]
3. Docker Compose for Development
Full Stack Development Environment:
# docker-compose.ymlversion:'3.8'services:# Application serviceapp:build:context:.dockerfile:Dockerfiletarget:builder# Use builder stage for developmentvolumes:-.:/app-/app/node_modules# Exclude node_modules from bind mountports:-"3000:3000"environment:-NODE_ENV=development-DATABASE_URL=postgres://devuser:devpass@db:5432/devdb-REDIS_URL=redis://redis:6379depends_on:db:condition:service_healthyredis:condition:service_startedcommand:npmrundevnetworks:-app-network# Database servicedb:image:postgres:16-alpinevolumes:-postgres_data:/var/lib/postgresql/data-./scripts/init.sql:/docker-entrypoint-initdb.d/init.sqlenvironment:POSTGRES_DB:devdbPOSTGRES_USER:devuserPOSTGRES_PASSWORD:devpassports:-"5432:5432"healthcheck:test: ["CMD-SHELL", "pg_isready -U devuser -d devdb"]
interval:5stimeout:5sretries:5networks:-app-network# Redis cacheredis:image:redis:7-alpinevolumes:-redis_data:/dataports:-"6379:6379"command:redis-server--appendonlyyesnetworks:-app-network# Adminer for database managementadminer:image:adminer:latestports:-"8080:8080"depends_on:-dbnetworks:-app-network# Nginx reverse proxynginx:image:nginx:alpinevolumes:-./nginx/nginx.conf:/etc/nginx/nginx.conf:ro-./nginx/conf.d:/etc/nginx/conf.d:roports:-"80:80"-"443:443"depends_on:-appnetworks:-app-networknetworks:app-network:driver:bridgevolumes:postgres_data:redis_data:
Development Override File:
# docker-compose.override.yml (automatically applied)version:'3.8'services:app:build:target:buildervolumes:-.:/app-/app/node_modulesenvironment:-DEBUG=true-LOG_LEVEL=debugcommand:npmrundev:watchdb:ports:-"5432:5432"# Expose for local toolsredis:ports:-"6379:6379"# Expose for local tools
version:'3.8'services:frontend:build:./frontendnetworks:-frontend-networkbackend:build:./backendnetworks:-frontend-network-backend-networkdb:image:postgres:16-alpinenetworks:-backend-networknetworks:frontend-network:driver:bridgebackend-network:driver:bridgeinternal:true# No external access
version:'3.8'services:app:image:myapp:latestvolumes:# Named volume (managed by Docker)-app_data:/app/data# Bind mount (host directory)-./config:/app/config:ro# Anonymous volume (for excluding from bind mount)-/app/node_modules# tmpfs mount (in-memory)-type:tmpfstarget:/app/tmptmpfs:size:100Mvolumes:app_data:driver:localdriver_opts:type:nonedevice:/data/appo:bind
# Dockerfile.dev
FROM node:20-alpine
WORKDIR /app
# Install development dependencies
RUN apk add --no-cache git
# Install nodemon globally for hot reload
RUN npm install -g nodemon
# Copy package files
COPY package*.json ./
# Install all dependencies (including devDependencies)
RUN npm install
# Don't copy source - use volume mount instead
# Source will be mounted at runtime
EXPOSE 3000
# Use nodemon for hot reload
CMD ["nodemon", "--watch", "src", "--ext", "js,ts,json", "src/index.js"]
# Directory structure
docker/
├── docker-compose.yml # Base configuration
├── docker-compose.dev.yml # Development overrides
├── docker-compose.test.yml # Test environment
├── docker-compose.prod.yml # Production configuration
└── .env.example # Environment template
Usage:
# Development
docker compose -f docker-compose.yml -f docker-compose.dev.yml up
# Testing
docker compose -f docker-compose.yml -f docker-compose.test.yml up
# Production
docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d
4. Database Migration Pattern
Migration Service:
# docker-compose.ymlservices:migrate:build:context:.dockerfile:Dockerfilecommand:npmrunmigrateenvironment:-DATABASE_URL=postgres://user:pass@db:5432/mydbdepends_on:db:condition:service_healthyprofiles:-migrate# Only run when explicitly requestedseed:build:context:.dockerfile:Dockerfilecommand:npmrunseedenvironment:-DATABASE_URL=postgres://user:pass@db:5432/mydbdepends_on:-migrateprofiles:-seed
Usage:
# Run migrations
docker compose --profile migrate up migrate
# Run migrations and seed
docker compose --profile migrate --profile seed up
Best Practices
1. Image Optimization
# Use specific versions
FROM python:3.12.1-slim # Not :latest
# Combine RUN commands to reduce layers
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
gcc \
libpq-dev \
&& rm -rf /var/lib/apt/lists/* \
&& pip install --no-cache-dir -r requirements.txt
# Use .dockerignore
# .dockerignore
.git
.gitignore
node_modules
npm-debug.log
Dockerfile*
docker-compose*
.dockerignore
.env*
*.md
.pytest_cache
__pycache__
*.pyc
.coverage
htmlcov
2. Security Best Practices
# Run as non-root user
RUN useradd --create-home --shell /bin/bash appuser
USER appuser
# Don't store secrets in images
# Use environment variables or secrets management
# Scan images for vulnerabilities
# docker scan myimage:latest
# Use read-only filesystem where possible
# docker run --read-only myimage
3. Layer Caching Strategy
# Order from least to most frequently changed
FROM node:20-alpine
# 1. System dependencies (rarely change)
RUN apk add --no-cache git
# 2. Package manifests (change sometimes)
COPY package*.json ./
RUN npm ci
# 3. Application code (changes often)
COPY . .
# 4. Build step
RUN npm run build
# Check logs
docker logs container-name
# Check container status
docker inspect container-name
# Run interactively to debug
docker run -it --entrypoint sh image-name
Permission denied errors:
# Fix file ownership
docker run --rm -v $(pwd):/app alpine chown -R $(id -u):$(id -g) /app
# Or use user namespace remapping
Out of disk space:
# Clean up unused resources
docker system prune -a --volumes
# Check disk usage
docker system df
Slow builds:
# Enable BuildKitexport DOCKER_BUILDKIT=1
# Use cache mounts
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt