Unverified Commit d8660bb0 authored by Kévin Dunglas's avatar Kévin Dunglas Committed by GitHub

docker examples: improve perf by using COPY --link (#52835)

### What?

Uses `COPY --link` instructions in Docker examples.

### Why?

This significantly [accelerates Docker builds and improves caching](https://www.howtogeek.com/devops/how-to-accelerate-docker-builds-and-optimize-caching-with-copy-link/).

This instruction is supported for more than 1 year and is now supported by almost all modern Docker installations.
We use it successfully for a while in [the API Platform distribution](https://github.com/api-platform/api-platform/blob/main/pwa/Dockerfile), which includes Next.js and is derived from these examples.

### How?

By updating the existing examples.
parent 0f945637
#syntax=docker/dockerfile:1.4
FROM node:18-alpine FROM node:18-alpine
WORKDIR /app WORKDIR /app
# Install dependencies based on the preferred package manager # Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ COPY --link package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \ RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \ elif [ -f package-lock.json ]; then npm ci; \
...@@ -12,10 +13,10 @@ RUN \ ...@@ -12,10 +13,10 @@ RUN \
else echo "Warning: Lockfile not found. It is recommended to commit lockfiles to version control." && yarn install; \ else echo "Warning: Lockfile not found. It is recommended to commit lockfiles to version control." && yarn install; \
fi fi
COPY src ./src COPY --link src ./src
COPY public ./public COPY --link public ./public
COPY next.config.js . COPY --link next.config.js .
COPY tsconfig.json . COPY --link tsconfig.json .
# Next.js collects completely anonymous telemetry data about general usage. Learn more here: https://nextjs.org/telemetry # Next.js collects completely anonymous telemetry data about general usage. Learn more here: https://nextjs.org/telemetry
# Uncomment the following line to disable telemetry at run time # Uncomment the following line to disable telemetry at run time
......
#syntax=docker/dockerfile:1.4
FROM node:18-alpine FROM node:18-alpine
WORKDIR /app WORKDIR /app
# Install dependencies based on the preferred package manager # Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ COPY --link package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
# Omit --production flag for TypeScript devDependencies # Omit --production flag for TypeScript devDependencies
RUN \ RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
...@@ -13,10 +14,10 @@ RUN \ ...@@ -13,10 +14,10 @@ RUN \
else echo "Warning: Lockfile not found. It is recommended to commit lockfiles to version control." && yarn install; \ else echo "Warning: Lockfile not found. It is recommended to commit lockfiles to version control." && yarn install; \
fi fi
COPY src ./src COPY --link src ./src
COPY public ./public COPY --link public ./public
COPY next.config.js . COPY --link next.config.js .
COPY tsconfig.json . COPY --link tsconfig.json .
# Environment variables must be present at build time # Environment variables must be present at build time
# https://github.com/vercel/next.js/discussions/14030 # https://github.com/vercel/next.js/discussions/14030
......
#syntax=docker/dockerfile:1.4
FROM node:18-alpine AS base FROM node:18-alpine AS base
# Step 1. Rebuild the source code only when needed # Step 1. Rebuild the source code only when needed
...@@ -6,7 +7,7 @@ FROM base AS builder ...@@ -6,7 +7,7 @@ FROM base AS builder
WORKDIR /app WORKDIR /app
# Install dependencies based on the preferred package manager # Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ COPY --link package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
# Omit --production flag for TypeScript devDependencies # Omit --production flag for TypeScript devDependencies
RUN \ RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
...@@ -16,10 +17,10 @@ RUN \ ...@@ -16,10 +17,10 @@ RUN \
else echo "Warning: Lockfile not found. It is recommended to commit lockfiles to version control." && yarn install; \ else echo "Warning: Lockfile not found. It is recommended to commit lockfiles to version control." && yarn install; \
fi fi
COPY src ./src COPY --link src ./src
COPY public ./public COPY --link public ./public
COPY next.config.js . COPY --link next.config.js .
COPY tsconfig.json . COPY --link tsconfig.json .
# Environment variables must be present at build time # Environment variables must be present at build time
# https://github.com/vercel/next.js/discussions/14030 # https://github.com/vercel/next.js/discussions/14030
...@@ -48,16 +49,17 @@ FROM base AS runner ...@@ -48,16 +49,17 @@ FROM base AS runner
WORKDIR /app WORKDIR /app
# Don't run production as root # Don't run production as root
RUN addgroup --system --gid 1001 nodejs RUN \
RUN adduser --system --uid 1001 nextjs addgroup --system --gid 1001 nodejs; \
adduser --system --uid 1001 nextjs
USER nextjs USER nextjs
COPY --from=builder /app/public ./public COPY --from=builder --link /app/public ./public
# Automatically leverage output traces to reduce image size # Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing # https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --link --chown=1001:1001 /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static COPY --from=builder --link --chown=1001:1001 /app/.next/static ./.next/static
# Environment variables must be redefined at run time # Environment variables must be redefined at run time
ARG ENV_VARIABLE ARG ENV_VARIABLE
......
#syntax=docker/dockerfile:1.4
FROM node:18-alpine AS base FROM node:18-alpine AS base
# 1. Install dependencies only when needed # 1. Install dependencies only when needed
...@@ -8,7 +9,7 @@ RUN apk add --no-cache libc6-compat ...@@ -8,7 +9,7 @@ RUN apk add --no-cache libc6-compat
WORKDIR /app WORKDIR /app
# Install dependencies based on the preferred package manager # Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ COPY --link package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \ RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \ elif [ -f package-lock.json ]; then npm ci; \
...@@ -19,10 +20,10 @@ RUN \ ...@@ -19,10 +20,10 @@ RUN \
# 2. Rebuild the source code only when needed # 2. Rebuild the source code only when needed
FROM base AS builder FROM base AS builder
WORKDIR /app WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules COPY --from=deps --link /app/node_modules ./node_modules
COPY . . COPY --link . .
# This will do the trick, use the corresponding env file for each environment. # This will do the trick, use the corresponding env file for each environment.
COPY .env.development.sample .env.production COPY --link .env.development.sample .env.production
RUN yarn build RUN yarn build
# 3. Production image, copy all the files and run next # 3. Production image, copy all the files and run next
...@@ -31,15 +32,16 @@ WORKDIR /app ...@@ -31,15 +32,16 @@ WORKDIR /app
ENV NODE_ENV=production ENV NODE_ENV=production
RUN addgroup -g 1001 -S nodejs RUN \
RUN adduser -S nextjs -u 1001 addgroup -g 1001 -S nodejs; \
adduser -S nextjs -u 1001
COPY --from=builder /app/public ./public COPY --from=builder --link /app/public ./public
# Automatically leverage output traces to reduce image size # Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing # https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --link --chown=1001:1001 /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static COPY --from=builder --link --chown=1001:1001 /app/.next/static ./.next/static
USER nextjs USER nextjs
......
#syntax=docker/dockerfile:1.4
FROM node:18-alpine AS base FROM node:18-alpine AS base
# 1. Install dependencies only when needed # 1. Install dependencies only when needed
...@@ -8,7 +9,7 @@ RUN apk add --no-cache libc6-compat ...@@ -8,7 +9,7 @@ RUN apk add --no-cache libc6-compat
WORKDIR /app WORKDIR /app
# Install dependencies based on the preferred package manager # Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ COPY --link package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \ RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \ elif [ -f package-lock.json ]; then npm ci; \
...@@ -20,10 +21,10 @@ RUN \ ...@@ -20,10 +21,10 @@ RUN \
# 2. Rebuild the source code only when needed # 2. Rebuild the source code only when needed
FROM base AS builder FROM base AS builder
WORKDIR /app WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules COPY --from=deps --link /app/node_modules ./node_modules
COPY . . COPY --link . .
# This will do the trick, use the corresponding env file for each environment. # This will do the trick, use the corresponding env file for each environment.
COPY .env.production.sample .env.production COPY --link .env.production.sample .env.production
RUN yarn build RUN yarn build
# 3. Production image, copy all the files and run next # 3. Production image, copy all the files and run next
...@@ -32,15 +33,16 @@ WORKDIR /app ...@@ -32,15 +33,16 @@ WORKDIR /app
ENV NODE_ENV=production ENV NODE_ENV=production
RUN addgroup -g 1001 -S nodejs RUN \
RUN adduser -S nextjs -u 1001 addgroup -g 1001 -S nodejs; \
adduser -S nextjs -u 1001
COPY --from=builder /app/public ./public COPY --from=builder --link /app/public ./public
# Automatically leverage output traces to reduce image size # Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing # https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --link --chown=1001:1001 /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static COPY --from=builder --link --chown=1001:1001 /app/.next/static ./.next/static
USER nextjs USER nextjs
......
#syntax=docker/dockerfile:1.4
FROM node:18-alpine AS base FROM node:18-alpine AS base
# 1. Install dependencies only when needed # 1. Install dependencies only when needed
...@@ -8,7 +9,7 @@ RUN apk add --no-cache libc6-compat ...@@ -8,7 +9,7 @@ RUN apk add --no-cache libc6-compat
WORKDIR /app WORKDIR /app
# Install dependencies based on the preferred package manager # Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ COPY --link package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \ RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \ elif [ -f package-lock.json ]; then npm ci; \
...@@ -20,10 +21,10 @@ RUN \ ...@@ -20,10 +21,10 @@ RUN \
# 2. Rebuild the source code only when needed # 2. Rebuild the source code only when needed
FROM base AS builder FROM base AS builder
WORKDIR /app WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules COPY --from=deps --link /app/node_modules ./node_modules
COPY . . COPY --link . .
# This will do the trick, use the corresponding env file for each environment. # This will do the trick, use the corresponding env file for each environment.
COPY .env.staging.sample .env.production COPY --link .env.staging.sample .env.production
RUN yarn build RUN yarn build
# 3. Production image, copy all the files and run next # 3. Production image, copy all the files and run next
...@@ -32,15 +33,16 @@ WORKDIR /app ...@@ -32,15 +33,16 @@ WORKDIR /app
ENV NODE_ENV=production ENV NODE_ENV=production
RUN addgroup -g 1001 -S nodejs RUN \
RUN adduser -S nextjs -u 1001 addgroup -g 1001 -S nodejs; \
adduser -S nextjs -u 1001
COPY --from=builder /app/public ./public COPY --from=builder --link /app/public ./public
# Automatically leverage output traces to reduce image size # Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing # https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --link --chown=1001:1001 /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static COPY --from=builder --link --chown=1001:1001 /app/.next/static ./.next/static
USER nextjs USER nextjs
......
#syntax=docker/dockerfile:1.4
FROM node:18-alpine AS base FROM node:18-alpine AS base
# Install dependencies only when needed # Install dependencies only when needed
...@@ -7,7 +8,7 @@ RUN apk add --no-cache libc6-compat ...@@ -7,7 +8,7 @@ RUN apk add --no-cache libc6-compat
WORKDIR /app WORKDIR /app
# Install dependencies based on the preferred package manager # Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ COPY --link package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \ RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \ elif [ -f package-lock.json ]; then npm ci; \
...@@ -19,8 +20,8 @@ RUN \ ...@@ -19,8 +20,8 @@ RUN \
# Rebuild the source code only when needed # Rebuild the source code only when needed
FROM base AS builder FROM base AS builder
WORKDIR /app WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules COPY --from=deps --link /app/node_modules ./node_modules
COPY . . COPY --link . .
# Next.js collects completely anonymous telemetry data about general usage. # Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry # Learn more here: https://nextjs.org/telemetry
...@@ -40,15 +41,16 @@ ENV NODE_ENV production ...@@ -40,15 +41,16 @@ ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime. # Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1 # ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup --system --gid 1001 nodejs RUN \
RUN adduser --system --uid 1001 nextjs addgroup --system --gid 1001 nodejs; \
adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public COPY --from=builder --link /app/public ./public
# Automatically leverage output traces to reduce image size # Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing # https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --link --chown=1001:1001 /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static COPY --from=builder --link --chown=1001:1001 /app/.next/static ./.next/static
USER nextjs USER nextjs
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment