How to run React app inside Docker with env vars
See original GitHub issueAlready set the env vars in my Docker on AWS, but the app is not using it when running.
Does anyone know how I can do it?
For development, I’m using the .env
file. For the build, I’m putting the .env
file in .dockerignore
so this development file doesn’t go into the build version. My intention is to use the default Env Vars configuration in AWS ECS.
I’m not using docker-compose
. I’m just using the Dockerfile
.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:46 (6 by maintainers)
Top Results From Across the Web
How to implement runtime environment variables with create ...
We should be able to configure our React application using -e flag (environment variables) when using Docker run command.
Read more >Reading an environment variable in react which was set by ...
First in GH action file I use run: docker build ... --build-arg REACT_APP_APIURL=${{ secrets.REACT_APP_APIURL }} . Then I use them in Dockerfile ......
Read more >Making environment variables accessible in front-end containers
Learn how to inject environment variables directly into your React, Angular, or Vue.js codebase during the front-end container build ...
Read more >ReactJS + Docker - Environment Variables - Jason Campos
Injecting environment variables into client-side rendered ReactJS applications built with Docker and Compose.
Read more >How to use react environment variables: The complete guide
Learn how to use react environment variables in development and production environments with Kubernetes and docker compose examples.
Read more >Top Related Medium Post
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
thanks for all the tips on this page, I’ve been able to get CRA running in docker. I found a few other handy things:
Here’s my setup: Dockerfile
run.sh (executed by the docker container)
NOTES: If NODE_ENV=development this will run the webpack-development-server, otherwise it will build the app and serve it via nginx. (I got this from a great blog post which I can’t for the life of me find anymore)
nginx.conf
docker-compose.yml
For my development with hot reloading, I run one terminal window and execute the following:
docker-compose up -d dev
To watch and run tests, I then open another terminal windown and execute:
docker-compose up test
(I don’t use the -d flag as I want to see the output)And for a production sanity check, I can run
docker-compose up prod
NOTE: - this will serve the react app at the time the docker image was created, so if you’ve been making changes since the docker build, these won’t appear. However, if this is used as part of a CI system which checks out the code and builds it, the image built will be the latest.On an unrelated note, I also found it useful to set the NODE_PATH in my package.json to allow relative imports:
@gearon Docker is amazing, I can run create-react-app without installing node on my local, hence avoiding having to use node/npm version managers such as nvm. Plus, it just works! I would even go so far to say at the very least you should add a how to integrate with docker section to the readme.
If anyone is interested I got it to work with the following (for all steps below, replace the string boilerplate with you apps name) - if there are any improvements I can make, please let me know.
Step 1) After creating your react app with create-react-app -> cd into your newly created apps directory and run this command
Step 2) Add a dockerfile to the root of your app directory
Dockerfile
Step 3) create a docker-compose.yml in your root directory
docker-compose.yml
You can now access your app as normal at http://localhost:3000
and you can even interact with your new docker container via the following command
Yes, this isn’t following some of the nodejs docker recommend best practices, but I use the above only for the dev environment configuration so I’m unaware if those recommendations are needed. I’m open minded to improving this though.
Obviously you need to configure the build docker differently than above.