Environment variables not found symfony

10,693

Solution 1

If you extend from the official PHP Docker image, it sets clear_env = no for you or you can set it yourself in your image's pool config.

Here's the line adding clear_env = no to the config in the official image. You just need to add this to your FPM pool config, or you can add them one-by-one if you prefer as shown by Robert.

Solution 2

I've run into this problem myself. You have to explicitly map the environment variables you want to make accessible to php/symfony in php-fpm.conf like:

[www]
env[MY_ENV_VAR_1] = 'value1'
env[MY_ENV_VAR_2] = 'value2'

However that doesn't seem to work with actual enviornment variables from the host!.

There is a long discussion of that here (along with several, what seem to me, laborious work-arounds to the problem: https://groups.google.com/forum/#!topic/docker-user/FCzUbjTIp_0

I've successfully done it in the pool.d configuration file like so:

env[DATABASE_HOST] = $DATABASE_HOST
env[DATABASE_PORT] = $DATABASE_PORT
env[DATABASE_NAME] = $DATABASE_NAME

I just add this in as part of the docker file:

ADD fpm/app.pool.conf /etc/php5/fpm/pool.d/
Share:
10,693
klsdskldsd
Author by

klsdskldsd

Updated on June 04, 2022

Comments

  • klsdskldsd
    klsdskldsd almost 2 years

    I have a docker container :

    services:
    php-fpm:
        build:
            context: ./docker/php-fpm
    
        volumes:
            - ./symfony:/home/home
        container_name: php
        ports:
            - "9004:9001"
        networks:
            - local
        working_dir: /home/home
        environment:
            - DATABASE_HOST=test
            - DATABASE_PORT=
            - DATABASE_NAME=test
    

    I made :

    1. docker-compose build --no-cache
    2. docker-compose up
    3. clear cache

    When I refresh the page I get : Environment variable not found: "DATABASE_HOST".. What's the problem I don't understand. I spent a lot of time analyze this issue. Have you an idea about that ? Thx in advance. Btw when I do docker inspect I see all this environment variables assigned.

    • schildi
      schildi almost 6 years
    • Gregoire Ducharme
      Gregoire Ducharme almost 6 years
      You might be mixing Docker environment var which are like your system var (like Unix ENV) and your Symfony var ? Could you show where you're trying to use DATABASE_HOST ?