diff --git a/dockerfile b/dockerfile index 142e57e..62a52f6 100644 --- a/dockerfile +++ b/dockerfile @@ -1,9 +1,8 @@ -# build environment +# BUILD ENVIRONMENT FROM node:14-slim as builder WORKDIR /app -# global args -ARG APP_SUBFOLDER= +ARG APP_SUBFOLDER="" COPY .npmrc .npmrc COPY package*.json ./ @@ -12,18 +11,15 @@ RUN rm -f .npmrc COPY . ./ -# set the PUBLIC_URL environment variable -ENV PUBLIC_URL /${APP_SUBFOLDER}/ - # build the react app -# RUN npm run build -RUN if [ -z "$APP_SUBFOLDER" ]; then npm run build; else PUBLIC_URL=$PUBLIC_URL npm run build; fi +RUN if [ -z "$APP_SUBFOLDER" ]; then npm run build; else PUBLIC_URL=/${APP_SUBFOLDER}/ npm run build; fi - -# production environment +# PRODUCTION ENVIRONMENT FROM node:14-slim -RUN printf '\n\n- Copy application files\n' +ARG APP_SUBFOLDER="" + +RUN printf '\n\n- Copy application files\n' COPY --from=builder /app/build ./application/${APP_SUBFOLDER} COPY --from=builder /app/build/index.html ./application/ COPY --from=builder /app/setenv.js ./application/setenv.js @@ -33,6 +29,7 @@ RUN npm install -g serve # environment variables ENV AUTHOR="Tudor Stanciu" +ENV PUBLIC_URL=/${APP_SUBFOLDER}/ ARG APP_VERSION=0.0.0 ENV APP_VERSION=${APP_VERSION} @@ -44,4 +41,4 @@ WORKDIR / EXPOSE 80 -CMD ["sh", "-c", "node setenv.js && serve -s application -p 80"] \ No newline at end of file +CMD ["sh", "-c", "node application/setenv.js && serve -s application -p 80"] \ No newline at end of file diff --git a/setenv.js b/setenv.js index abe4d1a..86b0cac 100644 --- a/setenv.js +++ b/setenv.js @@ -3,7 +3,8 @@ const fs = require("fs"); const path = require("path"); const prefix = "REACT_APP_"; -const scriptPath = path.join("./application", process.env.PUBLIC_URL, "env.js"); +const publicUrl = process.env.PUBLIC_URL || ""; +const scriptPath = path.join("./application", publicUrl, "env.js"); function generateScriptContent() { const prefixRegex = new RegExp(`^${prefix}`);