This project showcases a simple use of environement variables for a dockerized React application.
React compiles environment variables directly into the page which goes against the portability of Docker images. Although there is no such thing as environment variables in the browser, this solution proposes to inject them though the window
object, which is supported by all browsers.
- A file named
.env
shoud be created containing all environement variables names with default. This file can be checked out as it should not contain confidential nor secret information. - You should add
<script src="%PUBLIC_URL%/env.js"></script>
topublic/index.html
so the file can be picked up during the build time.
- A bash script (
env.sh
) reads.env
and creates a newenv.js
that injects thewindow._env_
variable, which is then read by the code. - Docker -e options and docker-compose
environment
are then passed to thewindow
object.