-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce docker image size and fine-tune multi-stage builds 🐳
- Loading branch information
Showing
3 changed files
with
33 additions
and
19 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
node_modules | ||
.next | ||
.dockerignore | ||
.github | ||
.git | ||
Dockerfile |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,41 @@ | ||
# Pull node image from docker hub | ||
FROM node:14 AS development | ||
|
||
# Set working directory | ||
# Build target dependencies # | ||
########################### | ||
FROM node:14-alpine AS base | ||
WORKDIR /app | ||
|
||
ARG NODE_ENV=development | ||
|
||
# Add `/app/node_modules/.bin` to $PATH | ||
ARG NODE_ENV=production | ||
ENV PATH=/app/node_modules/.bin:$PATH \ | ||
NODE_ENV="$NODE_ENV" | ||
|
||
# Install and cache app dependencies | ||
COPY package.json yarn.lock /app/ | ||
RUN cd /app/ && yarn install --production=false | ||
|
||
# Copy existing application directory contents | ||
COPY . /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 development AS production | ||
ARG NODE_ENV=production | ||
WORKDIR /app | ||
RUN yarn build | ||
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" ] |
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