58

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.

jwodder
  • 54,758
  • 12
  • 108
  • 124
Bring Coffee Bring Beer
  • 1,035
  • 3
  • 11
  • 19

4 Answers4

75

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
Chris
  • 22,923
  • 4
  • 56
  • 50
  • 1
    So my python script is the reason for the container. I just have: "CMD ["python","./script.py"]" in the Dockerfile but I want to pass "-a API_KEY -f FILENAME -o ORG_ID" – Bring Coffee Bring Beer Sep 15 '17 at 19:04
  • 6
    It might be important to mention that you need to use the exec form `ENTRYPOINT ["python","./script.py"]` instead of the shell form `ENTRYPOINT python ./script.py` to be able to propagate the parameter(s) into python. – naeschdy Jul 07 '21 at 16:43
  • @naeschdy yes, that's what was bothering me. you're right – jasper Nov 17 '21 at 01:05
  • i really like the entrypoint you can build then is really like using your program locally just adding the "docker run " instead "python .py" in the cli – pelos May 10 '22 at 16:40
37

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']

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
21

This answer is kind of late but for any future readers, i would like to make it more towards the question asked i.e. with respect to argparse.

The basic idea like @Chris pointed out is it. One way to achieve the solution is to pass arguments to the image in docker run command itself. These arguments would then be passed to your ENTRYPOINT, therefore passing to the python script.

The files would look something like this typically..

file.py

import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('pos', type=str, help='Example Positional Argument') # will be accesible under args.POS
parser.add_argument('--opt', type=str , help='Example Optional Argument') # will be accesible with args.OPT

args = parser.parse_args()

# do something with pos and OPT

Without docker, You would run this file (assuming it is in the pwd) as python file.py --opt opt_val pos_val

Dockerfile

FROM python:<your_tag>
COPY ./file.py ./ # Assuming your Dockerfile and file.py are in the same directory

# some custom build steps

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

Docker build and run commands

You build with this : docker build --tag example:0.0.1 <dir>

The below shows multiline(for better readability) run commands,

Docker run

docker run --rm \
   --name example.container \
   example:0.0.1 \
   --opt=opt_val \
   POS=pos_value

Docker run (powershell)

docker run --rm `
   --name example.container `
   example:0.0.1 `
   --opt=opt_val `
   POS=pos_value

So here are some points to remember:

  • Argparse has support for adding positional and optional arguments and should be passed accordingly to the image in the docker run command.
  • The solution pointed out above works but is not as flexible as id generally like it to be. Better to use Environment variables and access inside the script with os.environ().
  • With this solution you dont "hard-code" anything to the Dockerfile
AvidJoe
  • 596
  • 5
  • 19
15

Inside Dockerfile, i use the CMD command like this:

FROM python:3
COPY ./app /app
WORKDIR /app
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r req.pip
CMD ["python","start.py","(api-url) ","(api-key)","(file-path)"]

Note Per each args/params, separate with coma

If you are using flags, you will need to split

CMD ["python","start.py","-u","(api-url) ","-k","(api-key)","-f","(file-path)"]
cryanbhu
  • 4,780
  • 6
  • 29
  • 47
Alex Montoya
  • 4,697
  • 1
  • 30
  • 31
  • I think the question was looking for a solution to flexibly pass the arguments when running docker, instead of hard-coding them in the Dockerfile. – Memphis Meng Apr 30 '23 at 01:28