Pass environment to add docker to symfony config
I want to pass config 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 am getting a 500 error "Environment variable not found: DATABASE_HOST"
I also tried SYMFONY__DATABASE_HOST in docker-compose but also didn't work.
How it works?
The error you are getting is about Symfony environment variables (see here) . Docker creates environment variables fetched from a file .env
that is inside the build context (AKA is the directory you want docker-compose from) from a file docker-compose.yml
. What you really want to do is make sure that the environment variables that you set in the symfony config / options and your file docker-compose.yml
match in particular the db node.
You should consider hacking images into PHP-FPM and Nginx images to have the best potential scalability and separation of concerns according to the best practices outlined by Docker. All of these technologies have regularly maintained images available on Docker Hub .
Here's my docker-compose.yml
which creates containers for PHP-FPM, MySQL, and Nginx in a multi-container environment with the latest stable packages available 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 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 check out my docker-symfony maxpou repository fork here . I updated the docker-compose.yml and set the bindings to be compatible with version 3 of the Compose file format.