Skip to content

Instantly share code, notes, and snippets.

@scarstens
Created April 10, 2022 03:34
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 scarstens/3d0b41094ba8317a089c2f294f359910 to your computer and use it in GitHub Desktop.
Save scarstens/3d0b41094ba8317a089c2f294f359910 to your computer and use it in GitHub Desktop.
Dockerfile for production deploys
# 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 npm deps
FROM node:14-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 package-lock.json
# so that this `npm install` layer is only recomputed
# if these dependency files change. Nice speed hack!
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install --frozen-lockfile
# END DEPS IMAGE
# Now we make a container to handle our Build
FROM node:14-alpine AS BUILD_IMAGE
ENV APP_ENV development
# 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
RUN ls /app/.next
# Remove all the development dependencies since we don't
# need them to run the actual server.
RUN rm -rf node_modules
RUN npm install --only=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:14-alpine
ENV APP_ENV development
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 package-lock.json
# 2. the Next build output and static files
# 3. the node_modules.
WORKDIR /app
COPY --from=BUILD_IMAGE --chown=nextjs:nodejs /app/package.json /app/package-lock.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