miniature-apartment-8295
09/06/2023, 12:42 PMpants run
and environment variables. I want to run a container and pass environment to the python script which should run inside the container.
I want to execute this from another python script. I tried the following:
envs = ['env_1 = "value_1"', 'env_2="value_2"']
docker_run_args = '-e' + '-e'.join(envs)
command = ["pants", "run", f'--docker-run-args="{docker_run_args}"', "path/to/Dockerfile"]
subprocess.run(command, check=True)
When I look inside the container I see that instead of the desired envs I have one single env:
env_1: value_1 -e value_2
Does anyone know what I need to change or even have a better solution? The envs list is actually much bigger and automatically generated within the python script.
I also checked the command by printing it: ['pants', 'run', '--docker-run-args="-e env_1="value_1" -e env_2="value_2""', 'path/to/Dockerfile']
cold-vr-15232
09/06/2023, 12:57 PM--docker-run-args="-e env_1="value_1" -e env_2="value_2""
miniature-apartment-8295
09/06/2023, 12:58 PMcurved-television-6568
09/06/2023, 2:44 PMdocker-run-args
takes a list of args. The docs will make this more clear: https://www.pantsbuild.org/docs/reference-docker#run_argscurved-television-6568
09/06/2023, 2:45 PMcurved-television-6568
09/06/2023, 2:46 PMminiature-apartment-8295
09/06/2023, 2:48 PMcurved-television-6568
09/06/2023, 4:00 PMshlex.split
on it to determine the individual args, so quoting is important here.
The meat of the parsing for list options are here: https://github.com/pantsbuild/pants/blob/70c0d45574de6740b154911e38fc4d27729916bc/src/python/pants/option/custom_types.py#L279-L325 with shell_str lists being handled here: https://github.com/pantsbuild/pants/blob/70c0d45574de6740b154911e38fc4d27729916bc/src/python/pants/option/custom_types.py#L194-L199enough-analyst-54434
09/06/2023, 4:22 PM"--docker-run-args=--env", "--docker-run-args=<var>=<value>", ...
in pairs like that. Any Pants list option can be specified multiple times and each time will push a value on an internal list. So that would emulate passing docker run --env <var>=<value> --env <var>=<value> ...
.enough-analyst-54434
09/06/2023, 4:23 PM