Passing file as argument to Docker container

PythonDocker

Python Problem Overview


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)

Python Solutions


Solution 1 - Python

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 /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.

Solution 2 - Python

You may also make your script read from STDIN and then pass data to docker using cat. Have a look at https://stackoverflow.com/questions/44421635/how-to-get-docker-container-to-read-from-stdin

The trick is to keep STDIN open even if not attached with --interactive or -i (alias) option for Docker.

Something like:

cat /path/to/file | docker run -i --rm boot python boot.py

Or:

docker run -i --rm boot python booty.py < /path/to/file

EOF is the end of the input.

Solution 3 - Python

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)

Or, again, depending on the purpose of your container, somebody could extend your image. For example, a Dockerfile like

FROM yourdockerimage:latest
COPY file1.txt ./
CMD ["python", "boot.py", "file1.txt"]

Choose which ever option suits your project the best.

Solution 4 - Python

One option is to make use of volumes.

This way all collaborators on the project are able to mount them in the containers.

Solution 5 - Python

You could change your Dockerfile to:

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

and then run it to read from STDIN:

docker run -i boot:latest -<file1.txt

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmppView Question on Stackoverflow
Solution 1 - PythonHakRoView Answer on Stackoverflow
Solution 2 - PythonNick RozView Answer on Stackoverflow
Solution 3 - PythonJustDanyulView Answer on Stackoverflow
Solution 4 - PythonHammedView Answer on Stackoverflow
Solution 5 - PythongdzcorpView Answer on Stackoverflow