Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

python - Passing file as argument to Docker container

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"]

then I build the Docker container with :

docker build -t boot/latest .

then run the container

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:

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'

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

Something like this :

docker run -v /local/path/to/file1.txt:/container/path/to/file1.txt -t boot:latest python boot.py 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...