Skip to content

Commit

Permalink
Reduce docker image size and fine-tune multi-stage builds 🐳
Browse files Browse the repository at this point in the history
  • Loading branch information
kachar committed Feb 16, 2021
1 parent ee1f55d commit d15bebe
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
node_modules
.next
.dockerignore
.github
.git
Dockerfile
48 changes: 29 additions & 19 deletions Dockerfile
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" ]
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ services:
# Local:Container mounting points
- ./src:/app/src
- ./public:/app/public
- /app/node_modules
- /app/.next

environment:
API_URL: ${API_URL}
Expand Down

0 comments on commit d15bebe

Please sign in to comment.