将文件作为参数传递给Docker容器 [英] Passing file as argument to Docker container

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

问题描述

一个非常简单的python程序,假设当前目录是/ PYTHON,我想将file.txt作为参数传递给python脚本boot.py,这是我的Dockerfile

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

FROM python
COPY boot.py ./
COPY file.txt ./
RUN pip install numpy
CMD ["python", "boot.py", "file.txt"]

然后我用:

docker build -t boot / latest。

然后运行容器

docker run -t boot:latest python boot.py file.txt

我得到了正确的结果。

但是如果我将另一个文件file1.txt复制到当前目录(从另一个目录) (不是/ PYTHON),然后我再次运行容器:

But If I copy another file file1.txt to the current directory (from a different directory (not /PYTHON)), then I run the container again:

docker run -t boot:latest python boot.py file1.txt

我遇到以下错误:


FileNotFoundError :[Errno 2]没有这样的文件或目录:'file1 .txt'

FileNotFoundError: [Errno 2] No such file or directory: 'file1.txt'

所以错误是由于file1.txt不在容器中,但如果我与一个朋友,而这个朋友想通过一个非常不同的文件作为参数,那么我该如何编写Dockerfile以便所有人都可以将没有太大关系的文件作为参数传递给我的容器?提前致谢。 (我是Docker的新手)

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 不在容器中。

It won't work that way. Like you said, file1.txt is not in the container.

解决方法是使用Docker卷从您的文件中注入文件

The work around is to use Docker volumes to inject files from your host machine to the container when running it.

类似这样的东西:

docker run -v /local/path/to/file1.txt:/container/path/to/file1.txt -t boot:latest python boot.py file1.txt

然后 /local/path/to/file1.txt 将是主机上的路径,它将覆盖 / container /容器上的path / to / file1.txt

Then /local/path/to/file1.txt would be the path on your host machine which will override /container/path/to/file1.txt on the container.

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

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