Last active
February 10, 2022 09:39
-
-
Save stefanopascazi/a683194a2e3f1a391027430486673023 to your computer and use it in GitHub Desktop.
Dockerfile for build NextJS application
This file contains 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
# Double-container Dockerfile for separated build process. | |
# If you're just copy-pasting this, don't forget a .dockerignore! | |
# We're starting with the same base image, but we're declaring | |
# that this block outputs an image called DEPS that we | |
# won't be deploying - it just installs our Yarn deps | |
FROM node:16.13.0-alpine AS deps | |
# If you need libc for any of your deps, uncomment this line: | |
# RUN apk add --no-cache libc6-compat | |
# Copy over ONLY the package.json and yarn.lock | |
# so that this `yarn install` layer is only recomputed | |
# if these dependency files change. Nice speed hack! | |
WORKDIR /app | |
COPY package.json ./ | |
RUN npm install --frozen-lockfile | |
# END DEPS IMAGE | |
# Now we make a container to handle our Build | |
FROM node:16.13.0-alpine AS BUILD_IMAGE | |
# Set up our work directory again | |
WORKDIR /app | |
# Bring over the deps we installed and now also | |
# the rest of the source code to build the Next | |
# server for production | |
COPY --from=deps /app/node_modules ./node_modules | |
COPY . . | |
RUN npm run build | |
# Remove all the development dependencies since we don't | |
# need them to run the actual server. | |
RUN rm -rf node_modules | |
RUN npm install --production --frozen-lockfile --ignore-scripts --prefer-offline | |
# END OF BUILD_IMAGE | |
# This starts our application's run image - the final output of build. | |
FROM node:16.13.0-alpine | |
ENV NODE_ENV production | |
RUN addgroup -g 1001 -S nodejs | |
RUN adduser -S nextjs -u 1001 | |
# Pull the built files out of BUILD_IMAGE - we need: | |
# 1. the package.json and yarn.lock | |
# 2. the Next build output and static files | |
# 3. the node_modules. | |
WORKDIR /app | |
COPY --from=BUILD_IMAGE --chown=nextjs:nodejs /app/package.json ./ | |
COPY --from=BUILD_IMAGE --chown=nextjs:nodejs /app/node_modules ./node_modules | |
COPY --from=BUILD_IMAGE --chown=nextjs:nodejs /app/public ./public | |
COPY --from=BUILD_IMAGE --chown=nextjs:nodejs /app/.next ./.next | |
# 4. OPTIONALLY the next.config.js, if your app has one | |
COPY --from=BUILD_IMAGE --chown=nextjs:nodejs /app/next.config.js ./ | |
USER nextjs | |
EXPOSE 3000 | |
CMD [ "npm", "start" ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment