Why Multi-Stage?
- Smaller final images
- Separate build and runtime dependencies
- Better security
Basic Multi-Stage
dockerfile
1# Build stage
2FROM node:18 AS builder
3WORKDIR /app
4COPY package*.json ./
5RUN npm ci
6COPY . .
7RUN npm run build
8
9# Production stage
10FROM nginx:alpine
11COPY /app/dist /usr/share/nginx/htmlRails Example
dockerfile
1# Build stage
2FROM ruby:3.2 AS builder
3RUN apt-get update && apt-get install -y build-essential
4WORKDIR /app
5COPY Gemfile* ./
6RUN bundle install --deployment
7
8# Production stage
9FROM ruby:3.2-slim
10RUN apt-get update && apt-get install -y libpq5
11WORKDIR /app
12COPY /app/vendor/bundle vendor/bundle
13COPY . .
14CMD ["rails", "server"]Multiple Build Stages
dockerfile
1FROM node:18 AS frontend
2WORKDIR /app
3COPY package*.json ./
4RUN npm ci && npm run build
5
6FROM ruby:3.2 AS backend
7WORKDIR /app
8COPY Gemfile* ./
9RUN bundle install
10
11FROM ruby:3.2-slim
12COPY /app/public/assets public/assets
13COPY /app/vendor vendor