diff --git a/bun.lock b/bun.lock index 9e1319a73..cee6b05ce 100644 --- a/bun.lock +++ b/bun.lock @@ -197,29 +197,31 @@ "name": "dht-node", "version": "2.5.1", "dependencies": { - "@hyperswarm/dht": "^6.5.1", - "config-schema": "*", "cross-env": "^7.0.3", - "database": "*", - "dotenv": "10.0.0", - "esbuild": "^0.25.3", - "joi": "^17.13.3", - "log4js": "^6.9.1", - "tsx": "^4.19.4", - "typeorm": "^0.3.22", - "uuid": "^8.3.2", + "dht-rpc": "6.18.1", + "sodium-universal": "4.0.1", }, "devDependencies": { "@biomejs/biome": "1.9.4", + "@hyperswarm/dht": "6.5.1", "@types/dotenv": "^8.2.3", "@types/jest": "27.5.1", "@types/joi": "^17.2.3", "@types/node": "^17.0.45", "@types/uuid": "^8.3.4", + "config-schema": "*", + "database": "*", + "dotenv": "10.0.0", + "esbuild": "^0.25.3", "jest": "27.5.1", + "joi": "^17.13.3", + "log4js": "^6.9.1", "prettier": "^2.8.8", "ts-jest": "27.1.4", + "tsx": "^4.19.4", + "typeorm": "^0.3.22", "typescript": "^4.9.5", + "uuid": "^8.3.2", }, }, "federation": { @@ -266,6 +268,7 @@ "apollo-server-testing": "2.25.2", "graphql-tag": "^2.12.6", "jest": "27.2.4", + "nodemon": "^2.0.7", "prettier": "^3.5.3", "ts-jest": "27.0.5", "typescript": "^4.9.5", diff --git a/dht-node/Dockerfile b/dht-node/Dockerfile index a219600b3..ccdeb631a 100644 --- a/dht-node/Dockerfile +++ b/dht-node/Dockerfile @@ -71,15 +71,20 @@ CMD /bin/sh -c "bun install --filter dht-node --no-cache --frozen-lockfile \ && bun install --global --no-cache --no-save turbo@^2 \ && turbo dht-node#dev --env-mode=loose" +################################################################################## +# Basic Image with bun setup and project and source code ######################### +################################################################################## +FROM bun-base as bun-base-src +COPY --chown=app:app . . + ################################################################################## # BUILDER (create partly monorepo only with data needed by dht-node) ############# ################################################################################## -FROM bun-base as build +FROM bun-base-src as build -COPY --chown=app:app . . RUN bun install --filter dht-node --no-cache --frozen-lockfile \ - && bun install --global --no-cache --no-save turbo@^2 \ - && turbo dht-node#build dht-node#typecheck --env-mode=loose + && bun install --global --no-cache --no-save turbo@^2 +RUN turbo dht-node#build dht-node#typecheck --env-mode=loose ################################################################################## # TEST ########################################################################### @@ -90,11 +95,41 @@ ENV DB_HOST=mariadb # Run command CMD /bin/sh -c "turbo dht-node#test --env-mode=loose" +################################################################################## +# install only node modules needed for running bundle ############################ +################################################################################## +FROM bun-base-src as production-node-modules + +# add node_modules from production_node_modules +RUN bun install --filter dht-node --production --frozen-lockfile --no-cache \ + && rm -rf /tmp/* ~/.cache node_modules/.cache \ + && ./scripts/clean-prebuilds.sh + ################################################################################## # PRODUCTION (Does contain only "binary"- and static-files to reduce image size) # ################################################################################## FROM base as production +# Copy "binary"-files from build image +COPY --chown=app:app --from=build ${DOCKER_WORKDIR}/dht-node/build/index.js ./index.js + +# add node_modules from production_node_modules +COPY --chown=app:app --from=production-node-modules ${DOCKER_WORKDIR}/node_modules ./node_modules + +# Copy log4js-config.json to provide log configuration +COPY --chown=app:app --from=build ${DOCKER_WORKDIR}/dht-node/log4js-config.json ./log4js-config.json +ENV DB_HOST=mariadb +ENV TZ=UTC +# Run command +CMD ["node", "index.js"] + + +################################################################################## +# PRODUCTION (Does contain only "binary"- and static-files to reduce image size) # +# Experimental, install only some node_modules which can't be bundled ############ +################################################################################## +FROM base as production-small + # Copy "binary"-files from build image COPY --chown=app:app --from=build ${DOCKER_WORKDIR}/dht-node/build/index.js ./index.js COPY --chown=app:app --from=build ${DOCKER_WORKDIR}/scripts/clean-prebuilds.sh ./clean-prebuilds.sh diff --git a/dht-node/README.md b/dht-node/README.md index 88a2ca570..3affa9a49 100644 --- a/dht-node/README.md +++ b/dht-node/README.md @@ -7,3 +7,11 @@ Bun crashes when a NAPI module tries to call `uv_interface_addresses`, a libuv function currently unsupported: Bun is working hard to support all NAPI module calls + +## Production Build +Package.json dependencies contain only node_modules which cannot be bundled because of native node modules or needed for run start script. They are manually picked from @hyperswarm/dht +dependencies. The versions should be updated, if @hyperswarm/dht is updated. +The goal is to get a really small footprint for the production image. It is also possible to use in bare_metal setup. + +### Bare Metal minimal setup +For a minimal bare metal production setup, look into [Dockerfile](Dockerfile) in the production step. diff --git a/dht-node/esbuild.config.ts b/dht-node/esbuild.config.ts index 8e0c6583a..f38039c43 100644 --- a/dht-node/esbuild.config.ts +++ b/dht-node/esbuild.config.ts @@ -8,7 +8,7 @@ build({ bundle: true, keepNames: true, // legalComments: 'inline', - external: ['udx-native', 'sodium-native'], + external: ['dht-rpc', 'sodium-universal'], minify: true, sourcemap: false, }) diff --git a/dht-node/package.json b/dht-node/package.json index 7e604ee73..4e7224b4a 100644 --- a/dht-node/package.json +++ b/dht-node/package.json @@ -17,29 +17,31 @@ "test": "cross-env TZ=UTC NODE_ENV=development DB_DATABASE=gradido_test_dht jest --runInBand --forceExit --detectOpenHandles" }, "dependencies": { - "@hyperswarm/dht": "^6.5.1", - "config-schema": "*", - "cross-env": "^7.0.3", - "database": "*", - "dotenv": "10.0.0", - "joi": "^17.13.3", - "log4js": "^6.9.1", - "typeorm": "^0.3.22", - "uuid": "^8.3.2" + "dht-rpc": "6.18.1", + "sodium-universal": "4.0.1", + "cross-env": "^7.0.3" }, "devDependencies": { "@biomejs/biome": "1.9.4", + "@hyperswarm/dht": "6.5.1", "@types/dotenv": "^8.2.3", "@types/jest": "27.5.1", "@types/joi": "^17.2.3", "@types/node": "^17.0.45", "@types/uuid": "^8.3.4", + "config-schema": "*", + "database": "*", + "dotenv": "10.0.0", "esbuild": "^0.25.3", "jest": "27.5.1", + "joi": "^17.13.3", + "log4js": "^6.9.1", "prettier": "^2.8.8", "ts-jest": "27.1.4", "tsx": "^4.19.4", - "typescript": "^4.9.5" + "typeorm": "^0.3.22", + "typescript": "^4.9.5", + "uuid": "^8.3.2" }, "engines": { "node": ">=18"