SuperAPI AI Directory

docker-workflow

Deployment & operations

View repository

Updated 10-15 22:16

About this skill

Docker Workflow

Overview

Docker containerization streamlines development, testing, and deployment by packaging applications and their dependencies into portable, reproducible containers. This SKILL guides you through professional Docker workflows, from development to operations.

Core Capabilities

  • Multi-stage builds: Separate build and runtime dependencies for optimal image size.
  • Docker Compose orchestration: Manage multi-container applications with networking and dependencies.
  • Image optimization: Reduce image size by 50-90% through best practices.
  • Development workflow: Support hot-reload, volume mounts, and environment-specific configuration.
  • Debugging tools: Provide container inspection, health checks, and troubleshooting utilities.
  • Production readiness: Cover security hardening, health checks, and deployment strategies.

When to Use This Skill

Enable when performing the following:

  • Containerizing a new application
  • Setting up a development environment with Docker
  • Creating Docker images suitable for production environments
  • Orchestrating multi-container applications
  • Debugging container issues
  • Optimizing Docker builds and images

Workflow Phases

Phase 1: Initial Setup

Create .dockerignore

Exclude unnecessary files from the build context:

node_modules/
__pycache__/
*.pyc
.git/
.env
*.log
dist/
build/
coverage/

For a comprehensive template, see examples/.dockerignore.

Core principles:

  • Exclude build artifacts and dependencies
  • Exclude sensitive files (.env, credentials)
  • Exclude version control systems (.git)
  • Reduce context size = faster builds

Analyze application requirements

Determine the following:

  • Runtime (Node.js, Python, Go, Java)
  • Dependencies and package manager
  • Build requirements vs runtime requirements
  • Port exposure and volume needs

Phase 2: Multi-Stage Dockerfile

Choose a strategy

Multi-stage builds reduce final image size by 50-90%:

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

# Stage 2: Production
FROM node:18-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
EXPOSE 3000
CMD ["node", "dist/index.js"]

For Node.js, Python, Go, Java, and Rust templates, see examples/Dockerfile.multi-stage.

Optimize Layer Caching

Order matters - place frequently changing content last:

# ✅ 좋음: 종속성이 별도로 캐시됨
COPY package.json package-lock.json ./
RUN npm ci
COPY . .

# ❌ 나쁨: 파일이 하나만 변경되어도 캐시가 무효화됨
COPY . .
RUN npm ci

Apply security best practices

# 특정 버전 사용
FROM node:18.17.1-alpine

# non-root 사용자로 실행
RUN addgroup -g 1001 -S nodejs && adduser -S nodejs -u 1001
USER nodejs

# 소유권과 함께 복사
COPY --chown=nodejs:nodejs . .

Security checklist:

  • Pin base image versions
  • Use minimal base images (alpine, slim)
  • Run as a non-root user
  • Perform vulnerability scanning
  • Minimize installation packages

Phase 3: Docker Compose Setup

Service definition

docker-compose.ymlcreates:

version: '3.8'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    environment:
      - DATABASE_URL=postgresql://db:5432/myapp
    depends_on:
      db:
        condition: service_healthy
    volumes:
      - ./src:/app/src  # 개발 시 hot-reload
    networks:
      - app-network

  db:
    image: postgres:15-alpine
    environment:
      POSTGRES_DB: myapp
    volumes:
      - postgres-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U user"]
      interval: 5s
    networks:
      - app-network

volumes:
  postgres-data:

networks:
  app-network:

For a full-featured setup including monitoring, queues, and caching, see examples/docker-compose.yml.

Environment configuration

Use override files per environment:

Development (docker-compose.override.yml):

services:
  app:
    build:
      target: development
    volumes:
      - ./src:/app/src
    environment:
      - NODE_ENV=development
    command: npm run dev

Production (docker-compose.prod.yml):

services:
  app:
    build:
      target: production
    restart: always
    environment:
      - NODE_ENV=production

Usage:

# 개발 (override 파일이 자동으로 사용됨)
docker-compose up

# 운영
docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d

Phase 4: Build and Run

Build command

# 기본 빌드
docker build -t myapp:latest .

# 특정 스테이지 빌드
docker build --target production -t myapp:prod .

# BuildKit을 사용한 빌드 (빠름)
DOCKER_BUILDKIT=1 docker build -t myapp:latest .

Run command

# 단일 컨테이너
docker run -d -p 3000:3000 -e NODE_ENV=production myapp:latest

# Docker Compose
docker-compose up -d

# 로그 확인
docker-compose logs -f app

# 컨테이너 내 명령 실행
docker-compose exec app sh

# 중지 및 제거
docker-compose down -v

Phase 5: Debugging and Troubleshooting

Using the Helper script

scripts/docker_helper.sh utility provides common debugging tasks:

# 컨테이너 상태 체크
./scripts/docker_helper.sh health myapp

# 상세 정보 조사
./scripts/docker_helper.sh inspect myapp

# 로그 확인
./scripts/docker_helper.sh logs myapp 200

# Shell 열기
./scripts/docker_helper.sh shell myapp

# 이미지 크기 분석
./scripts/docker_helper.sh size myapp:latest

# 리소스 정리 (Cleanup)
./scripts/docker_helper.sh cleanup

Common issues

When the container exits immediately:

docker logs myapp
docker run -it --entrypoint sh myapp:latest

Network connectivity:

docker network inspect myapp_default
docker exec myapp ping db

Volume permissions:

# Dockerfile에서 수정
RUN chown -R nodejs:nodejs /app/data

Phase 6: Optimization

Reducing image size

Strategies:

  1. Use smaller base images (alpine > slim > debian)
  2. Use multi-stage builds to exclude build tools
  3. Combine RUN commands to reduce the number of layers
  4. Perform cleanup within the same layer
  5. Use .dockerignore

Example:

# ✅ 좋음: 결합 및 정리 완료
RUN apt-get update && \
    apt-get install -y --no-install-recommends package1 && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

Build performance

# BuildKit 활성화
export DOCKER_BUILDKIT=1

# 캐시 마운트(Cache mounts) 사용
RUN --mount=type=cache,target=/root/.cache/pip \
    pip install -r requirements.txt

# 병렬 빌드
docker-compose build --parallel

Phase 7: Production Deployment

Dockerfile for production environment

FROM node:18-alpine AS production

# 보안: non-root 사용자
RUN addgroup -g 1001 -S nodejs && adduser -S nodejs -u 1001

WORKDIR /app
COPY --from=builder --chown=nodejs:nodejs /app/dist ./dist
USER nodejs

# Health check
HEALTHCHECK --interval=30s --timeout=3s \
  CMD node healthcheck.js

EXPOSE 3000
CMD ["node", "dist/index.js"]

Deployment command

# Registry용 태그 지정
docker tag myapp:latest registry.example.com/myapp:v1.0.0

# Registry로 Push
docker push registry.example.com/myapp:v1.0.0

# 배포
docker-compose pull && docker-compose up -d

# 무중단 업데이트 (Rolling update)
docker-compose up -d --no-deps --build app

Common Patterns

Full-Stack Application

  • Frontend + Backend + Database + Redis
  • examples/docker-compose.yml reference

Microservices

  • API Gateway + Multiple Services + Message Queue
  • Network isolation and service discovery

Applying Hot Reload during development

  • Volume mount for source code
  • Override file for development configuration

Best Practices Summary

Security

✅ latestUse specific image versions rather than ✅ Run as a non-root user ✅ Use secrets management for sensitive data ✅ Scan images for vulnerabilities ✅ Use minimal base images

Performance

✅ Use multi-stage builds ✅ Optimize layer caching ✅ Use .dockerignore ✅ Combine RUN commands ✅ Use BuildKit

Development

✅ Use docker-compose for multi-container apps ✅ Use volumes for hot-reload ✅ Implement health checks ✅ Apply proper dependency order

Production

✅ Set restart policies ✅ Use orchestration (Swarm, Kubernetes) ✅ Monitor with health checks ✅ Use a reverse proxy ✅ Implement rolling updates

Helper Resources

  • scripts/docker_helper.sh: container inspection, health checks, automation
  • examples/Dockerfile.multi-stage: templates for Node.js, Python, Go, Java, Rust
  • examples/docker-compose.yml: full-featured multi-service setup
  • examples/.dockerignore: comprehensive ignore patterns

Quick Reference

Essential Commands

# 빌드
docker build -t myapp .
docker-compose build

# 실행
docker run -d -p 3000:3000 myapp
docker-compose up -d

# 로그
docker logs -f myapp
docker-compose logs -f

# 실행 (Execute)
docker exec -it myapp sh
docker-compose exec app sh

# 중지
docker-compose down

# 정리 (Clean)
docker system prune -a

Debugging

# 조사 (Inspect)
docker inspect myapp

# 상태 (Stats)
docker stats myapp

# 네트워크
docker network inspect bridge

# 볼륨
docker volume ls