Mount directory in docker by specifying path as argument

Viewed 23

I have a python script that take two path, one as input folder and other as output folder via sys.argv. For example

python script.py from to

If no path is provided let say python script.py. It take default folder which is from and to.

I have created a docker image and i am mounting my local folder this way

docker run -v "$(pwd):/folder" myimage

As in this case I am not providing folder name argument, it take them by default and put them in folder folder of docker. This is working

But if i want to pass custom path,let how can i do that?

EDIT: Let say here is the code

argl = len(sys.argv)
if argl==1:
    dir_from = 'from'
    dir_to   = 'to'
elif argl == 3:
    dir_from  = sys.argv[1]
    dir_to    = sys.argv[2]

So if i pass python script.py the first if condition will work, and if i pass argument like python script.py abc/from abc/to the second elif condition work.

docker run -v "$(pwd):/folder" myimage This command pick the first condition, but how to pass custom path to it.

For example some thing link that docker run -v "abc/from abc/to:/folder" myimage

1 Answers

Here's how to pass in default values for your from and to path parameters at the time that you launch your container.

Define the two env vars as part of launching your container:

FROM_PATH = <get the from path default from wherever is appropriate>
TO_PATH = <get the from path default from wherever is appropriate>

Launch your container:

docker run -e FROM_PATH -e TO_PATH ... python script.py

You could specify the values for the env vars in the run command itself via something like -e FROM_PATH=/a/b/c. If you don't provide values, the values are assumed to be already defined in local env vars with the same name, as I did above.

Then inside your container code:

argl = len(sys.argv)
if argl==1:
    dir_from = os.getenv('FROM_PATH')
    dir_to   = os.getenv('TO_PATH')
elif argl == 3:
    dir_from  = sys.argv[1]
    dir_to    = sys.argv[2]
Related