Home > Software design >  Is there a way to provide docker-compose env-file from stdin?
Is there a way to provide docker-compose env-file from stdin?

Time:09-23

In docker run one can do

docker run --env-file <(env | grep ^APP_) ...

Is there a similar way for docker-compose? I would like to avoid physical env file.

CodePudding user response:

The equivalent of --env-file option of the docker cli in docker-compose is the env_file configuration option in the docker-compose file. But I think this requires a physical .env file.

If you want use the environment variables of your host machine, you can define them in docker-compose (with an optional fallback value):


version: "3.9"
services:
  app:
    image: myapp
    environment:
    - APP_MYVAR=${APP_MYVAR-fallbackvalue}

It's not so convenient as doing a grep of your ^APP_ vars, but one way to avoid the physical file.

CodePudding user response:

You can do it by supplying multiple compose files, as documented here. In your case the first one is a physical docker-compose.yml and the second one a Compose containing only the environment variables for the needed service. Obviously, env variables must be properly formatted, so a sed that prepends the string - is necessary because they are added as YAML list.

docker-compose -f docker-compose.yml -f <(printf "services:
  your_service:
    environment:\n$(env | grep ^APP_ | sed -e "s/^/    - /")"
) up

This is how Docker behaves:

When you supply multiple files, Compose combines them into a single configuration. Compose builds the configuration in the order you supply the files. Subsequent files override and add to their predecessors

  • Related