|
| 1 | +# Stage 1 - Create yarn install skeleton layer |
| 2 | +FROM node:18-bookworm-slim AS packages |
| 3 | + |
| 4 | +WORKDIR /app |
| 5 | +COPY package.json yarn.lock ./ |
| 6 | + |
| 7 | +COPY packages packages |
| 8 | + |
| 9 | +# Comment this out if you don't have any internal plugins |
| 10 | +# COPY plugins plugins |
| 11 | + |
| 12 | +RUN find packages \! -name "package.json" -mindepth 2 -maxdepth 2 -exec rm -rf {} \+ |
| 13 | + |
| 14 | +# Stage 2 - Install dependencies and build packages |
| 15 | +FROM node:18-bookworm-slim AS build |
| 16 | + |
| 17 | +# Install isolate-vm dependencies, these are needed by the @backstage/plugin-scaffolder-backend. |
| 18 | +RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ |
| 19 | + --mount=type=cache,target=/var/lib/apt,sharing=locked \ |
| 20 | + apt-get update && \ |
| 21 | + apt-get install -y --no-install-recommends python3 g++ build-essential && \ |
| 22 | + yarn config set python /usr/bin/python3 |
| 23 | + |
| 24 | +USER node |
| 25 | +WORKDIR /app |
| 26 | + |
| 27 | +COPY --from=packages --chown=node:node /app . |
| 28 | + |
| 29 | +RUN --mount=type=cache,target=/home/node/.cache/yarn,sharing=locked,uid=1000,gid=1000 \ |
| 30 | + yarn install --frozen-lockfile --network-timeout 600000 |
| 31 | + |
| 32 | +COPY --chown=node:node . . |
| 33 | + |
| 34 | +RUN yarn tsc |
| 35 | +RUN yarn --cwd packages/backend build |
| 36 | +# If you have not yet migrated to package roles, use the following command instead: |
| 37 | +# RUN yarn --cwd packages/backend backstage-cli backend:bundle --build-dependencies |
| 38 | + |
| 39 | +RUN mkdir packages/backend/dist/skeleton packages/backend/dist/bundle \ |
| 40 | + && tar xzf packages/backend/dist/skeleton.tar.gz -C packages/backend/dist/skeleton \ |
| 41 | + && tar xzf packages/backend/dist/bundle.tar.gz -C packages/backend/dist/bundle |
| 42 | + |
| 43 | +# Stage 3 - Build the actual backend image and install production dependencies |
| 44 | +FROM node:18-bookworm-slim |
| 45 | + |
| 46 | +# Install isolate-vm dependencies, these are needed by the @backstage/plugin-scaffolder-backend. |
| 47 | +RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \ |
| 48 | + --mount=type=cache,target=/var/lib/apt,sharing=locked \ |
| 49 | + apt-get update && \ |
| 50 | + apt-get install -y --no-install-recommends python3 g++ build-essential && \ |
| 51 | + yarn config set python /usr/bin/python3 |
| 52 | + |
| 53 | +# From here on we use the least-privileged `node` user to run the backend. |
| 54 | +USER node |
| 55 | + |
| 56 | +# This should create the app dir as `node`. |
| 57 | +# If it is instead created as `root` then the `tar` command below will |
| 58 | +# fail: `can't create directory 'packages/': Permission denied`. |
| 59 | +# If this occurs, then ensure BuildKit is enabled (`DOCKER_BUILDKIT=1`) |
| 60 | +# so the app dir is correctly created as `node`. |
| 61 | +WORKDIR /app |
| 62 | + |
| 63 | +# Copy the install dependencies from the build stage and context |
| 64 | +COPY --from=build --chown=node:node /app/yarn.lock /app/package.json /app/packages/backend/dist/skeleton/ ./ |
| 65 | + |
| 66 | +RUN --mount=type=cache,target=/home/node/.cache/yarn,sharing=locked,uid=1000,gid=1000 \ |
| 67 | + yarn install --frozen-lockfile --production --network-timeout 600000 |
| 68 | + |
| 69 | +# Copy the built packages from the build stage |
| 70 | +COPY --from=build --chown=node:node /app/packages/backend/dist/bundle/ ./ |
| 71 | + |
| 72 | +# Copy any other files that we need at runtime |
| 73 | +COPY --chown=node:node app-config.yaml ./ |
| 74 | + |
| 75 | +# This switches many Node.js dependencies to production mode. |
| 76 | +ENV NODE_ENV production |
| 77 | + |
| 78 | +CMD ["node", "packages/backend", "--config", "app-config.yaml"] |
0 commit comments