-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathDockerfile
72 lines (54 loc) · 2.22 KB
/
Dockerfile
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
63
64
65
66
67
68
69
70
71
72
# syntax=docker/dockerfile:1.5
# When updating image version, make sure to update below layer as well
FROM node:23.5.0-alpine3.20 as builder
# Create app directory
WORKDIR /app
# Dev dependencies for building any local packages
RUN <<EOF
apk update --no-cache
apk add git # Fetch some packages
apk add python3 # Some node gyp bindings require Python
apk add make g++ # Standard tools for building native extensions
npm install -g node-gyp # Compile native extensions
EOF
# Re-install packages if there were any changes
COPY package.json yarn.lock ./
RUN <<EOF
# Install development node modules so we can build the project, but we'll
# eventually throw these away in favor of the trimmed production node_modules
yarn install && mv node_modules node_modules-development
# Install only production node_modules and prune them so we're shipping as
# small an image as possible. This will reuse the local cache of packages that
# yarn already downloaded from the installation of development modules above.
yarn install --production --prefer-offline
# Move production node modules out of the directory so the build step doesn't
# accidentally try to use it.
mv node_modules ../node_modules-production
# Swap production node modules with the development so we can build the app
mv node_modules-development node_modules
EOF
# Copy the application code after installing so we benefit from the layer cache
COPY . .
# Build the application so we can distribute
RUN yarn build
# When updating image version, make sure to update the above layer as well
FROM node:23.5.0-alpine3.20 as app
WORKDIR /app
RUN <<EOF
# Requirement for Datadog runtime metrics integration
apk add libc6-compat
apk add curl
EOF
# Copy all packages including compiled extensions
COPY --from=builder /node_modules-production node_modules
# Copy essential source code
COPY --from=builder /app/build .
COPY ./pm2.config.js /app/pm2.config.cjs
# Dummy value to get ESM detection to work
RUN echo '{"type":"module"}' > package.json
# BuildKit doesn't support the --squash flag, so we emulate it
# copying everything into a single layer.
FROM scratch
COPY --from=app / /
WORKDIR /app
CMD ["npx", "pm2-runtime", "pm2.config.cjs"]