From 9f57df49924fc13d344b96267f524d68f1e9d452 Mon Sep 17 00:00:00 2001 From: Vijay Soni Date: Sun, 6 Oct 2024 23:52:11 +0530 Subject: [PATCH] Add base path support for Next.js frontend Add support for deploying the homepage container at a specified URL path, passed as an environment variable. --- Dockerfile | 3 ++- docker-entrypoint.sh | 3 +++ next.config.js | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) mode change 100755 => 100644 docker-entrypoint.sh diff --git a/Dockerfile b/Dockerfile index 3e6de756..820385cd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,6 +23,7 @@ WORKDIR /app ARG BUILDTIME ARG VERSION ARG REVISION +ARG NEXT_PUBLIC_BASE_PATH COPY --link --from=deps /app/node_modules ./node_modules/ COPY . . @@ -30,7 +31,7 @@ COPY . . SHELL ["/bin/ash", "-xeo", "pipefail", "-c"] RUN npm run telemetry \ && mkdir config \ - && NEXT_PUBLIC_BUILDTIME=$BUILDTIME NEXT_PUBLIC_VERSION=$VERSION NEXT_PUBLIC_REVISION=$REVISION npm run build + && NEXT_PUBLIC_BUILDTIME=$BUILDTIME NEXT_PUBLIC_VERSION=$VERSION NEXT_PUBLIC_REVISION=$REVISION NEXT_PUBLIC_BASE_PATH=$NEXT_PUBLIC_BASE_PATH npm run build # Production image, copy all the files and run next FROM docker.io/node:18-alpine AS runner diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh old mode 100755 new mode 100644 index c2858808..ea27aedb --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -12,6 +12,9 @@ export PGID=${PGID:-0} export HOMEPAGE_BUILDTIME=$(date +%s) +# Set the base path for the Next.js application +export NEXT_PUBLIC_BASE_PATH=${NEXT_PUBLIC_BASE_PATH:-""} + # Set privileges for /app but only if pid 1 user is root and we are dropping privileges. # If container is run as an unprivileged user, it means owner already handled ownership setup on their own. # Running chown in that case (as non-root) will cause error diff --git a/next.config.js b/next.config.js index d0395611..573a6586 100644 --- a/next.config.js +++ b/next.config.js @@ -9,6 +9,7 @@ const nextConfig = { unoptimized: true, }, i18n, + basePath: process.env.NEXT_PUBLIC_BASE_PATH || '', }; module.exports = nextConfig;