This guide covers Dockerfile creation, optimization, and best practices for building efficient container images.
# Start from a base image
FROM ubuntu:22.04
# Set maintainer info
LABEL maintainer="your-email@example.com"
# Set environment variables
ENV APP_HOME=/app \
NODE_ENV=production
# Set working directory
WORKDIR $APP_HOME
# Install dependencies
RUN apt-get update && apt-get install -y \
nodejs \
npm \
&& rm -rf /var/lib/apt/lists/*
# Copy application files
COPY package*.json ./
RUN npm install --production
COPY . .
# Expose ports
EXPOSE 3000
# Set the default command
CMD ["npm", "start"]# Build stage
FROM node:16 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
# Production stage
FROM node:16-slim
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY package*.json ./
RUN npm install --production
EXPOSE 3000
CMD ["npm", "start"]- Use official images when possible
- Use specific version tags instead of
latest - Use slim/alpine variants for smaller images
FROM node:16-slim
# or
FROM alpine:3.14- Combine related commands
- Order commands from least to most frequently changing
RUN apt-get update && apt-get install -y \
package1 \
package2 \
&& rm -rf /var/lib/apt/lists/*- Copy dependency files first
- Install dependencies before copying application code
COPY package.json package-lock.json ./
RUN npm install
COPY . .- Use non-root users
- Remove unnecessary dependencies
# Create and use non-root user
RUN useradd -r -u 1001 appuser
USER appuser
# Remove build dependencies
RUN apt-get purge -y build-essential- Use ARG for build-time variables
- Use ENV for runtime variables
ARG NODE_VERSION=16
FROM node:${NODE_VERSION}
ENV APP_HOME=/app
ENV NODE_ENV=productionHEALTHCHECK --interval=30s --timeout=3s \
CMD curl -f http://localhost:3000/health || exit 1# COPY is preferred for simple file copying
COPY . .
# ADD is used for URLs and tar extraction
ADD https://example.com/file.tar.gz /tmp/# ENTRYPOINT sets the main executable
ENTRYPOINT ["nginx"]
# CMD provides default arguments
CMD ["-g", "daemon off;"]# Declare volumes for persistent data
VOLUME ["/data"]FROM node:16-slim
WORKDIR /app
COPY package*.json ./
RUN npm install --production
COPY . .
EXPOSE 3000
HEALTHCHECK --interval=30s CMD curl -f http://localhost:3000/health
CMD ["npm", "start"]FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]FROM maven:3.8-openjdk-11 AS builder
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn package -DskipTests
FROM openjdk:11-jre-slim
COPY --from=builder /app/target/*.jar app.jar
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]- Use .dockerignore
node_modules
npm-debug.log
Dockerfile
.dockerignore
.git
.gitignore
- Minimize Layer Size
- Remove unnecessary files
- Clean up package manager caches
- Use multi-stage builds
- Leverage Build Cache
- Order instructions from least to most frequently changing
- Separate dependency installation from code copying
- Security Considerations
- Scan images for vulnerabilities
- Use minimal base images
- Keep base images updated
- Run as non-root user