Flex: cache-clear exception when env vars are not set

Created on 23 Sep 2017  路  3Comments  路  Source: symfony/flex

Hello.
I building a small demo using Symfony flex and I am using Docker to build reproducible images in dev and production environments.
I am using Doctrine dbal and i am reading the database configs from environment variables like recommended:

 dbal:
        default_connection:   default
        connections:
            # A collection of different named connections (e.g. default, conn2, etc)
            default:
                dbname: '%env(MYSQL_DATABASE)%'
                host:   '%env(MYSQL_HOST)%'
                port:   '%env(MYSQL_PORT)%'
                user:   '%env(MYSQL_USER)%'
                password: '%env(MYSQL_PASSWORD)%'
                charset: utf8mb4

The problem is when building the application using docker build its runs ``composer install, but during cache:warmup it gives an exception:

!    [Symfony\Component\DependencyInjection\Exception\EnvNotFoundException]  
!!    Environment variable not found: "MYSQL_DATABASE".     

This happens because environment variables are only set in runtime and not at build time in Docker. Also the .env file i am using in dev is not copied to the build.
I see some people running composer install, on Entrypoint command instead which wouldn't have this problem, but I dont like that solution. The built docker image should contain all the application code, including vendors and not be added when the container starts.

Its possible somehow to provide a fallback if env not found, set the value as empty string for example instead of throwing an Exception.. I can declare all the required ENV vars in the Dockerfile with empty value but that would not look very good also.

I still think composer install should not depend on the environment. Maybe, I can remove the cache-warmup command from composer.json and that one yes, I think it makes sense to be run at entry point command, still wouldn't be a standard way., since Symfony runs that command by default after each composer run.

What are your thoughts about this?

Most helpful comment

All 3 comments

Run composer using composer install --no-scripts, this will ignore the cache warming (you will need to this manually afterwards).

@sstok Yes, I discovered that later. I think this issue can be closed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

derrabus picture derrabus  路  8Comments

timonf picture timonf  路  8Comments

fabpot picture fabpot  路  7Comments

javiereguiluz picture javiereguiluz  路  6Comments

javiereguiluz picture javiereguiluz  路  5Comments