99#
1010# Once the commands have been run, you can build the image using `yarn build-image`
1111
12- # --- Builder: install prod deps and unpack the bundle
13- FROM node:22-bookworm-slim AS build
12+ # From mise.toml
13+ FROM node:22-bookworm-slim
1414
1515# Set Python interpreter for `node-gyp` to use
1616ENV PYTHON=/usr/bin/python3
1717
18- # enable Corepack + activate yarn@4.9.1 before dropping privileges
19- RUN corepack enable && corepack prepare yarn@4.9.1 --activate
18+ RUN groupmod -g 150 node && usermod -u 150 -g 150 node
19+ RUN corepack enable
20+ # Set the owner of the cache directory to node so we can use corepack
21+ RUN mkdir -p /home/node/.cache && chown -R node:node /home/node/.cache
2022
2123# Install isolate-vm dependencies, these are needed by the @backstage/plugin-scaffolder-backend.
24+ # If sqlite3 is not needed anymore, remove libsqlite3-dev and better-sqlite3.
2225RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
2326 --mount=type=cache,target=/var/lib/apt,sharing=locked \
2427 apt-get update && \
25- apt-get install -y --no-install-recommends python3 g++ build-essential && \
28+ apt-get install -y --no-install-recommends python3 g++ build-essential libsqlite3-dev && \
2629 rm -rf /var/lib/apt/lists/*
2730
28- # Install sqlite3 dependencies. You can skip this if you don't use sqlite3 in the image,
29- # in which case you should also move better-sqlite3 to "devDependencies" in package.json.
30- RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
31- --mount=type=cache,target=/var/lib/apt,sharing=locked \
32- apt-get update && \
33- apt-get install -y --no-install-recommends libsqlite3-dev && \
34- rm -rf /var/lib/apt/lists/*
3531
36- RUN mkdir -p /home/node/.cache
32+ # From here on we use the least-privileged `node` user to run the backend.
33+ USER node
3734
3835# This should create the app dir as `node`.
3936# If it is instead created as `root` then the `tar` command below will fail: `can't create directory 'packages/': Permission denied`.
4037# If this occurs, then ensure BuildKit is enabled (`DOCKER_BUILDKIT=1`) so the app dir is correctly created as `node`.
4138WORKDIR /app
4239
4340# Copy files needed by Yarn
44- COPY .yarn ./.yarn
45- COPY .yarnrc.yml ./
46- COPY backstage.json ./
41+ COPY --chown=node:node .yarn ./.yarn
42+ COPY --chown=node:node .yarnrc.yml ./
43+ COPY --chown=node:node backstage.json ./
4744
4845# This switches many Node.js dependencies to production mode.
4946ENV NODE_ENV=production
@@ -55,23 +52,17 @@ ENV NODE_OPTIONS="--no-node-snapshot"
5552# Copy repo skeleton first, to avoid unnecessary docker cache invalidation.
5653# The skeleton contains the package.json of each package in the monorepo,
5754# and along with yarn.lock and the root package.json, that's enough to run yarn install.
58- COPY yarn.lock package.json packages/backend/dist/skeleton.tar.gz ./
55+ COPY --chown=node:node yarn.lock package.json packages/backend/dist/skeleton.tar.gz ./
5956RUN tar xzf skeleton.tar.gz && rm skeleton.tar.gz
6057
61- RUN --mount=type=cache,target=/home/node/.cache,sharing=locked,uid=1000,gid=1000 \
62- yarn workspaces focus -A --production && yarn cache clean
58+ RUN --mount=type=cache,target=/home/node/.cache/yarn ,sharing=locked,uid=1000,gid=1000 \
59+ yarn workspaces focus --all --production && rm -rf "$( yarn cache clean)"
6360
6461# Then copy the rest of the backend bundle, along with any other files we might want.
65- COPY packages/backend/dist/bundle.tar.gz app-config*.yaml ./
62+ COPY --chown=node:node packages/backend/dist/bundle.tar.gz app-config*.yaml ./
6663RUN tar xzf bundle.tar.gz && rm bundle.tar.gz
6764
68- # --- Runtime: distroless NodeJS
69- FROM gcr.io/distroless/nodejs22-debian12
70- ENV NODE_ENV=production
71- ENV NODE_OPTIONS="--no-node-snapshot"
72- WORKDIR /app
73-
74- COPY package.json app-config*.yaml ./
75- COPY --from=build /app /app
65+ RUN mv packages packages_tmp
66+ RUN mkdir packages
7667
77- CMD ["packages/backend" , "--config" , " app-config.yaml" , " --config" , " app-config.production.yaml" , " --config" , " app-config.runtime.yaml" ]
68+ CMD ["sh" , "-c" , "cp -r packages_tmp/* packages/ && node packages/backend --config app-config.yaml --config app-config.production.yaml --config app-config.runtime.yaml" ]
0 commit comments