将参数传递给Docker容器中的Python argparse [英] Pass arguments to Python argparse within Docker container

查看:132
本文介绍了将参数传递给Docker容器中的Python argparse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试利用Docker容器。我有一个Python脚本,可以调用几个API并解析文件。该脚本采用了用于API的服务器URL,API密钥以及要解析的文件的文件路径的参数。我使用argparse在脚本中处理了这些。

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.

我如何将它们传递给Docker?我不想硬编码任何东西,因为我希望将此脚本交给需要定期运行此脚本并根据结果采取措施的工程师。

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.

感谢您的帮助。我一直在搜索,但似乎建议将内容硬编码到dockerfile中-我希望用户能够在运行时将它们放入。也许我已经找到了答案,但只是不理解。...

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

如果我的行话不正确,我深表歉意-这是我首次尝试使用Docker。

I apologize if my lingo is not right - this is my first attempt at utilizing Docker.

推荐答案

操作方式取决于您使用docker的方式。如果要在已运行的容器中运行脚本,则可以使用exec:

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>

或者,如果您有一个docker image,脚本是ENTRYPOINT,则传递给

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.

因此,如果您的docker文件如下所示:

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>

根据下面的评论,您似乎希望使用此选项。您应该更改dockerfile以指定

Based on your comment below, it looks like you want this option. You should change your dockerfile to specify

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

(而不使用CMD)
,然后可以通过以下方式运行:

(instead of using CMD) and then you can run via:

docker run --rm <yourImageName>  -a API_KEY - f FILENAME -o ORG_ID

这篇关于将参数传递给Docker容器中的Python argparse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆