React enviroment variables inside docker container not working


This Content is from Stack Overflow. Question asked by Naor Anhaisy

I have an issue, where inside my docker container of the react app, my env variables are not working (got undefined).

My Dockerfile:

FROM <my nginx image>

COPY build/. /usr/share/nginx/html

COPY config/nginx.conf /etc/nginx/nginx.conf

EXPOSE 8080 80

My .env file (in the root of the project):


And in my code, I access that env variable through process.env.REACT_APP_VAR.

However, when I execute inside my production Linux server the command docker exec client -e, I do get all the env variables, including REACT_APP_VAR, PATH, HOSTNAME and etc.

Important to say, this issue is only in the docker (in the prod server), in my windows development station it works fine (without docker).

Also, I can’t add ENV inside my Dockerfile, and I rather not use the docker yaml’s files.


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?