dunglas / symfony-docker

A Docker-based installer and runtime for Symfony. Install: download and `docker compose up`.
https://dunglas.dev/2021/12/symfonys-new-native-docker-support-symfony-world/
2.58k stars 767 forks source link

Production issue: environment variable not found "MAILER_DSN" #667

Closed gremo closed 1 month ago

gremo commented 1 month ago

I've the following error when building for production:

1.185 Generated optimized autoload files (authoritative) containing 4185 classes
1.189 + composer dump-env prod
1.257 Successfully dumped .env files in .env.local.php
1.260 + bin/console cache:clear
1.451
1.452 In EnvVarProcessor.php line 221:
1.452
1.452   Environment variable not found: "MAILER_DSN".
1.452
1.452

The issue occurs only when using the following monolog configuration:

when@prod:
    monolog:
        handlers:
            main:
                type: fingers_crossed
                action_level: error
                handler: grouped
                excluded_http_codes: [404, 405]
                buffer_size: 50 # How many messages should be saved? Prevent memory leaks
            grouped:
                type: group
                members: [streamed, deduplicated]
            streamed:
                type: stream
                path: '%kernel.logs_dir%/%kernel.environment%.log'
                level: debug
            deduplicated:
                type: deduplication
                handler: symfony_mailer
            symfony_mailer:
                type: symfony_mailer
                from_email: 'no-reply@example.com'
                to_email: 'ops@example.com'
                subject: 'An Error Occurred! %%message%%'
                level: debug
                formatter: monolog.formatter.html
                content_type: text/html

I've double checked .env.local.php generated by composer dump-env prod and it contains the MAILER_DSN variable. Any help is much appreciated!

maxhelias commented 1 month ago

Have you cleared your opcache?

gremo commented 1 month ago

@maxhelias Do you think it's relevant at build time?

Anyways I removed all OPcache features from Docker file, and still the problem persist.

maxhelias commented 1 month ago

You're right, but without reproducer, I can only throw out ideas. Have your rebuild your docker image without cache ?

gremo commented 1 month ago

@maxhelias Yes, I know. Thanks for your help.

And yes, I completely removed all containers and volumes before rebuilding for production.

However, the issue can easily be reproduced by installing the Mailer component and the Monolog bundle, and simply pasting that configuration.

It seems that the configuration requires the MAILER_DNS environment variable (which makes sense, as it's used for sending emails). For some reason, during the cache clearing process, the generated .env.local.php (which in fact contains MAILER_DNS) file isn't being considered.

The fix is putting ENV MAILER_DSN=null://null in the Dockerfile.

But I'd like to solve it in the right way!

7-zete-7 commented 1 month ago

Hello @gremo!

Are you have declared (and uncommented) MAILER_DSN variable in your .env?

It must be declared automatically when you install symfony/mailer by the https://github.com/symfony/recipes/blob/main/symfony/mailer/4.3/manifest.json recipe.

gremo commented 1 month ago

@7-zete-7 nice catch! It works indeed uncommenting (it's commented by default).

But... why? Can you explain why .env.local.php isn' considered when clearing the cache?

7-zete-7 commented 1 month ago

@gremo, the .env.local.php - is optimized version of the configured environment.

This file generates only for runtime and reflects the same configuration of your .env files. Symfony can avoid this file usage on development or maintenance processes.

Any changes to .env.local.php can (and will) be ignored by not reading it or by overwriting it.