From 782045dd824bc01b7a07a8bbcc0f5b02954cf241 Mon Sep 17 00:00:00 2001 From: Jonathan Chemla Date: Wed, 25 Jun 2025 12:56:36 +0200 Subject: [PATCH] Cobalt Web image Dockerfile Build and run image with ``` docker build -f web/Dockerfile -t cobalt-web . docker run -e WEB_DEFAULT_API="https://real.api.endpoint" -e WEB_HOST="http://abc" -e WEB_PLAUSIBLE_HOST="def" -p 800:80 cobalt-web ``` Currently, vite build still uses process.env env-vars at build time, so the docker container is not using those defined via placeholders in `runtime-config.js` --- web/Dockerfile | 33 +++++++++++++++++++++++++++++++++ web/docker-entrypoint.sh | 12 ++++++++++++ web/pnpm-workspace.yaml | 4 ++++ web/static/runtime-config.js | 5 +++++ 4 files changed, 54 insertions(+) create mode 100644 web/Dockerfile create mode 100644 web/docker-entrypoint.sh create mode 100644 web/pnpm-workspace.yaml create mode 100644 web/static/runtime-config.js diff --git a/web/Dockerfile b/web/Dockerfile new file mode 100644 index 00000000..913b5645 --- /dev/null +++ b/web/Dockerfile @@ -0,0 +1,33 @@ +# syntax=docker/dockerfile:1 + + +# docker build -f web/Dockerfile -t cobalt-web . + +FROM node:20-alpine AS builder + +WORKDIR /repo + +RUN npm install -g pnpm + +# Copy monorepo into container +COPY . . + +# Install all deps for monorepo +RUN pnpm install --frozen-lockfile + +WORKDIR /repo/web +RUN pnpm --filter ./web run build + + +FROM nginx:alpine + +WORKDIR /usr/share/nginx/html +COPY --from=builder /repo/web/build . + +COPY web/docker-entrypoint.sh /docker-entrypoint.sh +RUN chmod +x /docker-entrypoint.sh + +EXPOSE 80 + +ENTRYPOINT ["/docker-entrypoint.sh"] +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/web/docker-entrypoint.sh b/web/docker-entrypoint.sh new file mode 100644 index 00000000..3d74020a --- /dev/null +++ b/web/docker-entrypoint.sh @@ -0,0 +1,12 @@ +#!/bin/sh +set -e + +# Replace config placeholders with runtime values +if [ -f "/usr/share/nginx/html/runtime-config.js" ]; then + envsubst '${WEB_HOST} ${WEB_PLAUSIBLE_HOST} ${WEB_DEFAULT_API}' \ + < /usr/share/nginx/html/runtime-config.js \ + > /usr/share/nginx/html/runtime-config.tmp.js && \ + mv /usr/share/nginx/html/runtime-config.tmp.js /usr/share/nginx/html/runtime-config.js +fi + +exec "$@" \ No newline at end of file diff --git a/web/pnpm-workspace.yaml b/web/pnpm-workspace.yaml new file mode 100644 index 00000000..d7ab6cba --- /dev/null +++ b/web/pnpm-workspace.yaml @@ -0,0 +1,4 @@ +packages: + - "api" + - "web" + - "packages/*" diff --git a/web/static/runtime-config.js b/web/static/runtime-config.js new file mode 100644 index 00000000..94fcb40b --- /dev/null +++ b/web/static/runtime-config.js @@ -0,0 +1,5 @@ +window.COBALT_CONFIG = { + WEB_HOST: "${WEB_HOST}", + WEB_PLAUSIBLE_HOST: "${WEB_PLAUSIBLE_HOST}", + WEB_DEFAULT_API: "${WEB_DEFAULT_API}" +}; \ No newline at end of file