|
| 1 | +# This Dockerfile is used for docker-based deployments to Azure for both preview environments and production |
| 2 | + |
| 3 | +# -------------------------------------------------------------------------------- |
| 4 | +# BASE IMAGE |
| 5 | +# -------------------------------------------------------------------------------- |
| 6 | +# To update the sha, run `docker pull node:$VERSION-alpine` |
| 7 | +# look for something like: `Digest: sha256:0123456789abcdef` |
| 8 | +FROM node:22-alpine@sha256:c13b26e7e602ef2f1074aef304ce6e9b7dd284c419b35d89fcf3cc8e44a8def9 AS base |
| 9 | + |
| 10 | +# This directory is owned by the node user |
| 11 | +ARG APP_HOME=/home/node/app |
| 12 | + |
| 13 | +# Make sure we don't run anything as the root user |
| 14 | +USER node |
| 15 | + |
| 16 | +WORKDIR $APP_HOME |
| 17 | + |
| 18 | + |
| 19 | +# --------------- |
| 20 | +# ALL DEPS |
| 21 | +# --------------- |
| 22 | +FROM base AS all_deps |
| 23 | + |
| 24 | +COPY --chown=node:node package.json package-lock.json ./ |
| 25 | + |
| 26 | +RUN npm ci --no-optional --registry https://registry.npmjs.org/ |
| 27 | + |
| 28 | +# For Next.js v12+ |
| 29 | +# This the appropriate necessary extra for node:VERSION-alpine |
| 30 | +# Other options are https://www.npmjs.com/search?q=%40next%2Fswc |
| 31 | +RUN npm i @next/swc-linux-x64-musl --no-save || npm i @next/swc-linux-arm64-musl --no-save |
| 32 | + |
| 33 | + |
| 34 | +# --------------- |
| 35 | +# PROD DEPS |
| 36 | +# --------------- |
| 37 | +FROM all_deps AS prod_deps |
| 38 | + |
| 39 | +RUN npm prune --production |
| 40 | + |
| 41 | + |
| 42 | +# --------------- |
| 43 | +# BUILDER |
| 44 | +# --------------- |
| 45 | +FROM all_deps AS builder |
| 46 | + |
| 47 | +COPY src ./src |
| 48 | +# The star is because it's an optional directory |
| 49 | +COPY .remotejson-cache* ./.remotejson-cache |
| 50 | +# The star is because it's an optional file |
| 51 | +COPY .pageinfo-cache.json.br* ./.pageinfo-cache.json.br |
| 52 | +# Certain content is necessary for being able to build |
| 53 | +COPY content/index.md ./content/index.md |
| 54 | +COPY content/rest ./content/rest |
| 55 | +COPY data ./data |
| 56 | + |
| 57 | +COPY next.config.js ./next.config.js |
| 58 | +COPY tsconfig.json ./tsconfig.json |
| 59 | + |
| 60 | +RUN npm run build |
| 61 | + |
| 62 | +# -------------------------------------------------------------------------------- |
| 63 | +# PREVIEW IMAGE - no translations |
| 64 | +# -------------------------------------------------------------------------------- |
| 65 | + |
| 66 | +FROM base AS preview |
| 67 | + |
| 68 | +# Copy just prod dependencies |
| 69 | +COPY --chown=node:node --from=prod_deps $APP_HOME/node_modules $APP_HOME/node_modules |
| 70 | + |
| 71 | +# Copy our front-end code |
| 72 | +COPY --chown=node:node --from=builder $APP_HOME/.next $APP_HOME/.next |
| 73 | + |
| 74 | +# We should always be running in production mode |
| 75 | +ENV NODE_ENV=production |
| 76 | + |
| 77 | +# Preferred port for server.js |
| 78 | +ENV PORT=4000 |
| 79 | + |
| 80 | +ENV ENABLED_LANGUAGES="en" |
| 81 | + |
| 82 | +# This makes it possible to set `--build-arg BUILD_SHA=abc123` |
| 83 | +# and it then becomes available as an environment variable in the docker run. |
| 84 | +ARG BUILD_SHA |
| 85 | +ENV BUILD_SHA=$BUILD_SHA |
| 86 | + |
| 87 | +# Copy only what's needed to run the server |
| 88 | +COPY --chown=node:node package.json ./ |
| 89 | +COPY --chown=node:node assets ./assets |
| 90 | +COPY --chown=node:node content ./content |
| 91 | +COPY --chown=node:node src ./src |
| 92 | +COPY --chown=node:node .remotejson-cache* ./.remotejson-cache |
| 93 | +COPY --chown=node:node .pageinfo-cache.json.br* ./.pageinfo-cache.json.br |
| 94 | +COPY --chown=node:node data ./data |
| 95 | +COPY --chown=node:node next.config.js ./ |
| 96 | +COPY --chown=node:node tsconfig.json ./ |
| 97 | + |
| 98 | +EXPOSE $PORT |
| 99 | + |
| 100 | +CMD ["node_modules/.bin/tsx", "src/frame/server.ts"] |
| 101 | + |
| 102 | +# -------------------------------------------------------------------------------- |
| 103 | +# PRODUCTION IMAGE - includes all translations |
| 104 | +# -------------------------------------------------------------------------------- |
| 105 | +FROM preview AS production |
| 106 | + |
| 107 | +# Override what was set for previews |
| 108 | +# Make this match the default of `Object.keys(languages)` in src/languages/lib/languages.js |
| 109 | +ENV ENABLED_LANGUAGES "en,zh,es,pt,ru,ja,fr,de,ko" |
| 110 | + |
| 111 | +# Copy in all translations |
| 112 | +COPY --chown=node:node translations ./translations |
0 commit comments