简体   繁体   中英

how to pass argument (file path) to python app in Docker

I have a python application that is expecting first argument as file path. basically a configuration file.

This file it should get form volume/ mount in Docker

How to pass this:

. Snippets Python:

with open(sys.argv[1], 'r') as ymlfile:
    cfg = yaml.load(ymlfile)

Docker file

COPY install.py /wiki/install.py
CMD [ "python", "/wiki/install.py", "/config/config.yml"]

Run image command

sudo docker run -v /config:/home/example/config/ app-wiki

I am expecting config.yml file available at /home/example/config/ will be copied in /config dir and inside Docker file it will be available

but its not working this way.

Where I am going wrong?

The way docker CMD works is you need to make sure that the file is copied into the correct location with correct permissions.

Example:

RUN mkdir -p /config
RUN mkdir -p /wiki
ADD cp <your-location> /config/config.yml
ADD install.py /wiki/install.py
CMD [ "python", "/wiki/install.py", "/config/config.yml"]

Also, docker will keep the same permissions that you have in your local directory. So, make sure you have correct permission set on both files.

The issue is that you have got the direction wrong. The format is <hostpath>:<containerpath>

Below

sudo docker run -v /config:/home/example/config/ app-wiki

should be

sudo docker run -v /home/example/config/:/config app-wiki

Doing the config present in /home/example/config/ will be available in container at /config folder

Edit-1

Added a bit of more explanation to clear you doubts.

COPY install.py /wiki/install.py
CMD [ "python", "/wiki/install.py", "/config/config.yml"]

When you run the above image it will expect a config to be available at /config/config.yml .

Now if you have a folder on your host /home/tarun/wikiconfig which has a config.yml file then you run the container using

sudo docker run -v /home/tarun/wikiconfig:/config app-wiki

If the name of the config.yml file is different in your wikiconfig folder, then you will mount the file to config.yml

sudo docker run -v /home/tarun/wikiconfig/myconfig.yml:/config/config.yml app-wiki

Both would override the config you added when you build the Dockerfile, because when you a mount a folder or file from host, anything with the same path inside container is no more accessible to the image

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