简体   繁体   English

无法将引用的字符串传递给docker容器中的python脚本

[英]Unable to pass quoted strings to the python script within a docker container

Unable to pass quoted strings to the python script within a docker container. 无法将引用的字符串传递给docker容器中的python脚本。 The quoted string is split as two independent items of the sys.argv vector. 引用的字符串被拆分为sys.argv向量的两个独立项。

The expected behaviour of the python script is as follows. python脚本的预期行为如下。

check.py --coverage 'Hello World' --blabla something
All Raw Arguments
['--coverage', 'Hello World', '--blabla', 'something']
All Args
Namespace(blabla='something', coverage='Hello World')

Print Coverage
Hello World

Print blabla
something

The same script when I have it within the docker container and run using docker run , the text inside quotes is split into multiple vars of the list. 当我在docker容器中使用相同的脚本并使用docker run ,引号内的文本被拆分为列表的多个vars。

docker run --rm -i sample --coverage 'Hello World' --blabla something
All Raw Arguments
['--coverage', 'Hello', 'World', '--blabla', 'sometext']
usage: check.py [-h] [--coverage COVERAGE] [--blabla BLABLA]
check.py: error: unrecognized arguments: World

Note The difference in the sys.argv vector when the script is run normally and from within the docker 注意脚本正常运行时和syser中的sys.argv向量之间的差异

['--coverage', 'Hello World', '--blabla', 'something']
['--coverage', 'Hello', 'World', '--blabla', 'sometext']

In the first instance it is only one item 'Hello World', whereas when the script is inside docker it is 'Hello', 'World' 在第一个实例中它只有一个项目'Hello World',而当脚本在docker中时它是'Hello','World'

My sample dockerfile definition is as follows 我的示例dockerfile定义如下

FROM sample_base

ENTRYPOINT ["/check.py"]
CMD ["--help"]

Breaking my head for the past couple of hours. 在过去的几个小时里打破了我的脑袋。 Any help is much appreciated 任何帮助深表感谢

Tried all different combinations such as follows. 尝试了所有不同的组合,如下所示。

docker run --rm -i sample '--coverage "Hello\\\ World" --blabla sometext'
docker run --rm -i sample --coverage "Hello\\\ World" --blabla sometext
docker run --rm -i sample --coverage "Hello\ World" --blabla sometext
docker run --rm -i sample --coverage \"Hello World\" --blabla sometext
docker run --rm -i sample --coverage \"\"Hello World\"\" --blabla sometext
docker run --rm -i sample --coverage "\"\"Hello World\"\"" --blabla sometext
export COVERAGE="Hello World"
docker run --rm -i sample --coverage $COVERAGE --blabla sometext
docker run --rm -i sample --coverage ${COVERAGE} --blabla sometext
docker run --rm -i sample --coverage "$COVERAGE" --blabla sometext
docker run --rm -i sample --coverage "${COVERAGE}" --blabla sometext

Note All the above combination were tried with single quotes (') as well. 注意所有上述组合也尝试使用单引号(')。

Try this: 尝试这个:

docker run --rm -i sample sh -c '/check.py --coverage $COVERAGE --blabla sometext'

or bash 或者bash

Did you also tried docker run --rm -i sample --coverage "\\"Hello World\\"" --blabla sometext ? 你有没有尝试过docker run --rm -i sample --coverage "\\"Hello World\\"" --blabla sometext

I've had some problems with spaces using docker too and had to escape the quotes even though the code interpreter marked it as an error. 我使用docker的空间也遇到了一些问题,即使代码解释器将其标记为错误,也必须转义引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM