Last updated
Key Best Practices Applied
- Use specific version tags (node:20-alpine, not node:latest) for reproducible builds
- Use slim/alpine base images to minimize image size
- Copy dependency files before source code to maximize layer cache reuse
- Run as a non-root user for security
- Use multi-stage builds to exclude build tools from production images
- Add HEALTHCHECK instructions for container orchestration
- Use .dockerignore to exclude unnecessary files from the build context
Use the Dockerfile Generator at techconverter.me to select your application type and framework, configure your requirements, and get a production-ready Dockerfile with all best practices applied automatically.
Examples
Example 1: Node.js Application
# Generated Dockerfile for Node.js (Express API)
# Multi-stage build for smaller production image
# Stage 1: Build
FROM node:20-alpine AS builder
WORKDIR /app
# Copy dependency files first (cache optimization)
COPY package.json package-lock.json ./
RUN npm ci --only=production
# Stage 2: Production
FROM node:20-alpine AS production
WORKDIR /app
# Run as non-root user (security best practice)
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
# Copy only production dependencies
COPY --from=builder /app/node_modules ./node_modules
COPY . .
USER appuser
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s \
CMD wget -qO- http://localhost:3000/health || exit 1
CMD ["node", "src/index.js"]
Example 2: Python Application (FastAPI)
# Generated Dockerfile for Python FastAPI application
FROM python:3.12-slim AS base
# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
curl \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
# Copy requirements first (layer cache optimization)
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy application code
COPY . .
# Run as non-root user
RUN useradd -m -u 1001 appuser
USER appuser
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s \
CMD curl -f http://localhost:8000/health || exit 1
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
Example 3: Java Spring Boot (Multi-Stage)
# Generated Dockerfile for Java Spring Boot
# Uses JDK for build, JRE for runtime (smaller image)
# Stage 1: Build with Maven
FROM maven:3.9-eclipse-temurin-21 AS builder
WORKDIR /app
# Copy pom.xml first for dependency caching
COPY pom.xml .
RUN mvn dependency:go-offline -q
# Copy source and build
COPY src ./src
RUN mvn package -DskipTests -q
# Stage 2: Runtime with JRE only
FROM eclipse-temurin:21-jre-alpine AS production
WORKDIR /app
# Non-root user
RUN addgroup -S spring && adduser -S spring -G spring
COPY --from=builder /app/target/*.jar app.jar
USER spring
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s \
CMD wget -qO- http://localhost:8080/actuator/health || exit 1
ENTRYPOINT ["java", "-jar", "app.jar"]