From e8bb95cb35ed1b02a85cf7cbea5bfc20fe0324c2 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Wed, 5 May 2021 14:50:38 +0200 Subject: build: Support runtime variables in frontend Docker image This change adds support for dynamically specifying various variables for the frontend Docker image through environmental variables. Previously, these were embedded directly into the source code, which requires rebuilding the entire image to change these variables. --- docker-compose.override.yml | 5 ++--- docker-compose.prod.yml | 5 ++--- docker-compose.yml | 6 +++--- opendc-web/opendc-web-ui/Dockerfile | 17 ++++++----------- opendc-web/opendc-web-ui/public/index.html | 9 ++++++++- opendc-web/opendc-web-ui/scripts/envsubst-html.sh | 19 +++++++++++++++++++ .../opendc-web-ui/src/api/routes/token-signin.js | 2 +- opendc-web/opendc-web-ui/src/api/socket.js | 2 +- opendc-web/opendc-web-ui/src/containers/auth/Login.js | 6 +++--- opendc-web/opendc-web-ui/src/index.js | 5 +++-- 10 files changed, 48 insertions(+), 28 deletions(-) create mode 100755 opendc-web/opendc-web-ui/scripts/envsubst-html.sh diff --git a/docker-compose.override.yml b/docker-compose.override.yml index e48f4dff..172cc4cd 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -3,11 +3,10 @@ version: "3.8" # Docker Compose overrides for development environments services: frontend: - build: - args: - OPENDC_API_BASE_URL: http://localhost:8081 ports: - "8080:80" + environment: + API_BASE_URL: http://localhost:8081 api: ports: diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 3cefdcdd..96bb004e 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -3,11 +3,10 @@ version: "3.8" # Docker Compose overrides for production environments services: frontend: - build: - args: - OPENDC_API_BASE_URL: ${OPENDC_API_BASE_URL} ports: - "8080:80" + environment: + API_BASE_URL: ${OPENDC_API_BASE_URL} api: ports: diff --git a/docker-compose.yml b/docker-compose.yml index 982f2ed8..30c00996 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,13 +3,13 @@ services: frontend: build: context: opendc-web/opendc-web-ui - args: - OPENDC_OAUTH_CLIENT_ID: ${OPENDC_OAUTH_CLIENT_ID} - OPENDC_FRONTEND_SENTRY_DSN: ${OPENDC_FRONTEND_SENTRY_DSN} image: frontend restart: on-failure networks: - backend + environment: + OAUTH_CLIENT_ID: ${OPENDC_OAUTH_CLIENT_ID} + SENTRY_DSN: ${OPENDC_FRONTEND_SENTRY_DSN} api: build: opendc-web/opendc-web-api diff --git a/opendc-web/opendc-web-ui/Dockerfile b/opendc-web/opendc-web-ui/Dockerfile index 20e16b08..7aa3a7bf 100644 --- a/opendc-web/opendc-web-ui/Dockerfile +++ b/opendc-web/opendc-web-ui/Dockerfile @@ -1,4 +1,4 @@ -FROM node:14 AS staging +FROM node:15 AS staging MAINTAINER OpenDC Maintainers # Copy package details @@ -6,21 +6,16 @@ COPY ./package.json ./yarn.lock /opendc/ RUN cd /opendc && yarn # Build frontend -FROM node:14 AS build - -ARG OPENDC_OAUTH_CLIENT_ID -ARG OPENDC_API_BASE_URL -ARG OPENDC_FRONTEND_SENTRY_DSN +FROM node:15 AS build COPY ./ /opendc COPY --from=staging /opendc/node_modules /opendc/node_modules RUN cd /opendc/ \ - && export REACT_APP_OAUTH_CLIENT_ID=$OPENDC_OAUTH_CLIENT_ID \ - && export REACT_APP_API_BASE_URL=$OPENDC_API_BASE_URL \ - && export REACT_APP_SENTRY_DSN=$OPENDC_FRONTEND_SENTRY_DSN \ - && yarn build + && yarn build \ + && mv build/index.html build/index.html.template # Setup nginx to serve the frontend -FROM nginx:1.19 +FROM nginx:1.20 +COPY --from=build /opendc/scripts/envsubst-html.sh /docker-entrypoint.d/00-envsubst-html.sh COPY --from=build /opendc/build /usr/share/nginx/html COPY nginx.conf /etc/nginx/conf.d/default.conf diff --git a/opendc-web/opendc-web-ui/public/index.html b/opendc-web/opendc-web-ui/public/index.html index 44a0d80f..da734797 100644 --- a/opendc-web/opendc-web-ui/public/index.html +++ b/opendc-web/opendc-web-ui/public/index.html @@ -33,7 +33,7 @@ - + @@ -52,6 +52,13 @@ gtag('js', new Date()) gtag('config', 'UA-84285092-3') +