Hi, I have a question concerning `pants run` and ...
# general
m
Hi, I have a question concerning
pants 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:
Copy code
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']
c
Aren't the quotes here messing with each other?
--docker-run-args="-e env_1="value_1" -e env_2="value_2""
m
I think the quotes are the problem but I don't see how to solve it. I think I tried all possible combinations of quotes.
c
The
docker-run-args
takes a list of args. The docs will make this more clear: https://www.pantsbuild.org/docs/reference-docker#run_args
although the example in the help text seems contradictory..
so I agree your quotes may be messing this up for you, but in any case, you could always insert the square brackets to help pants to the right thing here, I believe..
m
So the example in the text is wrong and I actually should pass a list of args? I was trying to follow exactly that example.
c
well no, the example is correct, but when you pass it a single string value, it will do
shlex.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-L199
e
It's probably way simpler to reason about to just pass
"--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> ...
.
👍 1
And then you don't have to reason about quoting - hopefully.