Skip to content

Instantly share code, notes, and snippets.

@a-churchill
Last active November 11, 2022 05:52
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-churchill/9a55fffaae97b347de5885e7b7dd189c to your computer and use it in GitHub Desktop.
Save a-churchill/9a55fffaae97b347de5885e7b7dd189c to your computer and use it in GitHub Desktop.
Next.js Dockerfile

This is the Dockerfile we use at Causal to deploy Next.js. It is loosely based on the template provided by Vercel here.

# this Dockerfile is run in k8s, serving the Next.js frontend
FROM node:14.17.1-buster-slim
ARG version
SHELL ["/bin/bash", "-c"]
RUN : \
&& apt-get update \
&& apt-get upgrade -y \
&& apt-get autoremove -y \
&& rm -rf /var/lib/apt/lists/* \
&& :
# set up users
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
# copy build output
# these are ordered from least to most likely to change between builds, so we can utilize the Docker
# build cache as much as possible
WORKDIR /app
# configures yarn to use v2, including PnP package resolution
COPY ./.yarnrc.yml ./
# needed because we use a custom configuration - if we exclude this, Next uses the default config
COPY ./packages/frontend/next.config.js ./packages/frontend/
# all public files, i.e. fonts, images, etc.
COPY ./packages/frontend/public ./packages/frontend/public
# defines environment variables
COPY ./packages/frontend/.env.production ./packages/frontend/
# PnP package resolution file used by yarn v2
COPY ./.pnp.cjs ./
# lockfile used by yarn for package resolutions
COPY ./yarn.lock ./
# defines yarn workspace
COPY ./package.json ./
# defines "yarn start" command
COPY ./packages/frontend/package.json ./packages/frontend/
# includes all our packages & their binaries - this is the equivalent of copying over node_modules
COPY ./.yarn ./.yarn
# causal-common is used as a package
COPY ./packages/causal-common/package.json ./packages/causal-common/
COPY ./packages/causal-common/build ./packages/causal-common/build
# this is the actual Next.js output
COPY --chown=nextjs:nodejs ./packages/frontend/.next ./packages/frontend/.next
# set up server
WORKDIR /app/packages/frontend
USER nextjs
ENV PORT 3000
ENV NODE_ENV production
ENV VERSION=$version
EXPOSE $PORT
CMD yarn start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment