Skip to content
Advertisement

Run Docker container and passing a single option with multiple arguments to a python script

I am trying launch a Docker container that has as an entrypoint a python script that accepts a BBOX as an argument. I use a Bash script in order to automate the above.

Where the BBOX should be of this form: lonmin latmin lonmax latmax

Below is the Bash script

run.sh

JavaScript

Where <docker_image_name:tag> is a docker image.

The arguments are passed into the following python script: core.py

JavaScript

If I ran the following command it fails ./run.sh -a 3 33 5 40 and raises the following error:

argument –bbox: expected 4 arguments

NOTE! The following command is successful and it wont raise any errors python core.py --bbox 3 33 5 40

EDIT The command ./run.sh -a "3 33 5 40" passes the arguments as a single string. Echoing the arguments in the Bash script:

JavaScript

But it still raises the same error when it is passed to the python script.


Solution:

JavaScript

Advertisement

Answer

Based on the provided bash example, bbox="${OPTARG}" is always going to be an empty string as getopts ":a" defined a to be a simple flag (refer to this answer and associated posting on that thread for additional help). Even if you define getopts ":a:" such that -a will then accept an argument, calling ./run.sh -a 3 33 5 40 will result in ${bbox} having just 3 as the value (you can verify this yourself by echoing out the value e.g. echo "bbox=${bbox}") as again, it will only consume one argument. Essentially the Python program was being invoked with bbox flag set to a single empty string argument.

Since the arguments are going to be simple numbers without spaces, I will opt to pass them as a single quoted string in the bash version and then pass them unquoted to Python where its argparse will be able to handle the arguments separately, i.e.

File: run.sh

JavaScript

File: core.py

JavaScript

Execution:

JavaScript

If you must pass the arguments to bash as separate arguments (i.e. not a single quoted argument like in the above success example and as the first failure example due to lack of quotes), you will need to do one of the following suggestions found in the various answers in the following threads and adapt the script to pass the correct arguments to Python:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement