1

I have a python script that i run with the following command : python3 scan.py --api_token 5563ff177863e97a70a45dd4 --base_api_url http://101.102.34.66:4242/scanjob/ --base_report_url http://101.102.33.66:4242/ --job_id 42 This works perfectly when I run it on the command line

IN my Dockerfile , I have tried ARG and ENV . none seem to work

#ARG api_token
#ARG username
#ARG password

# Configure AWS arguments
#RUN aws configure set aws_access_key_id $AWS_KEY \
 #   && aws configure set aws_secret_access_key $AWS_SECRET_KEY \
  #  && aws configure set default.region $AWS_REGION

### copy bash script and change permission
RUN mkdir workspace
COPY scan-api.sh /workspace
RUN chmod +x  /workspace/scan-api.py
CMD ["/python3", "/workspace/scan-api.py"]

so how do i define this flagged argument in docker file ? And whats the command run when running the image ?

3 Answers 3

6

You can do this in two ways as you want to override at run time.

  • As args to Docker run command
  • As an ENV to Docker run command

1st is simplest and you will not need to change anything Dockerfile

docker run --rm my_image python3 /workspace/scan-api.py --bar tet --api_token 5563ff177863e97a70a45dd4 --base_api_url http://101.102.34.66:4242/scanjob/ --base_report_url http://101.102.33.66:4242/ --job_id

and my simple script

import sys
print  ("All ARGs",sys.argv[1:])

enter image description here

Using ENV you will need to change Dockerfile

I am posting the way for one, you can do this for all args

FROM python:3.7-alpine3.9 
ENV API_TOKEN=default_token
CMD ["sh", "-c", "python /workspace/scan-api.py $API_TOKEN"]

So you can override them during run time or have the ability to run with some default value.

docker run -it --rm -e API_TOKEN=new_token my_image
Sign up to request clarification or add additional context in comments.

2 Comments

upvoted for suggesting use of run args to inject these values dynamically. Question that should be asked is: are these values properties of the image itself? Or are they properties of the job that runs in the container? ENV and CMD are for image properties.
Thanks, run args are dynamic and python will consume it as an argument as we do without a container so you can say it not properties of Image it self only if the image container python and ENV is the image property if there is defined that you can use otherwise will not help, while CMD is generic can be overide so not a hard coded property of image as can be overide, as if pass ENV but not configured in a that container can conume so will not help
2

CMD takes exactly the same arguments you used from the command line.

CMD ["/python3", "scan.py", "--api_token", "5563ff177863e97a70a45dd4", "--base_api_url", "http://101.102.34.66:4242/scanjob/", "--base_report_url", "http://101.102.33.66:4242/", "--job_id", "42"]

3 Comments

Eh, just keeping what's in the original question. I assume that's just where python3 is installed in whatever image is being used.
Using python-3.7:alpine image
Then it's /usr/local/bin/python3 or just python3. Absolute /python3 won't work.
0

It's confusing.

You will need to use the SHELL form of ENTRYPOINT (or CMD) in order to have environment variable substitution, e.g.

ENTRYPOINT "/python3","/workspace/scan-api.py","--api-token=${TOKEN}" ...

And then run the container using something of the form:

docker run --interactive --tty --env=TOKEN=${TOKEN} ...

2 Comments

I will like to pass the arugument during cocker run like this; Docker in -it --api_token 5563ff177863ea70a45dd4 --base_api_url http://10.10.66:4242/scanjob/ --base_report_url http://10.10.66:4242/ --job_id 42
There are a couple of ways for you to do this. In my example above, use ENTRYPOINT "python3","/workspace/scan-api.py". This command sets the entrypoint (process) for your container to be /workspace/span-api.py. Then, when you run this container, you'll need to provide necessary flags. You could docker run --interactive --tty [[YOUR-CONTAINER-IMAGE]] --bar tet --api_token 5563ff177863e97a70a45dd4 --base_api_url http://101.102.34.66:4242/scanjob/ --base_report_url http://101.102.33.66:4242/ --job_id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.