Using System environment variables in node / react with docker












-1















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://docs.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.










share|improve this question























  • Docker by nature is isolated from the host system. You’ll need to pass your environment variables into the container at runtime.

    – MTCoster
    Nov 14 '18 at 16:20






  • 2





    Possible duplicate of How do I pass environment variables to Docker containers?

    – MTCoster
    Nov 14 '18 at 16:21
















-1















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://docs.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.










share|improve this question























  • Docker by nature is isolated from the host system. You’ll need to pass your environment variables into the container at runtime.

    – MTCoster
    Nov 14 '18 at 16:20






  • 2





    Possible duplicate of How do I pass environment variables to Docker containers?

    – MTCoster
    Nov 14 '18 at 16:21














-1












-1








-1








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://docs.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.










share|improve this question














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://docs.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.







node.js reactjs docker






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 16:17









SigexSigex

4951415




4951415













  • Docker by nature is isolated from the host system. You’ll need to pass your environment variables into the container at runtime.

    – MTCoster
    Nov 14 '18 at 16:20






  • 2





    Possible duplicate of How do I pass environment variables to Docker containers?

    – MTCoster
    Nov 14 '18 at 16:21



















  • Docker by nature is isolated from the host system. You’ll need to pass your environment variables into the container at runtime.

    – MTCoster
    Nov 14 '18 at 16:20






  • 2





    Possible duplicate of How do I pass environment variables to Docker containers?

    – MTCoster
    Nov 14 '18 at 16:21

















Docker by nature is isolated from the host system. You’ll need to pass your environment variables into the container at runtime.

– MTCoster
Nov 14 '18 at 16:20





Docker by nature is isolated from the host system. You’ll need to pass your environment variables into the container at runtime.

– MTCoster
Nov 14 '18 at 16:20




2




2





Possible duplicate of How do I pass environment variables to Docker containers?

– MTCoster
Nov 14 '18 at 16:21





Possible duplicate of How do I pass environment variables to Docker containers?

– MTCoster
Nov 14 '18 at 16:21












1 Answer
1






active

oldest

votes


















0














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.




  • Generating shell script
    https://medium.com/@vietgoeswest/passing-env-variables-from-the-server-to-your-create-react-app-c87578a45358


  • Setting up env with CRA
    https://medium.com/@tacomanator/environments-with-create-react-app-7b645312c09d


  • Dockerizing a Node.js web app
    https://nodejs.org/en/docs/guides/nodejs-docker-webapp/


  • Passing environment variables from Docker to Node https://medium.com/@felipedutratine/pass-environment-variables-from-docker-to-my-nodejs-or-golang-app-a1f2ddec31f5


  • Also helpful
    https://github.com/facebook/create-react-app/issues/982







share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304553%2fusing-system-environment-variables-in-node-react-with-docker%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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.




    • Generating shell script
      https://medium.com/@vietgoeswest/passing-env-variables-from-the-server-to-your-create-react-app-c87578a45358


    • Setting up env with CRA
      https://medium.com/@tacomanator/environments-with-create-react-app-7b645312c09d


    • Dockerizing a Node.js web app
      https://nodejs.org/en/docs/guides/nodejs-docker-webapp/


    • Passing environment variables from Docker to Node https://medium.com/@felipedutratine/pass-environment-variables-from-docker-to-my-nodejs-or-golang-app-a1f2ddec31f5


    • Also helpful
      https://github.com/facebook/create-react-app/issues/982







    share|improve this answer




























      0














      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.




      • Generating shell script
        https://medium.com/@vietgoeswest/passing-env-variables-from-the-server-to-your-create-react-app-c87578a45358


      • Setting up env with CRA
        https://medium.com/@tacomanator/environments-with-create-react-app-7b645312c09d


      • Dockerizing a Node.js web app
        https://nodejs.org/en/docs/guides/nodejs-docker-webapp/


      • Passing environment variables from Docker to Node https://medium.com/@felipedutratine/pass-environment-variables-from-docker-to-my-nodejs-or-golang-app-a1f2ddec31f5


      • Also helpful
        https://github.com/facebook/create-react-app/issues/982







      share|improve this answer


























        0












        0








        0







        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.




        • Generating shell script
          https://medium.com/@vietgoeswest/passing-env-variables-from-the-server-to-your-create-react-app-c87578a45358


        • Setting up env with CRA
          https://medium.com/@tacomanator/environments-with-create-react-app-7b645312c09d


        • Dockerizing a Node.js web app
          https://nodejs.org/en/docs/guides/nodejs-docker-webapp/


        • Passing environment variables from Docker to Node https://medium.com/@felipedutratine/pass-environment-variables-from-docker-to-my-nodejs-or-golang-app-a1f2ddec31f5


        • Also helpful
          https://github.com/facebook/create-react-app/issues/982







        share|improve this answer













        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.




        • Generating shell script
          https://medium.com/@vietgoeswest/passing-env-variables-from-the-server-to-your-create-react-app-c87578a45358


        • Setting up env with CRA
          https://medium.com/@tacomanator/environments-with-create-react-app-7b645312c09d


        • Dockerizing a Node.js web app
          https://nodejs.org/en/docs/guides/nodejs-docker-webapp/


        • Passing environment variables from Docker to Node https://medium.com/@felipedutratine/pass-environment-variables-from-docker-to-my-nodejs-or-golang-app-a1f2ddec31f5


        • Also helpful
          https://github.com/facebook/create-react-app/issues/982








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 17:48









        SigexSigex

        4951415




        4951415
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304553%2fusing-system-environment-variables-in-node-react-with-docker%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values