Skip to content

Commit

Permalink
build(dockerfile): use multi-stage build; reduce size by ~28%
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs committed Apr 25, 2024
1 parent ffe446d commit ad1b3a6
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
FROM node:20-bullseye-slim
# ------------------
# Temp image
# ------------------
FROM node:20-bullseye-slim AS tmp

# Workdir
WORKDIR /usr/app
WORKDIR /usr/app/tmp

# Copy source
COPY . /usr/app/tmp

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

# 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 ./
# Install node dependencies
RUN npm ci --ignore-scripts --omit=dev && \
npm pkg delete commitlint devDependencies jest nodemonConfig scripts && \
npm cache clean --force && \
Expand All @@ -25,10 +23,23 @@ RUN npm ci --ignore-scripts --omit=dev && \
rm -rf ./node_modules/htmltidy2/bin/win64 && \
rm -rf ./node_modules/htmltidy2/bin/darwin

# Copy source
COPY . .
# Change ownership of all files and directories
RUN chown -R node:node .
# ------------------
# 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

0 comments on commit ad1b3a6

Please sign in to comment.