backend: Create npm script package

This script creates a directory `backend/package` which only contains the
files required by the backend at runtime:
- The contents of `dist`
- `node_modules` minus `typescript` and `@typescript-eslint`.
  These packages are build-only and are larger than the remaining whole package.

By using only `backend/package` in the Docker image,
the backend content size in the image is decreased by 70% to 31M.
This, along with the improved copying in the Dockerfile, reduces the
backend image size by 44% to 200M.
(Step `RUN chown -R 1000:1000 /backend ...` created a layer that effectively
duplicated the backend.)
This commit is contained in:
Erik Arvstedt 2022-07-13 12:44:33 +02:00
parent d591f7c456
commit be72c5109a
No known key found for this signature in database
GPG Key ID: 33312B944DD97846
4 changed files with 7 additions and 8 deletions

View File

@ -24,6 +24,8 @@
"tsc": "./node_modules/typescript/bin/tsc -p tsconfig.build.json",
"build": "npm run tsc && npm run create-resources",
"create-resources": "cp ./src/tasks/price-feeds/mtgox-weekly.json ./dist/tasks && node dist/api/fetch-version.js",
"package": "npm run build && rm -rf package && mv dist package && mv node_modules package && npm run package-rm-build-deps",
"package-rm-build-deps": "(cd package/node_modules; rm -r typescript @typescript-eslint)",
"start": "node --max-old-space-size=2048 dist/index.js",
"start-production": "node --max-old-space-size=4096 dist/index.js",
"test": "./node_modules/.bin/jest --coverage",

View File

@ -9,18 +9,15 @@ COPY . .
RUN apt-get update
RUN apt-get install -y build-essential python3 pkg-config
RUN npm install --omit=dev --omit=optional
RUN npm run build
RUN npm run package
FROM node:16.16.0-buster-slim
WORKDIR /backend
COPY --from=builder /build/ .
RUN chmod +x /backend/start.sh
RUN chmod +x /backend/wait-for-it.sh
RUN chown -R 1000:1000 /backend && chmod -R 755 /backend
RUN chown 1000:1000 ./
COPY --from=builder --chown=1000:1000 /build/package ./package/
COPY --from=builder --chown=1000:1000 /build/mempool-config.json /build/start.sh /build/wait-for-it.sh ./
USER 1000

2
docker/backend/start.sh Normal file → Executable file
View File

@ -205,4 +205,4 @@ sed -i "s!__LND_REST_API_URL__!${__LND_REST_API_URL__}!g" mempool-config.json
# CLN
sed -i "s!__CLN_SOCKET__!${__CLN_SOCKET__}!g" mempool-config.json
node /backend/dist/index.js
node /backend/package/index.js

0
docker/backend/wait-for-it.sh Normal file → Executable file
View File