0

I have a python script python_fun.py and going to prepare a docker file for it, the code needs a directory and a url for downloading some data. I am wondering how I can pass the url path. According to this question I designed the docker as below:

in the python_fun.py I have:

def do_somthing(url, dir_path):
    #do something!

if __name__=="__main__":
    dir_path = sys.argv[1]
    url = sys.argv[2]
    do_somthing(url, dir_path)

I made a shell script as below

#!/bin/bash
while getopts 'i' OPTION; do
    case $OPTION in
        i) dir_pth=$OPTARG;;
        
    esac
done
python python_fun.py $4 $dir_pth

In the dockerfile I call the bash script as below:

RUN bash test.sh  -i /module/testdata/results/ 

and am going to build and run the docker using the below commands:

docker build -t myaccount/docker_name:latest .

But it gives me list index out of range issue in the python script, any idea to fix it?

Sarah
  • 33
  • 6
  • 1
    Can you use the standard Python library [`argparse` module](https://docs.python.org/3/library/argparse.html) and do the command-line parsing in the Python application itself, getting rid of the shell-script wrapper? That would have one layer fewer (and avoid the shell-quoting bugs you currently have). – David Maze Sep 01 '21 at 22:51
  • thanks for the reply, how could I change the dockerfile then? `RUN python python_fun.py -i /module/testdata/results/ ` – Sarah Sep 02 '21 at 13:26
  • 1
    You shouldn't need to spell out `python` if the `python_fun.py` script is executable and starts with the "shebang" line `#!/usr/bin/env python3`. – David Maze Sep 02 '21 at 13:28

0 Answers0