Search code examples
pythondockerbind

how to provide file path from binded directory to program in docker


I have a python code, that works on files. It read files, do something etc. As an input to the program, you have to provide path to a file. And here is a question. After I dockerize my program, how it will find a file from host system (outside the container)? I've read that I will have to bind that host directory with the containter. I think I can handle creation of docker file. My question is simpler. How to pass file from binded directory to a docker file. If I bind "C:\user\desktop\test\" directory, and later, if I run docker file with "C:\user\desktop\test\test.file" argument, will this work? Or do I have to (somehow) indicate that selected directory is outside the container? Most of tutorials tells that you have to bind directory, but I couldn't find information about how to actualy use files from the binded directory.


Solution

  • If you want to copy a file into the Docker image because it's not going to be changed during the program's usage, use the COPY command in the Dockerfile:

    COPY path/to/file/on/host path/to/file/in/image
    

    But it looks like you want to dynamically set the file's path at runtime. In that case, don't copy the file or path in the Dockerfile. Instead, use a docker-compose.yml to run the image built with your Dockerfile:

    version: '3.7'
    services:
      app:
        build:
          context: .
        volumes:
          - type: bind
            source: ./path/to/folder/containing/file
            target: /path/the/file/will/have/in/container