by Krunoslav Banovac
How to implement runtime environment variables with create-react-app, Docker, and Nginx
There are many ways to configure your React application. Let’s use an approach which respects Twelve-Factor App methodology. This means it enforces reconfiguration during runtime. Therefore no build per environment would be required.
🤔 What do we want to achieve?
We want to be able to run our React application as a Docker container that is built once. It runs everywhere by being configurable during runtime. The output should be a lightweight and performant container which serves our React application as static content, which we achieve by using Ngnix Alpine. Our application should allow configuration within docker-compose file such as this:
version: "3.2" services: my-react-app: image: my-react-app ports: - "3000:80" environment: - "API_URL=https://production.example.com"
We should be able to configure our React application using
-e flag (environment variables) when using
Docker run command.
On first glance, this approach may seem to bring too small of a benefit for the extra work it requires for initial setup. But once setup is done, environment specific configurations and deployment will be way easier to handle. So for anyone targeting dynamic environments or using orchestration systems, this approach is definitely something to consider.
🧐 The problem
First of all, it must be clear that there is no such thing as environment variables inside the browser environment. Whichever solution we use nowadays is nothing but a fake abstraction.
But, then you might ask, what about
.env files and
REACT_APP prefixed environment variables which come straight from documentation? Even inside the source code, these are used as
process.env just like we use environment variables inside Node.js.
In reality, the object
process does not exist inside the browser environment, it’s Node-specific. CRA by default doesn’t do server-side rendering. It can’t inject environment variables during content serving (like Next.js does). During transpiling, Webpack process replaces all occurrences of
process.env with a string value that was given. This means it can only be configured during build time.
The specific moment when it is still possible to inject environment variables happens when we start our container. Then we can read environment variables from inside the container. We can write them into a file which can be served via Nginx (which also serves our React app). They are imported into our application using
<script> tag inside the head section of
window object. Injected to be globally available within our application the browser way.
🐢 Step by step guide
Let’s start with a simple
create-react-app project and create
.env file with our first environment variable that we want to expose.
Then let’s write a small bash script which will read
We need to add the following line to
<head> element inside
index.htmlwhich then imports the file created by our bash script.
Let’s display our environment variable within the application:
During development, if we don’t want to use Docker, we can run bash script via
npm script runner by modifying
And if we run
yarn dev we should see output like this:
There are two ways to reconfigure environment variables within dev. Either change the default value inside
.env file or override defaults by running
yarn devcommand with environment variables prepended:
API_URL=https://my.new.dev.api.com yarn dev
And finally, edit
.gitignore so that we exclude environment configurations out of the source code:
# Temporary env files /public/env-config.js env-config.js
As for the development environment, that’s it! We are half-way there. We haven’t made a huge difference at this point compared to what CRA offered by default for the development environment. The true potential of this approach shines in production.
Now we are going to create minimal Nginx configuration so that we can build an optimized image which serves the production-ready application.
# Create directory for Ngnix configuration mkdir -p conf/conf.d touch conf/conf.d/default.conf conf/conf.d/gzip.conf
The main configuration file should look somewhat like this:
It’s also useful to enable gzip compression so that our assets are more lightweight during network transition:
Now that our Nginx configuration is ready, we can finally create Dockerfile and docker-compose files:
Initially, we use
node:alpine image to create an optimized production build of our application. Then, we build a runtime image on top of
# => Build container FROM node:alpine as builder WORKDIR /app COPY package.json . COPY yarn.lock . RUN yarn COPY . . RUN yarn build # => Run container FROM nginx:1.15.2-alpine # Nginx config RUN rm -rf /etc/nginx/conf.d COPY conf /etc/nginx # Static build COPY --from=builder /app/build /usr/share/nginx/html/ # Default port exposure EXPOSE 80 # Copy .env file and shell script to container WORKDIR /usr/share/nginx/html COPY ./env.sh . COPY .env . # Add bash RUN apk add --no-cache bash # Make our shell script executable RUN chmod +x env.sh # Start Nginx server CMD ["/bin/bash", "-c", "/usr/share/nginx/html/env.sh && nginx -g \"daemon off;\""]
Now our container is ready. We can do all the standard stuff with it. We can build a container, run it with inline configurations and push it to a repository provided by services such as Dockerhub.
docker run command should output application like so:
Lastly, let’s create our docker-compose file. You will usually have different docker-compose files depending on the environment and you will use
-f flag to select which file to use.
version: "3.2" services: cra-runtime-environment-variables: image: kunokdev/cra-runtime-environment-variables ports: - "5000:80" environment: - "API_URL=production.example.com"
And if we do
docker-compose up we should see output like so:
Great! We have now achieved our goal. We can reconfigure our application easily in both development and production environments in a very convenient way. We can now finally build only once and run everywhere!
If you got stuck or have additional ideas, access the source code on GitHub.
💅 Next steps
The current implementation of the shell script will print all variables included within the .env file. Most of the time we don’t want to expose all of them. You could implement filters for variables you don’t want to expose using prefixes or a similar technique.
🐓 Alternative solutions
As noted above, the build time configuration will satisfy most use cases. You can rely on the default approach using .env file per environment and build a container for each environment and inject values via CRA Webpack provided environment variables.
You could also have a look at this CRA GitHub repository issue which covers this problem. By now, there should be more posts and issues which cover this topic. Each offers a similar solution as above. It’s up to you to decide how are you going to implement specific details. You might use Node.js to serve your application which means that you can also replace shells script with Node.js script. Note that Nginx is more convenient to serve static content.
If you have any questions or want to offer feedback; feel free to open issue on GitHub. Optionally follow me for further posts related to web technologies.