英文:
How can I correct this Dockerfile to take arguments properly?
问题
我有这个Dockerfile内容:
FROM python:latest
ARG b=8
ARG r=False
ARG p=1
ENV b=${b}
ENV r=${r}
ENV p=${p}
# 许多成功设置的行
ENTRYPOINT python analyze_all.py /analysispath -b $b -p $p -r $r
我的意图是在命令行接受三个参数,如下所示:
docker run -it -v c:\analyze containername -e b=16 -e p=22 -e r=False
但不幸的是,我对某些基本和简单的东西有所误解,而不是复杂的东西,所以我感到无助 :).
英文:
I have this dockerfile content:
FROM python:latest
ARG b=8
ARG r=False
ARG p=1
ENV b=${b}
ENV r=${r}
ENV p=${p}
# many lines of successful setup
ENTRYPOINT python analyze_all.py /analysispath -b $b -p $p -r $r
My intention was to take three arguments at the command line like so:
docker run -it -v c:\analyze containername -e b=16 -e p=22 -e r=False
But unfortunately, I'm misunderstanding something fundamental and simple here instead of something complicated, so I'm helpless :).
答案1
得分: 0
如果我理解问题正确,这个Dockerfile应该可以满足要求:
FROM python:latest
# test script that prints argv
COPY analyze_all.py /
ENTRYPOINT ["python", "analyze_all.py", "/analysispath"]
启动:
$ docker run -it test:latest -b 16 -p 22 -r False
sys.argv=['analyze_all.py', '/analysispath', '-b', '16', '-p', '22', '-r', 'False']
看起来你的Dockerfile是设计用于在Windows上构建和运行容器的。我在Linux上测试了我的Dockerfile,这种方法在Windows上使用可能不会有太大区别。
我认为在这种情况下不需要ARG
指令,因为它定义了一个用户可以在构建时使用docker build
命令传递的变量。我还建议您查看Dockerfile参考中的ENTRYPOINT指令:
docker run
的命令行参数将附加在exec形式的ENTRYPOINT中的所有元素之后,并将覆盖使用CMD指定的所有元素。这允许将参数传递给入口点,即docker run -d将将-d参数传递给入口点。
此外,这个问题可能对你有帮助:https://stackoverflow.com/questions/37904682/how-do-i-use-docker-environment-variable-in-entrypoint-array
英文:
If I understood the question correctly, this Dockerfile should do what is required:
FROM python:latest
# test script that prints argv
COPY analyze_all.py /
ENTRYPOINT ["python", "analyze_all.py", "/analysispath"]
Launch:
$ docker run -it test:latest -b 16 -p 22 -r False
sys.argv=['analyze_all.py', '/analysispath', '-b', '16', '-p', '22', '-r', 'False']
Looks like your Dockerfile is designed to build and run a container on Windows. I tested my Dockerfile on Linux, it probably won't be much different to use this approach on Windows.
I think ARG
instructions isn't needed in this case because it defines a variable that users can pass at build-time using the docker build
command. I would also suggest that you take a look at the Dockerfile reference for the ENTRYPOINT
instruction:
> Command line arguments to docker run <image> will be appended after all elements in an exec form ENTRYPOINT, and will override all elements specified using CMD. This allows arguments to be passed to the entry point, i.e., docker run <image> -d will pass the -d argument to the entry point.
Also, this question will probably be useful for you: https://stackoverflow.com/questions/37904682/how-do-i-use-docker-environment-variable-in-entrypoint-array
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论