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
Anything you provide after the image name in the
docker run
command line replaces theCMD
from the Dockerfile, and then that gets appended to theENTRYPOINT
to form a complete command.Since you put the script name in
CMD
, you need to repeat that in thedocker run
invocation:(This split of
ENTRYPOINT
andCMD
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 theCMD
and remove theENTRYPOINT
entirely.)sys.argv[0]
refer to theFileName
so you can not expecttestfoo
when you rundocker run -it my_image testfoo
So you can just replace the entrypoint to below then you are good to pass runtime argument
testfoo
Now pass argument
testfoo