6

I want to pass configuration parameters from docker-compose file to my Symfony application, I tried this:

app:
    image: <NGINX + PHP-FPM IMAGE>
    environment:
        DATABASE_HOST: db

My parameters.yml file :

database_host: "%env(DATABASE_HOST)%"

I get error 500 "Environment variable not found: DATABASE_HOST"

I also tried SYMFONY__DATABASE_HOST in docker-compose but also not working.

How does it work?

5
  • DATABASE_HOST=db Commented Mar 25, 2017 at 1:15
  • Configuration looks totally valid. Do you see environment variable set with docker inspect <container>? Commented Mar 25, 2017 at 5:54
  • @johnharris85 if he's using Docker Compose version 3 or 2 both DATABASE_HOST: db and - DATABASE_HOST=db are valid according to the documentation here Commented Mar 26, 2017 at 22:27
  • So they are! Thanks :) Commented Mar 28, 2017 at 1:15
  • Hello, were you able to make this work? I did the same thing. I can see my env variables when I type 'docker inspect' and also inside the container when I type 'env'. However, when I set "%env(DATABASE_USER)%" on the parameters.yml Symfony is not able to find it. Commented May 17, 2017 at 13:44

1 Answer 1

3

The error you're receiving refers to Symfony runtime environment variables (see here). Docker compose environment variables are retrieved from the .env file that resides within the build context (AKA the directory where you want docker-compose to run from) of the docker-compose.yml file. What you really want to do is make sure that the environment variables you set in your Symfony config/parameters and your docker-compose.yml file match, especially the db host.

You should consider breaking your image up into PHP-FPM and Nginx images to have better potential scalability and a separation of concerns according to the best practices outlined by Docker here. All of these technologies have regularly-maintained images available on Docker Hub.

Here's my docker-compose.yml that creates containers for PHP-FPM, MySQL, and Nginx in a multicontainer environment with the latest stable packages available as of today.

version: '3'

services:
    db:
        image: mysql
        volumes:
            - ./.data/db:/var/lib/mysql
        environment:
            MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
            MYSQL_DATABASE: ${MYSQL_DATABASE}
            MYSQL_USER: ${MYSQL_USER}
            MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    php:
        build: php7.1-fpm
        depends_on:
            - db
        ports:
            - "9000:9000"
        volumes:
            - symfony-bind-mount:/var/www/symfony
            - symfony-logs-bind-mount:/var/www/symfony/app/logs
    nginx:
        build: nginx
        depends_on:
            - php
        ports:
            - "8025:8025"
            - "80:80"
        volumes:
            - symfony-bind-mount:/var/www/symfony
            - nginx-logs-bind-mount:/var/log/nginx

volumes:
    symfony-bind-mount:
        driver: local
        driver_opts:
            o: bind,rw
            type: none
            device: ${SYMFONY_APP_PATH}
    nginx-logs-bind-mount:
        driver: local
        driver_opts:
            o: bind,rw
            type: none
            device: ${DOCKER_SYMFONY_PATH}/logs/nginx
    symfony-logs-bind-mount:
        driver: local
        driver_opts:
            o: bind,rw
            type: none
            device: ${DOCKER_SYMFONY_PATH}/logs/symfony

Here's my .env file:

# Symfony application's path
SYMFONY_APP_PATH=/absolute/path/to/symfony/project

# Path to local docker-symfony repository
DOCKER_SYMFONY_PATH=/absolute/path/to/sibling/docker/directory

# MySQL
MYSQL_ROOT_PASSWORD=root
MYSQL_DATABASE=mydb
MYSQL_USER=
MYSQL_PASSWORD=password

You can checkout my fork of maxpou's docker-symfony repository here. I've updated the docker-compose.yml and the bind mounts to be compatible with version 3 of the Compose file format.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, you're right about the .env file, It works with it. But I think it's too much for my need because Symfony reads .env file on each request, on production not good for performance. Is there a way to "build" parameters.yml with docker-compose environment variables? Also thanks on the advice about my compose file, the thing is I build and push my application on my own private registry. I use one image with PHP-FPM + NGINX + CODE SOURCE. I don't know how to manage this with multiple containers.

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.