From 4effd8ebe1ce42cda792bacb9e26518454ec3767 Mon Sep 17 00:00:00 2001 From: einhornimmond Date: Wed, 30 Apr 2025 14:44:56 +0200 Subject: [PATCH] fix docker setup, use single container as default option --- Dockerfile | 161 ++++++++++++++++++++++++++++++++++++ backend/Dockerfile | 2 +- bun.lock | 4 +- dht-node/Dockerfile | 2 +- docker-compose.override.yml | 45 +++++++++- docker-compose.yml | 63 ++++++++++++-- federation/Dockerfile | 4 +- nginx/gradido.conf | 8 +- package.json | 3 +- yarn.lock | 14 +--- 10 files changed, 272 insertions(+), 34 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..5d714b472 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,161 @@ +################################################################################## +# BASE ########################################################################### +################################################################################## +FROM node:18.20.7-bookworm as base + +# ENVs (available in production aswell, can be overwritten by commandline or env file) +ENV TURBO_CACHE_DIR=/tmp/turbo +## DOCKER_WORKDIR would be a classical ARG, but that is not multi layer persistent - shame +ENV DOCKER_WORKDIR="/app" +## We Cannot do `$(date -u +'%Y-%m-%dT%H:%M:%SZ')` here so we use unix timestamp=0 +ENV BUILD_DATE="1970-01-01T00:00:00.00Z" +## We cannot do $(npm run version).${BUILD_NUMBER} here so we default to 0.0.0.0 +ENV BUILD_VERSION="0.0.0.0" +## We cannot do `$(git rev-parse --short HEAD)` here so we default to 0000000 +ARG BUILD_COMMIT +ENV BUILD_COMMIT=${BUILD_COMMIT} +## SET NODE_ENV +ENV NODE_ENV="production" +## App relevant Envs +ENV BACKEND_PORT="4000" +ENV FEDERATION_PORT="5010" +ENV FRONTEND_MODULE_PORT="3000" +ENV ADMIN_MODULE_PORT="8080" + +# Labels +LABEL org.label-schema.build-date="${BUILD_DATE}" +LABEL org.label-schema.name="gradido:backend" +LABEL org.label-schema.description="Gradido GraphQL Backend" +LABEL org.label-schema.usage="https://github.com/gradido/gradido/blob/master/README.md" +LABEL org.label-schema.url="https://gradido.net" +LABEL org.label-schema.vcs-url="https://github.com/gradido/gradido/tree/master/backend" +LABEL org.label-schema.vcs-ref="${BUILD_COMMIT}" +LABEL org.label-schema.vendor="Gradido Community" +LABEL org.label-schema.version="${BUILD_VERSION}" +LABEL org.label-schema.schema-version="1.0" +LABEL maintainer="support@gradido.net" + +# Install Additional Software +## install: git +#apk add --no-cache libc6-compat +#RUN apk --no-cache add git +# Install bun +# RUN apt-get update && apt-get install -y curl unzip +RUN curl -fsSL https://bun.sh/install | BUN_INSTALL=/usr/local bash +# Add bun to PATH +# Install turbo globally +RUN bun install --global turbo +# Add bun's global bin directory to PATH +ENV PATH="/root/.bun/bin:${PATH}" + +#RUN yarn global add turbo + +# Settings +## Expose Container Port +EXPOSE ${BACKEND_PORT} +EXPOSE ${FEDERATION_PORT} +EXPOSE ${FRONTEND_MODULE_PORT} +EXPOSE ${ADMIN_MODULE_PORT} + +## Workdir +RUN mkdir -p ${DOCKER_WORKDIR} +WORKDIR ${DOCKER_WORKDIR} + +################################################################################## +# DEVELOPMENT (Connected to the local environment, to reload on demand) ########## +################################################################################## +FROM base as development + +# We don't need to copy or build anything since we gonna bind to the +# local filesystem which will need a rebuild anyway + +# Run command +# (for development we need to execute yarn install since the +# node_modules are on another volume and need updating) +CMD /bin/sh -c "bun install && turbo dev --env-mode=loose" + +################################################################################## +# INSTALL (Does contain all node_modules) ######################################## +################################################################################## +FROM base as install + +# Copy everything +COPY --chown=app:app ./ ./ + +# yarn install +RUN bun install --frozen-lockfile --non-interactive + +# try with bun, use yarn if problems occur +# go into admin folder and use yarn to install local dependencies which need to use nohoist for @vee-validate/i18n which isn't supported by bun +#RUN bun install --frozen-lockfile + + +################################################################################## +# TEST ########################################################################### +################################################################################## +FROM install as test + +# Run command +CMD /bin/sh -c "turbo test --env-mode=loose" + +################################################################################## +# RESET DB ####################################################################### +################################################################################## +FROM install as reset + +# Run command +CMD /bin/sh -c "cd database && bun run reset" + +################################################################################## +# BUILD (Does contain all files and is therefore bloated) ######################## +################################################################################## +FROM install as build + +# turbo build +RUN turbo build --env-mode=loose + +################################################################################## +# PRODUCTION ##################################################################### +################################################################################## +FROM build as production + +# Run command +CMD /bin/sh -c "turbo start --env-mode=loose" + +################################################################################## +# FINAL PRODUCTION IMAGE ######################################################### +################################################################################## +FROM node:18.20.7-bookworm-slim as production2 + +ENV TURBO_CACHE_DIR=/tmp/turbo +ENV DOCKER_WORKDIR="/app" +ENV NODE_ENV="production" +ENV DB_HOST=mariadb +WORKDIR ${DOCKER_WORKDIR} + +# Copy only the build artifacts from the previous build stage +COPY --chown=app:app --from=build /app/node_modules ./node_modules +COPY --chown=app:app --from=build /app/package.json ./package.json +COPY --chown=app:app --from=build /app/yarn.lock ./yarn.lock +COPY --chown=app:app --from=build /app/turbo.json ./turbo.json +# and Turbo cache to prevent rebuilding +COPY --chown=app:app --from=build /tmp/turbo ./tmp/turbo + +RUN yarn global add turbo + +COPY --chown=app:app --from=build /app/backend ./backend +COPY --chown=app:app --from=build /app/frontend ./frontend +COPY --chown=app:app --from=build /app/admin ./admin +COPY --chown=app:app --from=build /app/database ./database +COPY --chown=app:app --from=build /app/config ./config +COPY --chown=app:app --from=build /app/federation ./federation +COPY --chown=app:app --from=build /app/dht-node ./dht-node + +# Ports exposen +EXPOSE ${BACKEND_PORT} +EXPOSE ${FEDERATION_PORT} +EXPOSE ${FRONTEND_MODULE_PORT} +EXPOSE ${ADMIN_MODULE_PORT} + +# Command to start +CMD ["turbo", "start", "--env-mode=loose"] \ No newline at end of file diff --git a/backend/Dockerfile b/backend/Dockerfile index d83be1c16..4cca6c1ea 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -94,7 +94,7 @@ CMD /bin/sh -c "turbo backend#test --env-mode=loose" FROM base as production # Copy "binary"-files from build image -COPY --chown=app:app --from=installer ${DOCKER_WORKDIR}/backend/build/index.js ./index.js +COPY --chown=app:app --from=installer ${DOCKER_WORKDIR}/backend/build/src/index.js ./index.js # Copy log4js-config.json to provide log configuration COPY --chown=app:app --from=installer ${DOCKER_WORKDIR}/backend/log4js-config.json ./log4js-config.json diff --git a/bun.lock b/bun.lock index b4203cb05..ba2d1a6d4 100644 --- a/bun.lock +++ b/bun.lock @@ -250,7 +250,7 @@ }, "devDependencies": { "@biomejs/biome": "1.9.4", - "@types/express": "4.17.12", + "@types/express": "4.17.21", "@types/jest": "27.0.2", "@types/lodash.clonedeep": "^4.5.6", "@types/node": "^17.0.21", @@ -3336,8 +3336,6 @@ "fast-glob/glob-parent": ["glob-parent@5.1.2", "", { "dependencies": { "is-glob": "^4.0.1" } }, "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow=="], - "federation/@types/express": ["@types/express@4.17.12", "", { "dependencies": { "@types/body-parser": "*", "@types/express-serve-static-core": "^4.17.18", "@types/qs": "*", "@types/serve-static": "*" } }, "sha512-pTYas6FrP15B1Oa0bkN5tQMNqOcVXa9j4FTFtO8DWI9kppKib+6NJtfTOOLcwxuuYvcX2+dVG6et1SxW/Kc17Q=="], - "federation/apollo-server-testing": ["apollo-server-testing@2.25.2", "", { "dependencies": { "apollo-server-core": "^2.25.2" }, "peerDependencies": { "graphql": "^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0" } }, "sha512-HjQV9wPbi/ZqpRbyyhNwCbaDnfjDM0hTRec5TOoOjurEZ/vh4hTPHwGkDZx3kbcWowhGxe2qoHM6KANSB/SxuA=="], "federation/helmet": ["helmet@7.2.0", "", {}, "sha512-ZRiwvN089JfMXokizgqEPXsl2Guk094yExfoDXR0cBYWxtBbaSww/w+vT4WEJsBW2iTUi1GgZ6swmoug3Oy4Xw=="], diff --git a/dht-node/Dockerfile b/dht-node/Dockerfile index 12bc64f58..d1406a9c4 100644 --- a/dht-node/Dockerfile +++ b/dht-node/Dockerfile @@ -94,7 +94,7 @@ CMD /bin/sh -c "turbo dht-node#test --env-mode=loose" FROM base as production # Copy "binary"-files from build image -COPY --chown=app:app --from=installer ${DOCKER_WORKDIR}/dht-node/build/index.js ./index.js +COPY --chown=app:app --from=installer ${DOCKER_WORKDIR}/dht-node/build/src/index.js ./index.js # We also install the native node_modules which cannot be bundled # TODO: find a elegant way to use the right versions from yarn.lock diff --git a/docker-compose.override.yml b/docker-compose.override.yml index aa818ab0b..1c72ea47e 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -1,5 +1,43 @@ services: + ######################################################## + # Gradido ############################################## + ######################################################## + gradido: + image: gradido/gradido:local-development + build: + context: ./ + dockerfile: ./Dockerfile + target: development + args: + BUILD_COMMIT: ${BUILD_COMMIT} + BUILD_COMMIT_SHORT: ${BUILD_COMMIT_SHORT} + BUILD_VERSION: ${BUILD_VERSION} + depends_on: + - mariadb + networks: + - internal-net + - external-net + ports: + - ${BACKEND_PORT:-4000}:${BACKEND_PORT:-4000} + - ${FEDERATION_PORT:-5010}:${FEDERATION_PORT:-5010} + - ${FRONTEND_MODULE_PORT:-3000}:${FRONTEND_MODULE_PORT:-3000} + - ${ADMIN_MODULE_PORT:-8080}:${ADMIN_MODULE_PORT:-8080} + environment: + # Envs used in Dockerfile + # - DOCKER_WORKDIR="/app" + # - PORT=4000 + - BUILD_DATE + - BUILD_VERSION + - BUILD_COMMIT + - NODE_ENV=production + - DB_HOST=mariadb + volumes: + - ./logs/backend:/logs/backend + - gradido_node_modules:/app/node_modules + - .:/app + + ######################################################## # FRONTEND ############################################# ######################################################## @@ -196,6 +234,8 @@ services: ######################################################### phpmyadmin: image: phpmyadmin + profiles: + - debug environment: - PMA_ARBITRARY=1 #restart: always @@ -212,6 +252,8 @@ services: ######################################################## mailserver: image: maildev/maildev + profiles: + - debug ports: - 1080:1080 - 1025:1025 @@ -219,8 +261,9 @@ services: - external-net volumes: - frontend_node_modules: admin_node_modules: + frontend_node_modules: + gradido_node_modules: backend_node_modules: backend_database_node_modules: backend_database_build: diff --git a/docker-compose.yml b/docker-compose.yml index 4d5423a04..795d9003c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,6 +5,41 @@ services: + ######################################################## + # Gradido ############################################## + ######################################################## + gradido: + image: gradido/gradido:local-production + build: + context: ./ + dockerfile: ./Dockerfile + target: production + args: + BUILD_COMMIT: ${BUILD_COMMIT} + BUILD_COMMIT_SHORT: ${BUILD_COMMIT_SHORT} + BUILD_VERSION: ${BUILD_VERSION} + depends_on: + - mariadb + networks: + - internal-net + - external-net + ports: + - ${BACKEND_PORT:-4000}:${BACKEND_PORT:-4000} + - ${FEDERATION_PORT:-5010}:${FEDERATION_PORT:-5010} + - ${FRONTEND_MODULE_PORT:-3000}:${FRONTEND_MODULE_PORT:-3000} + - ${ADMIN_MODULE_PORT:-8080}:${ADMIN_MODULE_PORT:-8080} + environment: + # Envs used in Dockerfile + # - DOCKER_WORKDIR="/app" + # - PORT=4000 + - BUILD_DATE + - BUILD_VERSION + - BUILD_COMMIT + - NODE_ENV=production + - DB_HOST=mariadb + volumes: + - ./logs:/logs + ######################################################## # FRONTEND ############################################# ######################################################## @@ -19,6 +54,8 @@ services: BUILD_COMMIT: ${BUILD_COMMIT} BUILD_COMMIT_SHORT: ${BUILD_COMMIT_SHORT} BUILD_VERSION: ${BUILD_VERSION} + profiles: + - single networks: - external-net - internal-net @@ -32,8 +69,6 @@ services: # - BUILD_VERSION="0.0.0.0" # - BUILD_COMMIT="0000000" - NODE_ENV=production - volumes: - - ./config:/config # env_file: # - ./.env # - ./frontend/.env @@ -52,6 +87,8 @@ services: BUILD_COMMIT: ${BUILD_COMMIT} BUILD_COMMIT_SHORT: ${BUILD_COMMIT_SHORT} BUILD_VERSION: ${BUILD_VERSION} + profiles: + - single networks: - external-net - internal-net @@ -65,8 +102,6 @@ services: # - BUILD_VERSION="0.0.0.0" # - BUILD_COMMIT="0000000" - NODE_ENV=production - volumes: - - ./config:/config # env_file: # - ./.env # - ./admin/.env @@ -98,6 +133,8 @@ services: context: ./ dockerfile: ./backend/Dockerfile target: production + profiles: + - single networks: - internal-net ports: @@ -132,6 +169,8 @@ services: context: ./ dockerfile: ./dht-node/Dockerfile target: production + profiles: + - single networks: - internal-net - external-net @@ -164,9 +203,11 @@ services: build: # since we have to include the entities from ./database we cannot define the context as ./backend # this might blow build image size to the moon ?! - context: ./dlt-connector - dockerfile: ./Dockerfile + context: . + dockerfile: ./dlt-connector/Dockerfile target: production + profiles: + - dlt networks: - internal-net - external-net @@ -198,6 +239,8 @@ services: context: ./ dockerfile: ./federation/Dockerfile target: production + profiles: + - single networks: - internal-net - external-net @@ -231,6 +274,8 @@ services: context: . dockerfile: ./database/Dockerfile target: up + profiles: + - database depends_on: - mariadb networks: @@ -257,6 +302,8 @@ services: build: context: ./dlt-database target: production_up + profiles: + - dlt depends_on: - mariadb networks: @@ -284,9 +331,7 @@ services: - external-net - internal-net depends_on: - - frontend - - backend - - admin + - gradido ports: - 80:80 volumes: diff --git a/federation/Dockerfile b/federation/Dockerfile index eabc602d9..255821a92 100644 --- a/federation/Dockerfile +++ b/federation/Dockerfile @@ -93,10 +93,10 @@ CMD /bin/sh -c "turbo federation#test --env-mode=loose" FROM base as production # Copy "binary"-files from build image -COPY --chown=app:app --from=installer ${DOCKER_WORKDIR}/dht-node/build/index.js ./index.js +COPY --chown=app:app --from=installer ${DOCKER_WORKDIR}/federation/build/src/index.js ./index.js # Copy log4js-config.json to provide log configuration -COPY --chown=app:app --from=installer ${DOCKER_WORKDIR}/dht-node/log4js-config.json ./log4js-config.json +COPY --chown=app:app --from=installer ${DOCKER_WORKDIR}/federation/log4js-config.json ./log4js-config.json # Run command CMD ["node", "index.js"] \ No newline at end of file diff --git a/nginx/gradido.conf b/nginx/gradido.conf index 77c756215..c0489d549 100644 --- a/nginx/gradido.conf +++ b/nginx/gradido.conf @@ -25,7 +25,7 @@ server { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; - proxy_pass http://frontend:3000; + proxy_pass http://gradido:3000; proxy_redirect off; } @@ -38,7 +38,7 @@ server { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; - proxy_pass http://backend:4000; + proxy_pass http://gradido:4000; proxy_redirect off; } @@ -51,7 +51,7 @@ server { proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; - proxy_pass http://backend:4000/hook; + proxy_pass http://gradido:4000/hook; proxy_redirect off; } @@ -65,7 +65,7 @@ server { proxy_set_header Host $host; # TODO: in docker environemnt we do not have the trailing slash. This needs work - proxy_pass http://admin:8080; + proxy_pass http://gradido:8080; proxy_redirect off; } diff --git a/package.json b/package.json index 5f209624c..4f28c02a9 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "release": "scripts/release.sh", "installAll": "yarn && cd database && yarn && cd ../frontend && yarn && cd ../admin && yarn && cd ../backend && yarn && cd ../federation && yarn && cd ../dht-node && yarn && cd ../e2e-tests && yarn && cd ..", "docker:admin": "docker build -f ./admin/Dockerfile --target production -t \"gradido/admin:latest\" --build-arg NODE_ENV=\"production\" --build-arg BUILD_COMMIT=$(git rev-parse HEAD) --build-arg BUILD_COMMIT_SHORT=$(git rev-parse --short HEAD) .", - "docker:frontend": "docker build -f ./frontend/Dockerfile --target production -t \"gradido/frontend:latest\" --build-arg NODE_ENV=\"production\" --build-arg BUILD_COMMIT=$(git rev-parse HEAD) --build-arg BUILD_COMMIT_SHORT=$(git rev-parse --short HEAD) ." + "docker:frontend": "docker build -f ./frontend/Dockerfile --target production -t \"gradido/frontend:latest\" --build-arg NODE_ENV=\"production\" --build-arg BUILD_COMMIT=$(git rev-parse HEAD) --build-arg BUILD_COMMIT_SHORT=$(git rev-parse --short HEAD) .", + "docker": "BUILD_COMMIT=$(git rev-parse HEAD) docker compose -f docker-compose.yml up" }, "dependencies": { "auto-changelog": "^2.4.0", diff --git a/yarn.lock b/yarn.lock index 9914b8de7..f350f0362 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1969,7 +1969,7 @@ resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.7.tgz#4158d3105276773d5b7695cd4834b1722e4f37a8" integrity sha512-w28IoSUCJpidD/TGviZwwMJckNESJZXFu7NBZ5YJ4mEUnNraUn9Pm8HSZm/jDF1pDWYKspWE7oVphigUPRakIQ== -"@types/express-serve-static-core@^4.17.18", "@types/express-serve-static-core@^4.17.21", "@types/express-serve-static-core@^4.17.33": +"@types/express-serve-static-core@^4.17.21", "@types/express-serve-static-core@^4.17.33": version "4.19.6" resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.19.6.tgz#e01324c2a024ff367d92c66f48553ced0ab50267" integrity sha512-N4LZ2xG7DatVqhCZzOGb1Yi5lMbXSZcmdLDe9EzSndPV2HpWYWzRbaerl2n27irrm94EPpprqa8KpskPT085+A== @@ -1998,17 +1998,7 @@ "@types/express-serve-static-core" "^5.0.0" "@types/serve-static" "*" -"@types/express@4.17.12": - version "4.17.12" - resolved "https://registry.yarnpkg.com/@types/express/-/express-4.17.12.tgz#4bc1bf3cd0cfe6d3f6f2853648b40db7d54de350" - integrity sha512-pTYas6FrP15B1Oa0bkN5tQMNqOcVXa9j4FTFtO8DWI9kppKib+6NJtfTOOLcwxuuYvcX2+dVG6et1SxW/Kc17Q== - dependencies: - "@types/body-parser" "*" - "@types/express-serve-static-core" "^4.17.18" - "@types/qs" "*" - "@types/serve-static" "*" - -"@types/express@^4.17.12", "@types/express@^4.17.21": +"@types/express@4.17.21", "@types/express@^4.17.12", "@types/express@^4.17.21": version "4.17.21" resolved "https://registry.yarnpkg.com/@types/express/-/express-4.17.21.tgz#c26d4a151e60efe0084b23dc3369ebc631ed192d" integrity sha512-ejlPM315qwLpaQlQDTjPdsUFSc6ZsP4AN6AlWnogPjQ7CVi7PYF3YVz+CY3jE2pwYf7E/7HlDAN0rV2GxTG0HQ==