We have a react application which is bundled using npm run build and deployed in nginx container. Other users just get the docker image and env file.
We want to keep the env variables configurable. Since the npm run build command has generated minified files and then they are added to nginx deployement directory; I was not able to find a way to update those environments.
In React app there is environments directory which has environment.ts and in code they are accessed using ${process.env.REACT_APP_API_BASE_URL}.
Below is sample Dockerfile which we use.
FROM node:14 as build-stage
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
ARG REACT_APP_API_BASE_URL
ENV REACT_APP_API_BASE_URL=$REACT_APP_API_BASE_URL
RUN npm run build
# Stage 2
FROM nginx:1.17.0-alpine
COPY --from=build-stage /app/build /usr/share/nginx/html
EXPOSE $REACT_DOCKER_PORT
CMD nginx -g 'daemon off;'
How to make env variables available to bundled javascript code ? Any suggestions would help a lot.