feat: create docker images for client (#51289)

This commit is contained in:
Oliver Eyton-Williams
2023-08-31 17:37:32 +02:00
committed by GitHub
parent 8f6809686e
commit 66d7c28f99
8 changed files with 267 additions and 23 deletions

View File

@@ -1,28 +1,51 @@
FROM node:16-buster AS builder
# Install doppler CLI
RUN (curl -Ls --tlsv1.2 --proto "=https" --retry 3 https://cli.doppler.com/install.sh) | sh -s -- --verify-signature
# bookworm was only released on 10-6-2023, so is a little too new.
FROM node:18-bullseye AS builder
# global installs need root permissions, so have to happen before we switch to
# the node user
RUN npm i -g pnpm@8
# node images create a non-root user that we can use
USER node
WORKDIR /home/node/build
COPY --chown=node:node . .
# Pass `DOPPLER_TOKEN` at build time to create an encrypted snapshot for high-availability
ARG DOPPLER_TOKEN
RUN \
doppler secrets download doppler.encrypted.json &&\
# Install and donot ignore the scripts for sharp
pnpm install --no-progress --ignore-scripts=false &&\
doppler run --fallback=doppler.encrypted.json --command="npm run create:config" &&\
doppler run --fallback=doppler.encrypted.json --command="npm run build:curriculum" &&\
doppler run --fallback=doppler.encrypted.json --command="npm run build:client"
# Use a lightweight image for the serving the files
FROM node:16-alpine
RUN npm i -g serve@13
ARG HOME_LOCATION
ARG API_LOCATION
ARG FORUM_LOCATION
ARG NEWS_LOCATION
ARG RADIO_LOCATION
ARG CLIENT_LOCALE
ARG CURRICULUM_LOCALE
ARG SHOW_LOCALE_DROPDOWN_MENU
ARG ALGOLIA_APP_ID
ARG ALGOLIA_API_KEY
ARG STRIPE_PUBLIC_KEY
ARG PAYPAL_CLIENT_ID
ARG PATREON_CLIENT_ID
ARG DEPLOYMENT_ENV
ARG SHOW_UPCOMING_CHANGES
ARG SHOW_NEW_CURRICULUM
ARG GROWTHBOOK_URI
ARG FREECODECAMP_NODE_ENV
# We're installing specific packages even though it is not strictly necessary -
# pnpm install would work. The idea is to make the dependencies explicit and
# keep them under our control.
RUN pnpm config set dedupe-peer-dependents false
# Scripts need to be run at this stage (--ignore-scripts cannot be used) because
# without them, Gatsby will not install sharp.
RUN pnpm install -w -F=client -F=ui -F=browser-scripts -F=challenge-parser \
--frozen-lockfile
RUN pnpm build:client
FROM node:18-alpine
RUN npm i -g serve@13 pm2@4
USER node
WORKDIR /home/node
COPY --from=builder /home/node/build/client/public/ client/public
COPY --from=builder /home/node/build/docker/web/serve.json client/serve.json
WORKDIR /home/node/client
COPY --from=builder /home/node/build/client/public/ public
COPY --from=builder /home/node/build/docker/web/serve.sh serve.sh
COPY --from=builder /home/node/build/docker/web/pm2-start.sh pm2-start.sh
COPY --from=builder /home/node/build/docker/web/serve.json serve.json
EXPOSE 8000
CMD ["serve", "--config", "client/serve.json", "--cors", "--no-clipboard", "--no-port-switching", "-p", "8000", "client/public"]
ENTRYPOINT [ "./pm2-start.sh" ]
CMD [ "8000" ]

2
docker/web/pm2-start.sh Executable file
View File

@@ -0,0 +1,2 @@
#!/bin/sh
pm2-runtime start -i 0 --interpreter=sh ./serve.sh --name client-primary -- $1

View File

@@ -1,5 +1,43 @@
{
"directoryListing": false,
"headers": [
{
"source": "{**/*.html,**/app-data.json,**/page-data.json}",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=0, must-revalidate"
}
]
},
{
"source": "**/*-@(????????????????????????????????|????????????????????).@(js|woff|ttf|svg|png)",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=172800, immutable"
}
]
},
{
"source": "{misc/*.js,sw.js}",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=0, must-revalidate"
}
]
},
{
"source": "{js/sass.sync.js,css/bootstrap.min.css}",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=14400, stale-while-revalidate=172800, must-revalidate"
}
]
}
],
"trailingSlash": false,
"rewrites": [
{

2
docker/web/serve.sh Executable file
View File

@@ -0,0 +1,2 @@
#!/bin/sh
serve -c ../serve.json -p $1 public