Passing file as argument to Docker container

后端 未结 5 756
名媛妹妹
名媛妹妹 2020-12-29 18:23

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

FRO         


        
相关标签:
5条回答
  • 2020-12-29 18:53

    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.

    0 讨论(0)
  • 2020-12-29 18:56

    One option is to make use of volumes.

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

    0 讨论(0)
  • 2020-12-29 18:59

    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.

    0 讨论(0)
  • 2020-12-29 19:05

    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
    
    0 讨论(0)
  • 2020-12-29 19:10

    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?

    Something like:

    cat /path/to/file | docker run -i --rm boot python boot.py
    
    0 讨论(0)
提交回复
热议问题