Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass arguments to Python argparse within Docker container

I am embarking on my first attempt at utilizing a docker container. I have a python script that calls a couple API's and parses a file. The script took parameters for the URL of the server for the API, the API key, and the file path for the file to parse. I used argparse to handle these in the script.

How do I get get these passed into Docker? I do not want to hard code anything as I am looking to hand this script off to an engineer who needs to run this periodically and take action based on the results.

Thanks for your help. I have been searching but it seems like hard coding things into the dockerfile is the suggestion - I want the user to be able to put these in at run time. Or perhaps I have found the answer and am just not understanding it....

I apologize if my lingo is not right - this is my first attempt at utilizing Docker.

like image 307
Bring Coffee Bring Beer Avatar asked Sep 15 '17 18:09

Bring Coffee Bring Beer


People also ask

How do you pass ARG Docker?

If you want to pass multiple build arguments with docker build command you have to pass each argument with separate — build-arg. docker build -t <image-name>:<tag> --build-arg <key1>=<value1> --build-arg <key2>=<value2> .

How do you pass arguments to a Python script?

In Python, arguments are passed to a script from the command line using the sys package. The argv member of sys ( sys. argv ) will store all the information in the command line entry and can be accessed inside the Python script. Python's getopt module can also be used to parse named arguments.


2 Answers

The way you do it depends on how your using docker. If you want to run your script in an already running container, you can use exec:

docker exec <yourContainerName> python <yourScript> <args>

Alternatively, if you have a docker image where your script is the ENTRYPOINT, any arguments you pass to the docker run command will be added to the entrypoint.

So, if your docker file looks like this:

FROM yourbase
....
ENTRYPOINT <yourScript>

Then you can just run the script by running the container itself:

docker run --rm <yourImageName> <args>

Based on your comment below, it looks like you want this option. You should change your dockerfile to specify

ENTRYPOINT ["python","./script.py"]

(instead of using CMD) and then you can run via:

docker run --rm <yourImageName>  -a API_KEY - f FILENAME -o ORG_ID
like image 86
Chris Avatar answered Oct 17 '22 15:10

Chris


So let's assume your command is below

python app.py "URL" "APIKEY" "filepath"

So you will put your Dockerfile in below fashion

FROM python:3.6
WORKDIR /app
COPY app.py .
ENTRYPOINT ["python", "app.py"]

Then the person running the docker container would do it like below

docker run -v /home/tarun/project/filetoparse.yaml:/config.yaml <yourimagename> "URL" "APIKEY" /config.yaml

If you want to give more flexibility you an can even use environment variables

docker run -v /home/tarun/project/filetoparse.yaml:/config.yaml -e APIKEY="XYZ" <dockerimage> "URL" /config.yaml

And then in your script you can read it using os.environ['APIKEY']

like image 35
Tarun Lalwani Avatar answered Oct 17 '22 15:10

Tarun Lalwani