简体   繁体   中英

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. The quoted string is split as two independent items of the sys.argv vector.

The expected behaviour of the python script is as follows.

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 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

['--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'

My sample dockerfile definition is as follows

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

Did you also tried 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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