This is the Dockerfile we use at Causal to deploy Next.js. It is loosely based on the template provided by Vercel here.
Last active
November 11, 2022 05:52
-
-
Save a-churchill/9a55fffaae97b347de5885e7b7dd189c to your computer and use it in GitHub Desktop.
Next.js Dockerfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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