gpt4 book ai didi

docker - 在执行Docker运行时传递参数

转载 作者:行者123 更新时间:2023-12-02 18:50:21 29 4
gpt4 key购买 nike

如果我有以下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"]
随着应用程序如下:
#!/usr/bin/python
import sys
print('Here is your param: ', sys.argv[0])
当我调用docker run -it(myimg)时,如何传递参数,使输出成为参数?
例如:
docker run -it (myparam) "testfoo"

would print

Here is your param: testfoo

最佳答案

sys.argv[0]指的是FileName,因此在运行testfoo时不会指望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])
因此,您可以将入口点替换为下面的内容,然后很好地传递运行时参数 testfoo
ENTRYPOINT ["python3","app.py"]
现在传递参数 testfoo
docker run -it --rm my_image testfoo

关于docker - 在执行Docker运行时传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63045130/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com