Python 将文件作为参数传递给 Docker 容器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41092587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing file as argument to Docker container
提问by mpp
A very simple python program, suppose current directory is /PYTHON, I want to pass file.txt as argument to python script boot.py, here is my Dockerfile
一个非常简单的python程序,假设当前目录是/PYTHON,我想将file.txt作为参数传递给python脚本boot.py,这是我的Dockerfile
FROM python
COPY boot.py ./
COPY file.txt ./
RUN pip install numpy
CMD ["python", "boot.py", "file.txt"]
then I build the Docker container with :
然后我使用以下命令构建 Docker 容器:
docker build -t boot/latest .
docker build -t boot/latest .
then run the container
然后运行容器
docker run -t boot:latest python boot.py file.txt
docker run -t boot:latest python boot.py file.txt
I got the correct results.
我得到了正确的结果。
But If I copy another file file1.txt to the current directory (from a different directory (not /PYTHON)), then I run the container again:
但是如果我将另一个文件 file1.txt 复制到当前目录(来自不同的目录(不是 /PYTHON)),那么我再次运行容器:
docker run -t boot:latest python boot.py file1.txt
docker run -t boot:latest python boot.py file1.txt
I got the following error:
我收到以下错误:
FileNotFoundError: [Errno 2] No such file or directory: 'file1.txt'
FileNotFoundError: [Errno 2] 没有这样的文件或目录:'file1.txt'
so the error is due to fact that file1.txt is not in the container, but if I share this container with a friend and the friend wants to pass a very different file as argument, how do I write the Dockerfile so anybody with my container can pass very different files as argument without errors ? Thanks in advance. (I am new to Docker)
所以错误是由于 file1.txt 不在容器中,但是如果我与朋友共享这个容器并且朋友想要传递一个非常不同的文件作为参数,我该如何编写 Dockerfile 以便任何人使用我的容器可以将非常不同的文件作为参数传递而没有错误吗?提前致谢。(我是 Docker 的新手)
回答by HakRo
It won't work that way. Like you said, file1.txt
is not in the container.
它不会那样工作。就像你说的,file1.txt
不在容器中。
The work around is to use Docker volumes to inject files from your host machine to the container when running it.
解决方法是在运行容器时使用 Docker 卷将文件从主机注入容器。
Something like this :
像这样的事情:
docker run -v /local/path/to/file1:/container/path/to/file.txt -t boot:latest python boot.py file1.txt
Then /local/path/to/file1
would be the path on your host machine which will override /container/path/to/file.txt
on the container.
然后/local/path/to/file1
将是您的主机上的路径,它将覆盖/container/path/to/file.txt
在容器上。
回答by Nick Roz
You may also make your script read from STDIN
and then pass data to docker using cat
. Have a look at how to get docker container to read from stdin?
您还可以STDIN
使用cat
. 看看如何让 docker 容器从标准输入读取?
Something like:
就像是:
cat /path/to/file | docker run -i --rm boot python boot.py
回答by Hasan
I do not think it is always possible to assume the other party will extend the image as pointed in JustDanyul's comment. It is better to ship something ready and self-contained.
我不认为总是可以假设对方会像 JustDanyul 的评论中指出的那样扩展图像。最好运送一些准备好且自给自足的东西。
I suggest two ways:
我建议两种方式:
----- Using only docker -------
----- 仅使用 docker -------
- Remove the
ENTRYPOINT
orCMD
- Mount run as pointed in HakRo's Comment
- 删除
ENTRYPOINT
或CMD
- 按照 HakRo 的评论中的指出运行安装
Reason: if you use CMD ["python", "boot.py"]
, then you are telling docker to run boot.py
without passing arguments.
原因:如果您使用CMD ["python", "boot.py"]
,那么您是在告诉 docker 在boot.py
不传递参数的情况下运行。
----- Using docker-compose -------
----- 使用 docker-compose -------
- You can keep the
ENTRYPOINT
orCMD
in theDockerfile
- Create boot service in the
docker-compose.yml
file - Mount the file in
docker-compose.yml
volumes docker-compose -f /path/to/docker-compose.yml run boot python boot.py file1.txt
- 您可以将
ENTRYPOINT
或保留CMD
在Dockerfile
- 在
docker-compose.yml
文件中创建启动服务 - 在
docker-compose.yml
卷中挂载文件 docker-compose -f /path/to/docker-compose.yml run boot python boot.py file1.txt
Both from my personal experience.
两者都来自我的个人经验。
回答by Tee Hammed
One option is to make use of volumes.
一种选择是利用卷。
This way all collaborators on the project are able to mount them in the containers.
这样,项目的所有合作者都可以将它们安装在容器中。
回答by JustDanyul
If I understand the question correctly, you are acknowledging that the file isn't in the container, and you are asking how to best share you container with the world, allowing people to add their own content into it.
如果我正确理解了这个问题,那么您承认该文件不在容器中,并且您正在询问如何最好地与世界共享您的容器,允许人们将他们自己的内容添加到其中。
You have a couple of options, either use docker volumes, which allows your friends (and other interested parties) to mount local volumes inside your docker containers. That is, you can overlay a folder on your local filesystem onto a folder inside the container(This is generally quite nifty when you are developing locally as well)
你有几个选择,要么使用docker volumes,它允许你的朋友(和其他感兴趣的人)在你的 docker 容器中安装本地卷。也就是说,您可以将本地文件系统上的文件夹覆盖到容器内的文件夹上(当您在本地开发时,这通常也很漂亮)
Or, again, depending on the purpose of your container, somebody could extend your image. For example, a Dockerfile like
或者,同样,根据容器的用途,有人可以扩展您的图像。例如,一个 Dockerfile 像
FROM yourdockerimage:latest
COPY file1.txt ./
CMD ["python", "boot.py", "file1.txt"]
Choose which ever option suits your project the best.
选择最适合您的项目的选项。
回答by gdzcorp
You could change your Dockerfile to:
您可以将 Dockerfile 更改为:
FROM python
COPY boot.py ./
COPY file.txt ./
RUN pip install numpy
ENTRYPOINT ["python", "boot.py"]
and then run it to read from STDIN
:
然后运行它以读取STDIN
:
docker run -i boot:latest -<file1.txt