I have ReactJs app with .env file like this:
REACT_APP_SERVER_URL="URL1"
Then I using Dockerfile
to build to Image:
# Fetching the latest node image on apline linux
FROM node:22 AS builder
# Setting up the work directory
WORKDIR /app
# Installing dependencies
COPY ./package.json ./
RUN npm install
# Copying all the files in our project
COPY . .
# Building dist and run
RUN npm run build
# # Fetching the latest nginx image
FROM nginx
# Copying built assets from builder
COPY --from=builder /app/build /usr/share/nginx/html
# Copying our nginx.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
After that, I pushed this Image to my reposity. Then I am using this Docker Composer file:
db:
image: postgres
container_name: db
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgress
POSTGRES_DB: my_db
PGPORT: 5433
volumes:
- ./pgdata:/var/lib/postgresql/data
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
expose:
- "5433"
ports:
- "5433:5433"
restart: always
server:
image: my_server_image
container_name: server
platform: linux/amd64
depends_on:
- db
ports:
- "8081:8081"
environment:
DB_HOST: db
react:
image: my_web_image
container_name: web
depends_on:
- server
platform: linux/amd64
environment:
- REACT_APP_SERVER_URL="New URL" <-----HERE
ports:
- "8082:8082"
But when run docker compose up
, then run my web app, I still see the REACT_APP_SERVER_URL show "URL1", can someone help?
console.log('url', process.env.REACT_APP_SERVER_URL)
-> URL1
.env.production
file and use that in your production build. See create-react-app.dev/docs/adding-custom-environment-variables/… – Canaigre.env
file directly before build – KnpENV variable of Image
, by the way, I can update this variable each time usingdocker compose up
– Knp