testcontainers / testcontainers-python

Testcontainers is a Python library that providing a friendly API to run Docker container. It is designed to create runtime environment to use during your automatic tests.
https://testcontainers-python.readthedocs.io/en/latest/
Apache License 2.0
1.53k stars 280 forks source link

Feature: Allow setting environment variables with DockerCompose #651

Open GMorris-professional opened 1 month ago

GMorris-professional commented 1 month ago

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.

alexanderankin commented 1 month ago

this makes sense to me

totallyzen commented 1 month ago

hey @GMorris-professional :wave:

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. :slightly_smiling_face:

alexanderankin commented 1 month ago

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