Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(dockerfile): use multi-stage build; reduce size by ~28% #1838

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
FROM node:20-bullseye-slim
# ------------------
# Temp image
# ------------------
FROM node:20-bullseye-slim AS tmp

# Workdir
WORKDIR /usr/app

# Create temp folder for files to be stored whilst being converted
RUN mkdir -p ./dist/temp/
WORKDIR /usr/app/tmp

# Install OS dependencies
# Curl needed for healthcheck command
RUN apt-get -q update &&\
apt-get -y --no-install-recommends install curl poppler-data poppler-utils unrtf && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# Copy and install node dependencies
COPY package.json package-lock.json ./
# Copy and install dependencies separately from the app's code
# to take advantage of Docker's layer caching
# See: https://docs.docker.com/build/cache/#optimizing-how-you-use-the-build-cache
COPY package.json .
COPY package-lock.json .
RUN npm ci --ignore-scripts --omit=dev && \
npm pkg delete commitlint devDependencies jest nodemonConfig scripts && \
npm cache clean --force && \
Expand All @@ -27,8 +23,27 @@ RUN npm ci --ignore-scripts --omit=dev && \

# Copy source
COPY . .
# Change ownership of all files and directories
RUN chown -R node:node .

# Create temp folder for files to be stored whilst being converted
RUN mkdir -p ./dist/temp/

# ------------------
# Final image
# ------------------
FROM node:20-bullseye-slim AS main

# Workdir
WORKDIR /usr/app

# Install OS dependencies
# Curl needed for healthcheck command
RUN apt-get -q update &&\
apt-get -y --no-install-recommends install curl poppler-data poppler-utils unrtf && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# Copy files from tmp image and change ownership
COPY --from=tmp --chown=node:node /usr/app/tmp /usr/app

# Node images provide 'node' unprivileged user to run apps and prevent
# privilege escalation attacks
Expand Down