-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathDockerfile
More file actions
62 lines (54 loc) · 2.82 KB
/
Copy pathDockerfile
File metadata and controls
62 lines (54 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# syntax=docker/dockerfile:1
# Stage 0: Use node image for base image for all stages
ARG NODE_VERSION=24.15.0-alpine
FROM --platform=$BUILDPLATFORM node:${NODE_VERSION} AS base
WORKDIR /usr/local/app
# Define an argument `YARN_CACHE_FOLDER` for the Yarn cache directory
ARG YARN_CACHE_FOLDER=/root/.yarn
# Stage 1: Build interfaces module
FROM base AS interfaces
COPY --link interfaces/package.json interfaces/tsconfig*.json yarn.lock ./
# Leverage a cache mount to `YARN_CACHE_FOLDER` to speed up subsequent builds
RUN --mount=type=cache,target=${YARN_CACHE_FOLDER} \
yarn install --immutable
COPY --link interfaces/src src/
RUN yarn pack
# Stage 2: Build common module
FROM base AS common
COPY --link --from=interfaces /usr/local/app/guardian-interfaces-*.tgz /tmp/interfaces.tgz
COPY --link common/package.json common/tsconfig*.json yarn.lock ./
COPY --link patches patches/
RUN node -e "const fs=require('fs'); const input=JSON.parse(fs.readFileSync('package.json')); input.dependencies['@guardian/interfaces']='file:/tmp/interfaces.tgz'; fs.writeFileSync('package.json', JSON.stringify(input));"
RUN --mount=type=cache,target=${YARN_CACHE_FOLDER} \
yarn install
COPY --link common/src src/
RUN yarn pack
# Stage 3: Installing production dependecies
FROM base AS deps
COPY --link --from=interfaces /usr/local/app/guardian-interfaces-*.tgz /tmp/interfaces.tgz
COPY --link --from=common /usr/local/app/guardian-common-*.tgz /tmp/common.tgz
COPY --link ai-service/package.json ai-service/tsconfig*.json yarn.lock ./
COPY --link patches patches/
RUN node -e "const fs=require('fs'); const input=JSON.parse(fs.readFileSync('package.json')); input.dependencies['@guardian/interfaces']='file:/tmp/interfaces.tgz'; input.dependencies['@guardian/common']='file:/tmp/common.tgz'; fs.writeFileSync('package.json', JSON.stringify(input));"
RUN --mount=type=cache,target=${YARN_CACHE_FOLDER},sharing=private \
yarn install --prod
# Stage 4: Build service
FROM base AS build
COPY --link --from=interfaces /usr/local/app/guardian-interfaces-*.tgz /tmp/interfaces.tgz
COPY --link --from=common /usr/local/app/guardian-common-*.tgz /tmp/common.tgz
COPY --link --from=deps /usr/local/app/package.json /usr/local/app/tsconfig*.json /usr/local/app/yarn.lock ./
RUN --mount=type=cache,target=${YARN_CACHE_FOLDER},sharing=private \
yarn install --immutable
COPY --link ai-service/src src/
RUN yarn run build:prod
# Stage 5: Create the final image
FROM node:${NODE_VERSION} AS image
WORKDIR /usr/local/app
ENV NODE_ENV=production
# Copy the production dependencies from the deps stage and the built application from the build stage into the image
COPY --link --from=deps /usr/local/app/node_modules node_modules/
COPY --link --from=deps /usr/local/app/package.json ./
COPY --link --from=build /usr/local/app/dist dist/
# Change the user to node
USER node
CMD [ "node", "dist/index.js" ]