1

I have one laravel docker container which is build with a custom nginx + php-fpm docker image.

I have deployed successfully to a k8s cluster and can access properly, also logging into the pod and running env I can see all the environment variables being set successfully from my k8s configmap

In the laravel code I read the environment variables like this: For example at SomeController.php have the following code:

$apiCode = env('API_CODE');
// also tried like this $apiCode = getenv('API_CODE'); still not successful in fetching

My problem and this question is that the env vars are always read empty inside the php code even though inside the pod with env command I can see them properly set, somehow the php code cannot find them.

(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)

In the kubernetes definition yml I attach the configmap to env variables like below and see them defined properly inside pod:

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

For the moment I am lost and without any idea why this might happen.

I thought initially that maybe configmap was created late after pod started (php fpm somehow process then did not pick on start the env)

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

I could log into pod by kubectl exec -it [podnamehere] /bin/bash and run env there and could see properly env vars set from my configmap api-config-env-file, but the code would always have them empty not be able to read them

1 Answer 1

1

I encountered this problems only when there are redis or caching system.

But you already state that

(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)

So double check if you run any command like php artisan optimize. It will save the cache to redis, and the laravel application will pick the setting up from there.

Just to make sure you don't miss anything.

Try: php artisan optimize:clear

This command will do the following:

Cached events cleared!
Compiled views cleared!
Application cache cleared!
Route cache cleared!
Configuration cache cleared!
Compiled services and packages files removed!
Caches cleared successfully!
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.