Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Allow setting environment variables with DockerCompose #651

Open
GMorris-professional opened this issue Jul 23, 2024 · 3 comments

Comments

@GMorris-professional
Copy link

What are you trying to do?
I would like to be able to build and run docker compose containers using environment variables not stored in an env file. We are trying to test spinning up n database containers where n is configurable. Each database has its own name and root credentials which are generated dynamically so we cannot hard code them in env files.

Why should it be done this way?

This enhancement would allow users to define the environment variables for a docker compose service at runtime. Gives the library more flexibility.

Other references:

Docker Compose Environment Variable Options.

@GMorris-professional GMorris-professional changed the title Feature: Allow for the use of individual environment variables with DockerCompose Feature: Allow setting environment variables with DockerCompose Jul 23, 2024
@alexanderankin
Copy link
Collaborator

this makes sense to me

@totallyzen
Copy link
Collaborator

hey @GMorris-professional 👋

You already can do this via compose.yml. It's part of the compose architecture that we call out to.
My recommendation would be to use the environment attribute https://docs.docker.com/compose/environment-variables/set-environment-variables/#use-the-environment-attribute
Compose will make use of the shell's env variables if you specify it in the compose.yml.

The problem with your proposal on the other hand is if you inject -e flags into the compose command we call, and any of those commands fail (say, you did my_compose.exec_in_container() then the secrets will likely leak into your CI, thus exposing said secrets. That's not a good way to proceed.

Because compose itself supports setting these variables through the compose.yml and that support environment substitution on top of that, I recommend using those instead of manipulating the env flags on call.

Remember! Since the docker world moved to Go, we don't talk to the api directly for compose, but use the CLI as a bridge. 🙂

@alexanderankin
Copy link
Collaborator

thats right, similar pattern for docker run cli command:

$ export SOME_VAR=hello
$ docker run --rm -it --entrypoint '' alpine:3.16 sh -c 'echo $SOME_VAR'

$ docker run --rm -it --entrypoint '' -e SOME_VAR alpine:3.16 sh -c 'echo $SOME_VAR'
hello

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants