0

I have a react app (CRA) it will be running inside a docker container for which I will set an environment variable "API_KEY" = "some_value". For the example of windows this is System Properties -> Environment Variables -> System Variables -> Variable = "API_KEY" and Value = "some_value".

I would like to access to get this variable into the application at run time. This is for the API_KEY for Azure App Insights.

https://learn.microsoft.com/en-us/azure/application-insights/app-insights-javascript#add-the-sdk-script-to-your-app-or-web-pages

The above link shows the <your instrumentation key>. I will be deploying the same application to multiple environments. Each instance of the application will need to use its specific App Insights. So the goal here is to specify that API_KEY in the environment variables which will be different for each of the docker containers.

Please note I am aware of nodeJs and process.env.API_KEY but this does not read from the systems environment variables. Is there a way to get the system variable to link up to the process.env for the node instance?

-PS this request is to an API service that will need to fire straight away. So making an API request to get it is out of the question. It will be exposed to the end client as it is logging JavaScript events for each client.

Sigex
  • 2,834
  • 2
  • 24
  • 25
  • Docker by nature is isolated from the host system. You’ll need to [pass your environment variables into the container at runtime](https://stackoverflow.com/q/30494050/1813169). – MTCoster Nov 14 '18 at 16:20
  • 2
    Possible duplicate of [How do I pass environment variables to Docker containers?](https://stackoverflow.com/questions/30494050/how-do-i-pass-environment-variables-to-docker-containers) – MTCoster Nov 14 '18 at 16:21

1 Answers1

4

The answer is NO. You can not read system environment variables. You can however set process environment variables in a *.env file. In my case I will generate this env file via a shell script which will be triggered by the Dockerfile. This will then scrap the process for it's environment variables that start with "REACT_APP_#".

Hope this helps others.

Sigex
  • 2,834
  • 2
  • 24
  • 25