Skip to content

Commit

Permalink
build(dockerfile): use multi-stage build; reduce size by ~28% (#1838)
Browse files Browse the repository at this point in the history
* build(dockerfile): use multi-stage build; reduce size by ~28%

* build(dockerfile): cache dependencies
  • Loading branch information
Fdawgs committed Apr 30, 2024
1 parent 6071a2d commit b354294
Showing 1 changed file with 31 additions and 16 deletions.
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

0 comments on commit b354294

Please sign in to comment.