
Docker Multi Stage
Generate a production Node.js Docker image using a three-stage Alpine build with non-root user and health checks.
Install
npx skills add https://github.com/pluginagentmarketplace/custom-plugin-docker --skill docker-multi-stageWhat is this skill?
- Three stages: deps (npm ci), builder (npm run build + prune), production Alpine runtime
- node:20-alpine base with non-root nextjs user (uid 1001)
- Copies only production node_modules and dist output into final image
- HEALTHCHECK against HTTP on port 3000 with wget
- NODE_ENV=production and PORT=3000 baked into runtime stage
Adoption & trust: 1 installs on skills.sh; 2 GitHub stars; 3/3 security scanners passed (skills.sh audits); trending (+100% hot-view momentum).
Recommended Skills
Journey fit
Multi-stage Dockerfiles are canonical Ship work—turning a built Node app into a small, runnable production artifact right before or during launch. Launch subphase covers container images, runtime defaults, and deploy-ready packaging rather than application feature coding.
Common Questions / FAQ
Is Docker Multi Stage safe to install?
skills.sh reports 3 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.
SKILL.md
READMESKILL.md - Docker Multi Stage
# Node.js Multi-Stage Dockerfile # Optimized for production with minimal image size # ============================================ # Stage 1: Dependencies # ============================================ FROM node:20-alpine AS deps WORKDIR /app # Copy package files COPY package*.json ./ # Install ALL dependencies (including dev) RUN npm ci # ============================================ # Stage 2: Builder # ============================================ FROM node:20-alpine AS builder WORKDIR /app # Copy dependencies from deps stage COPY --from=deps /app/node_modules ./node_modules # Copy source code COPY . . # Build the application RUN npm run build # Prune dev dependencies RUN npm prune --production # ============================================ # Stage 3: Production # ============================================ FROM node:20-alpine AS production # Security: Run as non-root user RUN addgroup -g 1001 -S nodejs && \ adduser -S nextjs -u 1001 WORKDIR /app # Copy only production dependencies COPY --from=builder --chown=nextjs:nodejs /app/node_modules ./node_modules # Copy built application COPY --from=builder --chown=nextjs:nodejs /app/dist ./dist COPY --from=builder --chown=nextjs:nodejs /app/package.json ./ # Set environment ENV NODE_ENV=production ENV PORT=3000 # Switch to non-root user USER nextjs # Expose port EXPOSE 3000 # Health check HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ CMD wget --no-verbose --tries=1 --spider http://localhost:3000/health || exit 1 # Start application CMD ["node", "dist/index.js"] # ============================================ # Build: docker build -t myapp:prod . # Run: docker run -p 3000:3000 myapp:prod # ============================================ # Python Multi-Stage Dockerfile # Optimized for production with minimal image size # ============================================ # Stage 1: Builder # ============================================ FROM python:3.12-slim AS builder WORKDIR /app # Install build dependencies RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ && rm -rf /var/lib/apt/lists/* # Create virtual environment RUN python -m venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" # Install Python dependencies COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # ============================================ # Stage 2: Production # ============================================ FROM python:3.12-slim AS production # Security: Create non-root user RUN groupadd -r appgroup && useradd -r -g appgroup appuser WORKDIR /app # Copy virtual environment from builder COPY --from=builder /opt/venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" # Copy application code COPY --chown=appuser:appgroup . . # Remove unnecessary files RUN rm -rf tests/ docs/ *.md Makefile # Set environment variables ENV PYTHONDONTWRITEBYTECODE=1 \ PYTHONUNBUFFERED=1 \ PYTHONFAULTHANDLER=1 # Switch to non-root user USER appuser # Expose port EXPOSE 8000 # Health check HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \ CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')" || exit 1 # Run application CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] # ============================================ # Build: docker build -t myapp:prod . # Run: docker run -p 8000:8000 myapp:prod # ============================================ # Multi-Stage Build Patterns ## Pattern 1: Build and Production The most common pattern - build in one stage, run in another. ```dockerfile # Build stage FROM node:20 AS builder WORKDIR /app COPY . . RUN npm ci && npm run build # Production stage FROM node:20-alpine COPY --from=builder /app/dist ./dist CMD ["node", "dist/index.js"] ``` ## Pattern 2: Dependency Caching Separate dependency installation for better caching. ```dockerfile # Dependencies stage FROM node:20-alpine AS deps C