Skip to content

Instantly share code, notes, and snippets.

@revolunet
Created August 5, 2021 04:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save revolunet/15418110220c5888745e987d6c03f50e to your computer and use it in GitHub Desktop.
Save revolunet/15418110220c5888745e987d6c03f50e to your computer and use it in GitHub Desktop.
Next.js Dockerfile with NEXT_PUBLIC_* variables
#
# Build a Next.js docker image with NEXT_PUBLIC_* env variables
#
# This makes the docker build not portable : you can't change these variables at runtime
#
FROM node:15-alpine as builder
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile --prefer-offline
COPY . ./
#
# NEXT_PUBLIC_ environment variables
#
# Next.js frontend variable are needed at build time
# because the frontend code is transpiled at build time
#
# as `yarn build` is executed INSIDE the Dockerfile
# then we need to use [docker ARG parameter](https://docs.docker.com/engine/reference/commandline/build/#set-build-time-variables---build-arg)
# to pass them to the docker build context
#
ARG NEXT_PUBLIC_ANALYTICS_ID
ENV NEXT_PUBLIC_ANALYTICS_ID=$NEXT_PUBLIC_ANALYTICS_ID
RUN yarn build
# remove dev dependencies
RUN yarn install --production --frozen-lockfile --prefer-offline
USER node
ENV PORT=$PORT_ARG
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
CMD ["yarn", "start"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment