skip to Main Content

If I have the following Dockfile

FROM centos:8
RUN yum update -y
RUN yum install -y python38-pip
COPY . /app
WORKDIR /app
RUN pip3 install -r requirements.txt
ENTRYPOINT ["python3"]
CMD ["app.py"]

With app being the following:

#!/usr/bin/python
import sys
print('Here is your param: ', sys.argv[0])

When I call docker run -it (myimg), how can I pass in a parameter so the output would be the param?

ex:

docker run -it (myparam) "testfoo"

would print 

Here is your param: testfoo

2

Answers


  1. Anything you provide after the image name in the docker run command line replaces the CMD from the Dockerfile, and then that gets appended to the ENTRYPOINT to form a complete command.

    Since you put the script name in CMD, you need to repeat that in the docker run invocation:

    docker run -it myimg app.py testfoo
    

    (This split of ENTRYPOINT and CMD seems odd to me. I’d make sure the script starts with a line like #!/usr/bin/env python3 and is executable, so you can directly run ./app.py; make that be the CMD and remove the ENTRYPOINT entirely.)

    Login or Signup to reply.
  2. sys.argv[0] refer to the FileName so you can not expect testfoo when you run docker run -it my_image testfoo

    The first item in the list, sys.argv[0], is the name of the Python script. The rest of the list elements, sys.argv[1] to sys.argv[n], are the command line arguments 2 through n

    print('Here is your param: file Name', sys.argv[0],'args testfoo:',sys.argv[1])
    

    So you can just replace the entrypoint to below then you are good to pass runtime argument testfoo

    ENTRYPOINT ["python3","app.py"]
    

    Now pass argument testfoo

    docker run -it --rm my_image testfoo
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search