<p>I have one laravel docker container which is build with a custom nginx + php-fpm docker image.</p>
<p>I have deployed successfully to a k8s cluster and can access properly, also logging into the pod and running <code>env</code> I can see all the environment variables being set successfully from my k8s <code>configmap</code></p>
<p>In the laravel code I read the environment variables like this:
For example at <code>SomeController.php</code> have the following code:</p>
<pre><code>$apiCode = env('API_CODE');
// also tried like this $apiCode = getenv('API_CODE'); still not successful in fetching
</code></pre>
<p>My problem and this question is that the env vars are always read empty inside the php code even though inside the pod with <code>env</code> command I can see them properly set, somehow the php code cannot find them.</p>
<p>(I am not caching laravel config so that case we can exclude, also tried with the command <code>php artisan config:clear</code> beforehand, still same result cannot fetch the env vars within the php)</p>
<p>In the kubernetes definition yml I attach the configmap to env variables like below and see them defined properly inside pod:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: api
  labels:
    tier: api
spec:
  replicas: 1
  selector:
    matchLabels:
      tier: api
  template:
    metadata:
      labels:
        tier: api
    spec:
      containers:
        - name: api
          image: somenging-fpm-image:latest
          ports:
            - containerPort: 80
          envFrom:
            - configMapRef:
                name: api-config-env-file
</code></pre>
<p>For the moment I am lost and without any idea why this might happen.</p>
<p>I thought initially that maybe configmap was created late after pod started (php fpm somehow process then did not pick on start the env)</p>
<p>To verify and exclude that case, I destroyed the pod and recreated the deployment+pod in order to use the already existing configmap, and still the result was the same php did not pick up the env vars that were present in pod</p>
<p>I could log into pod by <code>kubectl exec -it [podnamehere] /bin/bash</code> and run <code>env</code> there and could see properly env vars set from my configmap <code>api-config-env-file</code>, but the code would always have them empty not be able to read them</p>

Kristi Jorgji
<p>I encountered this problems only when there are <code>redis</code> or caching system.</p>
<p>But you already state that</p>
<blockquote>
<p>(I am not caching laravel config so that case we can exclude, also tried with the command php artisan config:clear beforehand, still same result cannot fetch the env vars within the php)</p>
</blockquote>
<p>So double check if you run any command like <code>php artisan optimize</code>. It will save the cache to redis, and the laravel application will pick the setting up from there.</p>
<p>Just to make sure you don't miss anything.</p>
<p>Try:
<code>php artisan optimize:clear</code></p>
<p>This command will do the following:</p>
<pre class="lang-sh prettyprint-override"><code>Cached events cleared!
Compiled views cleared!
Application cache cleared!
Route cache cleared!
Configuration cache cleared!
Compiled services and packages files removed!
Caches cleared successfully!
</code></pre>
