简体   繁体   English

在 Docker 容器内传递 python arguments (argparse)

[英]Pass python arguments (argparse) within Docker container

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 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当我在命令行上运行它时,这非常有效

IN my Dockerfile, I have tried ARG and ENV.在我的 Dockerfile 中,我尝试过 ARG 和 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?那么我如何在 docker 文件中定义这个标记的参数? And whats the command run when running the image?运行映像时运行的命令是什么?

You can do this in two ways as you want to override at run time.您可以通过两种方式执行此操作,因为您希望在运行时覆盖。

  • As args to Docker run command作为 Docker 运行命令的参数
  • As an ENV to Docker run command作为 Docker 运行命令的 ENV

1st is simplest and you will not need to change anything Dockerfile第一个是最简单的,你不需要改变任何东西 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:])

在此处输入图像描述

Using ENV you will need to change Dockerfile使用 ENV,您需要更改 Dockerfile

I am posting the way for one, you can do this for all args我正在发布一种方式,您可以为所有 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

CMD takes exactly the same arguments you used from the command line. CMD与您在命令行中使用的 arguments 完全相同。

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

It's confusing.这很令人困惑。

You will need to use the SHELL form of ENTRYPOINT (or CMD ) in order to have environment variable substitution, eg您需要使用SHELL形式的ENTRYPOINT (或CMD )才能进行环境变量替换,例如

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} ...

HTH!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM