<p>I have a docker container with a few environment variables setup. I know that in a typical use case, React is unable to read these variables as the process.env variables are replaced during transpiling.</p>

<p>I am trying to work around this without having to spin up a back-end server by using a plugin called react-env.</p>

<p>The core idea is to create a .env file which can be written to using a bash script. The bash script will copy any environmental variables to the .env file. The plugin will generate a env.js file which then will be consumed by the React application at runtime.</p>

<p><strong>Dockerfile</strong></p>

<pre><code>FROM node:9

# Install yarn
RUN npm install yarn

# Install serve.js
RUN yarn global add serve

# Create app directory
WORKDIR /usr/app

# Copy all necessary files and grant permissions.
# - yarn.lock
# - package.json
# - .env
# - docker-entrypoint.sh
COPY . /usr/app/
COPY yarn.lock /usr/app
COPY package.json /usr/app
COPY .env /usr/app
COPY docker-entrypoint.sh /usr/app/docker-entrypoint.sh
RUN chmod 777 /usr/app/docker-entrypoint.sh

# Install dependencies.
RUN printf "\n" &gt;&gt; /usr/app/.env
RUN echo 'REACT_APP_GCP_PROJECT_ID='$GCP_PROJECT_ID &gt;&gt; /usr/app/.env //READ ENVIRONMENTAL VARIABLE HERE
RUN yarn

# Build application.
RUN yarn build

# Set entrypoint of application.
ENTRYPOINT [ "/usr/app/docker-entrypoint.sh" ]
</code></pre>

<p><strong>docker-entrypoint.sh</strong></p>

<pre><code>serve build
</code></pre>

<p><strong>App.jsx</strong></p>

<pre><code>...

console.log(env("GCP_PROJECT_ID"));

...
</code></pre>

<p><strong>Inspect Console</strong></p>

<pre><code>'' //Empty string.
</code></pre>

<p>However, after making the image and deploying the application, the console outputs an empty string of the environmental variable.</p>

<p>Manually <code>exec</code> into the and running the <code>echo</code> command correctly does work:</p>

<p><strong>Querying manually:</strong></p>

<pre><code># echo 'REACT_APP_GCP_PROJECT_ID='$GCP_PROJECT_ID
REACT_APP_GCP_PROJECT_ID=SOME_VALUE
</code></pre>

<p>but</p>

<pre><code># cat .env
REACT_APP_SETUP="OK"
REACT_APP_GCP_PROJECT_ID=
</code></pre>

<p><strong>How can I get my pod environmental variables into my <code>.env</code> file before React runs?</strong></p>

Carrein
<p>It looks like $GCP_PROJECT_ID is not defined in the build environment, and thus it isn't included in the echo command when it runs at build time.  (I presume this is because you are having kubectl define it).</p>

<p>Assuming that you expect that $GCP_PROJECT_ID does not exist in the build environment and is instead something you are trying to copy from at the time your container executes (as indicated by your exec example), your problem is that the quoting of the RUN line</p>

<pre><code>RUN echo 'REACT_APP_GCP_PROJECT_ID='$GCP_PROJECT_ID &gt;&gt; /usr/app/.env
</code></pre>

<p>Is interpreting $GCP_PROJECT_ID during the build, which of course is the empty string.  In your test in the shell inside the container, it appears that $GCP_PROJECT_ID actually exists in the environment and all is well.</p>

<p>You need to actually quote the $GCP_PROJECT_ID variable to get it included into /usr/app/.env, like so (instead of being interpreted as an empty string!):</p>

<pre><code>RUN echo 'REACT_APP_GCP_PROJECT_ID=$GCP_PROJECT_ID' &gt;&gt; /usr/app/.env
</code></pre>

<p>Of course, if you need the literal string which is the value of $GCP_PROJECT_ID to be included in the .env file, your only alternative may be to actually do the echo into .env at the beginning of the container execution (e.g. at the top of <code>docker-entrypoint.sh</code>, and remove all the related code from your Dockerfile).</p>

<hr>

<p>If I am wrong about you having kubectl define it when the process starts, an alternative solution would require you to ensure that the docker build itself has access to that environment variable.  You can do this with the <code>--build-arg</code> argument, such as:</p>

<pre><code>docker build --build-arg GCP_PROJECT_ID=[correct value] .
</code></pre>

<p>In this case, you need to add an argument command to the Dockerfile, like so:</p>

<pre><code>ARG GCP_PROJECT_ID
</code></pre>

<p>at some point before the RUN command.</p>
