From 776ed29bcad7b7a1f81b32ae450561f007a3379c Mon Sep 17 00:00:00 2001 From: theoleuthardt Date: Fri, 28 Feb 2025 11:29:44 +0100 Subject: [PATCH 1/6] fix: frontend and backend now get variables at build time not runtime --- .github/workflows/deploy.yml | 4 ++++ backend/Dockerfile | 4 ++++ backend/server.ts | 4 ---- docker-compose.yaml | 10 ++++++---- frontend/Dockerfile | 3 +++ frontend/next.config.ts | 3 --- 6 files changed, 17 insertions(+), 11 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 34e50ba..1250eea 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -39,6 +39,8 @@ jobs: context: ./frontend push: true tags: ${{ secrets.DOCKERHUB_USERNAME }}/werkzeugkiste-frontend:latest + build-args: | + NEXT_PUBLIC_BACKEND_URL=${{ secrets.NEXT_PUBLIC_BACKEND_URL }} - name: Build and Push Backend uses: docker/build-push-action@v6 @@ -46,6 +48,8 @@ jobs: context: ./backend push: true tags: ${{ secrets.DOCKERHUB_USERNAME }}/werkzeugkiste-backend:latest + build-args: | + CORS_ALLOWED_ORIGIN=${{ secrets.CORS_ALLOWED_ORIGIN }} deploy-on-server: needs: docker-build-push diff --git a/backend/Dockerfile b/backend/Dockerfile index e602f45..c6a7478 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -61,7 +61,11 @@ USER fastify EXPOSE 4000 +ARG CORS_ALLOWED_ORIGIN +ENV CORS_ALLOWED_ORIGIN=$CORS_ALLOWED_ORIGIN + ENV PORT=4000 ENV HOSTNAME="0.0.0.0" + CMD ["node", "dist/server.js"] \ No newline at end of file diff --git a/backend/server.ts b/backend/server.ts index 6259cb5..d21e70e 100644 --- a/backend/server.ts +++ b/backend/server.ts @@ -10,10 +10,6 @@ import { generateQRCode } from "./src/routes/generateqrcode.route"; import { wordCounter } from "./src/routes/wordcounter.route"; import { videoToAudio } from "./src/routes/videotoaudio.route"; import { removeBG } from "./src/routes/removebg.route"; -import dotenv from "dotenv"; -import path from "node:path"; - -dotenv.config({ path: path.resolve(__dirname, "../.env") }); const app = Fastify({ logger: true }); diff --git a/docker-compose.yaml b/docker-compose.yaml index f034f04..aeb5cd4 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,30 +1,32 @@ services: frontend: + env_file: .env build: context: ./frontend dockerfile: Dockerfile + args: + NEXT_PUBLIC_BACKEND_URL: ${NEXT_PUBLIC_BACKEND_URL} container_name: werkzeugkiste-frontend - env_file: .env environment: - NODE_ENV=${NODE_ENV} - HOSTNAME=${HOSTNAME} - PORT=${FRONTEND_PORT} - - NEXT_PUBLIC_BACKEND_URL=${NEXT_PUBLIC_BACKEND_URL} ports: - "${FRONTEND_PORT}:3000" restart: unless-stopped backend: + env_file: .env build: context: ./backend dockerfile: Dockerfile + args: + CORS_ALLOWED_ORIGIN: ${CORS_ALLOWED_ORIGIN} container_name: werkzeugkiste-backend - env_file: .env environment: - NODE_ENV=${NODE_ENV} - HOSTNAME=${HOSTNAME} - PORT=${BACKEND_PORT} - - CORS_ALLOWED_ORIGIN=${CORS_ALLOWED_ORIGIN} ports: - "${BACKEND_PORT}:4000" restart: unless-stopped diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 4d67048..dc4130c 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -24,6 +24,9 @@ WORKDIR /app COPY --from=deps /app/node_modules ./node_modules COPY . . +ARG NEXT_PUBLIC_BACKEND_URL +ENV NEXT_PUBLIC_BACKEND_URL=$NEXT_PUBLIC_BACKEND_URL + # Next.js collects completely anonymous telemetry data about general usage. # Learn more here: https://nextjs.org/telemetry # Uncomment the following line in case you want to disable telemetry during the build. diff --git a/frontend/next.config.ts b/frontend/next.config.ts index 79cebf3..61ab14e 100644 --- a/frontend/next.config.ts +++ b/frontend/next.config.ts @@ -1,7 +1,4 @@ import type { NextConfig } from "next"; -import dotenv from "dotenv"; - -dotenv.config({ path: "../.env" }); const nextConfig: NextConfig = { output: "standalone", From 1ef7f9f822f7d3b4866def8f12a93867e4be500c Mon Sep 17 00:00:00 2001 From: theoleuthardt Date: Fri, 28 Feb 2025 11:37:15 +0100 Subject: [PATCH 2/6] feat: debug prints at runtime in docker --- backend/server.ts | 1 + frontend/next.config.ts | 2 ++ 2 files changed, 3 insertions(+) diff --git a/backend/server.ts b/backend/server.ts index d21e70e..1b7bd3e 100644 --- a/backend/server.ts +++ b/backend/server.ts @@ -34,4 +34,5 @@ console.log("Starting Fastify server..."); const PORT = process.env.BACKEND_PORT; app.listen({ port: Number(PORT), host: "0.0.0.0" }, () => { console.log(`🚀Fastify is live on http://localhost:${PORT}`); + console.log(`Allowed origin: ${process.env.CORS_ALLOWED_ORIGIN}`); }); diff --git a/frontend/next.config.ts b/frontend/next.config.ts index 61ab14e..2885d37 100644 --- a/frontend/next.config.ts +++ b/frontend/next.config.ts @@ -1,5 +1,7 @@ import type { NextConfig } from "next"; +console.log("NEXT_PUBLIC_BACKEND_URL", process.env.NEXT_PUBLIC_BACKEND_URL); + const nextConfig: NextConfig = { output: "standalone", env: { From 2dad778d0eb4cd0e5ce1fafb976f7ea60b156300 Mon Sep 17 00:00:00 2001 From: theoleuthardt Date: Fri, 28 Feb 2025 11:47:55 +0100 Subject: [PATCH 3/6] feat: debug print at nextjs container start --- .github/workflows/deploy.yml | 2 +- frontend/Dockerfile | 9 +++++---- frontend/entrypoint.sh | 3 +++ 3 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 frontend/entrypoint.sh diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 1250eea..a0ed3d1 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -49,7 +49,7 @@ jobs: push: true tags: ${{ secrets.DOCKERHUB_USERNAME }}/werkzeugkiste-backend:latest build-args: | - CORS_ALLOWED_ORIGIN=${{ secrets.CORS_ALLOWED_ORIGIN }} + CORS_ALLOWED_ORIGIN=${{ secrets.CORS_ALLOWED_ORIGIN }} deploy-on-server: needs: docker-build-push diff --git a/frontend/Dockerfile b/frontend/Dockerfile index dc4130c..f2affb9 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -60,8 +60,9 @@ USER nextjs EXPOSE 3000 ENV PORT=3000 - -# server.js is created by next build from the standalone output -# https://nextjs.org/docs/pages/api-reference/config/next-config-js/output ENV HOSTNAME="0.0.0.0" -CMD ["node", "server.js"] \ No newline at end of file + +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh + +CMD ["/entrypoint.sh"] \ No newline at end of file diff --git a/frontend/entrypoint.sh b/frontend/entrypoint.sh new file mode 100644 index 0000000..e3ec2e8 --- /dev/null +++ b/frontend/entrypoint.sh @@ -0,0 +1,3 @@ +#!/bin/sh +echo "NEXT_PUBLIC_BACKEND_URL: $NEXT_PUBLIC_BACKEND_URL" +exec node server.js From 22095aa71c7dc200b6aae80ceac95113c0eb8c22 Mon Sep 17 00:00:00 2001 From: Theo Leuthardt <60556192+theoleuthardt@users.noreply.github.com> Date: Fri, 28 Feb 2025 11:51:01 +0100 Subject: [PATCH 4/6] unnecessary change --- frontend/next.config.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/frontend/next.config.ts b/frontend/next.config.ts index 2885d37..61ab14e 100644 --- a/frontend/next.config.ts +++ b/frontend/next.config.ts @@ -1,7 +1,5 @@ import type { NextConfig } from "next"; -console.log("NEXT_PUBLIC_BACKEND_URL", process.env.NEXT_PUBLIC_BACKEND_URL); - const nextConfig: NextConfig = { output: "standalone", env: { From eb256f5cb5936fff4c91a2a9459df3cf821cf2da Mon Sep 17 00:00:00 2001 From: Theo Leuthardt <60556192+theoleuthardt@users.noreply.github.com> Date: Fri, 28 Feb 2025 11:52:52 +0100 Subject: [PATCH 5/6] fix: use bash idiot --- frontend/entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/entrypoint.sh b/frontend/entrypoint.sh index e3ec2e8..b838896 100644 --- a/frontend/entrypoint.sh +++ b/frontend/entrypoint.sh @@ -1,3 +1,3 @@ -#!/bin/sh +#!/bin/bash echo "NEXT_PUBLIC_BACKEND_URL: $NEXT_PUBLIC_BACKEND_URL" exec node server.js From 3b17ba8aa36ae032503347d97798f56628ebf351 Mon Sep 17 00:00:00 2001 From: theoleuthardt Date: Fri, 28 Feb 2025 12:04:15 +0100 Subject: [PATCH 6/6] fix: user switch for permissions to set chmod on entrypoint script --- frontend/Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frontend/Dockerfile b/frontend/Dockerfile index f2affb9..73ce712 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -55,7 +55,7 @@ RUN adduser --system --uid 1001 nextjs COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static -USER nextjs +USER root EXPOSE 3000 @@ -63,6 +63,7 @@ ENV PORT=3000 ENV HOSTNAME="0.0.0.0" COPY entrypoint.sh /entrypoint.sh + RUN chmod +x /entrypoint.sh CMD ["/entrypoint.sh"] \ No newline at end of file