forked from farcasterxyz/hub-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile.replicator
94 lines (68 loc) · 3.56 KB
/
Dockerfile.replicator
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Builds a Docker image for the replicator. Allows us to include custom changes
# from other packages the replicator depends on (e.g. hub-nodejs) to test those
# in isolation.
###############################################################################
############## Stage 1: Create pruned version of monorepo #####################
###############################################################################
FROM node:20.8-alpine AS prune
RUN apk add --no-cache libc6-compat
USER node
RUN mkdir /home/node/app
WORKDIR /home/node/app
# Run turbo prune to create a pruned version of monorepo
COPY --chown=node:node ./package.json ./package.json
RUN yarn global add turbo@$(node -e "console.log(require('./package.json').devDependencies.turbo)")
COPY --chown=node:node . .
RUN /home/node/.yarn/bin/turbo prune --scope=@farcaster/replicator --docker
###############################################################################
############## Stage 2: Build the code using a full node image ################
###############################################################################
FROM node:20.8-alpine AS build
# Needed for compilation step
RUN apk add --no-cache libc6-compat python3 make g++ linux-headers curl
USER node
RUN mkdir /home/node/app
WORKDIR /home/node/app
# Install Rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain 1.70.0
ENV PATH="/home/node/.cargo/bin:${PATH}"
# Rust flags to allow building with musl, which is needed for alpine
ENV RUSTFLAGS="-C target-feature=-crt-static"
# Copy dependency information and install all dependencies
COPY --chown=node:node --from=prune /home/node/app/out/json/ .
COPY --chown=node:node --from=prune /home/node/app/out/yarn.lock ./yarn.lock
RUN yarn install --frozen-lockfile --network-timeout 1800000
# Copy source code (and all other relevant files)
COPY --chown=node:node --from=prune /home/node/app/out/full/ .
# turbo prune doesn't include global tsconfig.json (https://github.com/vercel/turbo/issues/2177)
COPY --chown=node:node tsconfig.json tsconfig.json
# Build code
RUN yarn build
# Purge dev dependencies and only install production dependencies
RUN rm -rf node_modules && yarn install --production --ignore-scripts --prefer-offline --force --frozen-lockfile
###############################################################################
########## Stage 3: Copy over the built code to a leaner alpine image #########
###############################################################################
FROM node:20.8-alpine as app
# Requirement for runtime metrics integrations
RUN apk add libc6-compat
# Set non-root user
USER node
# Many npm packages use this to trigger production optimized behaviors
ENV NODE_ENV production
RUN mkdir /home/node/app
WORKDIR /home/node/app
# Copy results from previous stage.
# The base image is same as the build stage, so it is safe to copy node_modules over to this stage.
COPY --chown=node:node --from=prune /home/node/app/out/json/ .
COPY --chown=node:node --from=build /home/node/app/apps/replicator/build ./apps/replicator/build
COPY --chown=node:node --from=build /home/node/app/node_modules ./node_modules
COPY --chown=node:node --from=build /home/node/app/packages/core/dist ./packages/core/dist/
COPY --chown=node:node --from=build /home/node/app/packages/hub-nodejs/dist ./packages/hub-nodejs/dist/
# BuildKit doesn't support --squash flag, so emulate by copying into fewer layers
FROM scratch
COPY --from=app / /
# Repeat of above since it is lost between build stages
USER node
WORKDIR /home/node/app/apps/replicator
CMD ["node", "build/app.js"]