Created
February 16, 2021 15:13
-
-
Save kachar/317048e11e51b6b0ef1d2925f800754a to your computer and use it in GitHub Desktop.
Production ready multi-stage Next.js docker container based on node:14-alpine (avg size 250mb)
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
# Build target dependencies # | |
########################### | |
FROM node:14-alpine AS base | |
WORKDIR /app | |
ARG NODE_ENV=production | |
ENV PATH=/app/node_modules/.bin:$PATH \ | |
NODE_ENV="$NODE_ENV" | |
COPY package.json yarn.lock /app/ | |
EXPOSE 3040 | |
# Build target dependencies # | |
########################### | |
FROM base AS dependencies | |
# Install prod dependencies | |
RUN yarn install --production | |
# Cache prod dependencies | |
RUN cp -R node_modules /prod_node_modules | |
# Install dev dependencies | |
RUN yarn install | |
# Build target development # | |
############################ | |
FROM dependencies AS development | |
COPY . /app | |
CMD [ "yarn", "dev" ] | |
# Build target builder # | |
######################## | |
FROM base AS builder | |
COPY . /app | |
RUN yarn add --dev typescript @types/node && \ | |
yarn build && \ | |
rm -rf node_modules | |
# Build target production # | |
########################### | |
FROM base AS production | |
COPY --from=builder /app/.next /app/.next | |
COPY --from=dependencies /prod_node_modules /app/node_modules | |
COPY . /app | |
CMD [ "yarn", "start" ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment