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.47k stars 733 forks source link

.env.prod.local not picked up on docker compose up #628

Open fleonardelli opened 2 months ago

fleonardelli commented 2 months ago

In the docs https://github.com/dunglas/symfony-docker/blob/main/docs/production.md there last section says that you can pass env variables adding the .env.prod.local env_file to compose.prod.yaml.

I've added it, and created .env.prod.local:

SERVER_NAME=your-domain-name.example.com
APP_SECRET=ChangeMe
CADDY_MERCURE_JWT_SECRET=ChangeThisMercureHubJWTSecretKey

So now instead of running:

SERVER_NAME=your-domain-name.example.com \
APP_SECRET=ChangeMe \
CADDY_MERCURE_JWT_SECRET=ChangeThisMercureHubJWTSecretKey \
docker compose -f compose.yaml -f compose.prod.yaml up -d --wait

I want to run docker compose -f compose.yaml -f compose.prod.yaml up -d --wait

But I keep getting: WARN[0000] The "CADDY_MERCURE_JWT_SECRET" variable is not set. Defaulting to a blank string. WARN[0000] The "CADDY_MERCURE_JWT_SECRET" variable is not set. Defaulting to a blank string.

If I run it CADDY_MERCURE_JWT_SECRET=ChangeThisMercureHubJWTSecretKey docker compose -f compose.yaml -f compose.prod.yaml up -d --wait

The container stops restarting, but if I run export within the container, I can see the SERVER_NAME is the one from the .env variable and not from the .env.prod.local

I can't figure out how to make it pick up .env.prod.local.

7-zete-7 commented 2 months ago

Hello @fleonardelli!

You can say to Docker Compose to use non-default .env-file by setting --env-file option or by using the env_file attribute in (one of) compose.yaml file (see https://docs.docker.com/compose/environment-variables/set-environment-variables/).

maxhelias commented 2 months ago

Can you provide a reproducer pls ?

fleonardelli commented 2 months ago

Run: git clone https://github.com/dunglas/symfony-docker.git .

Modify the compose.prod.yaml to:

# Production environment override
services:
  php:
    env_file:
      - .env.prod.local
    build:
      context: .
      target: frankenphp_prod
    environment:
      APP_SECRET: ${APP_SECRET}
      MERCURE_PUBLISHER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET}
      MERCURE_SUBSCRIBER_JWT_KEY: ${CADDY_MERCURE_JWT_SECRET}

Add .env.prod.local:

SERVER_NAME=:80
APP_SECRET=ChangeMe
CADDY_MERCURE_JWT_SECRET=ChangeThisMercureHubJWTSecretKey

Now run: docker compose -f compose.yaml -f compose.prod.yaml up -d --wait

It will output: WARN[0000] The "APP_SECRET" variable is not set. Defaulting to a blank string. WARN[0000] The "CADDY_MERCURE_JWT_SECRET" variable is not set. Defaulting to a blank string. WARN[0000] The "CADDY_MERCURE_JWT_SECRET" variable is not set. Defaulting to a blank string. [+] Running 0/1

@maxhelias I guess it's enough. Because of that if I request the server it will always print phpinfo() output.

7-zete-7 commented 2 months ago

@fleonardelli when you configure both env_file attribute and set the values of environment variables to be mandatory, Docker Compose will still warn about missing values for environment variables from the host, even if these values are already set in the env_file attribute file.

If there are required parameters in environment attribute, they will not be subtracted from the env_file attribute file. The values of environment variables are passed through to the container.

If you want to be able to convert environment variables (as is done in this template), then you must use the --env-file parameter or the COMPOSE_ENV_FILES environment variable when running the docker compose command.

The documentation page has a table of results when there are multiple sources for environment variable values. However, there is no example of what the result will be if both parameters are present (in attribute env_file and in attribute environment).