How to mount a single file in a volume

后端 未结 14 844
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 10:16

I am trying to dockerize a PHP application. In the dockerfile, I download the archive, extract it, etc.

Everything works fine. However, if a new version gets released

相关标签:
14条回答
  • 2020-11-27 10:27

    I had been suffering from a similar issue. I was trying to import my config file to my container so that I can fix it every time I need without re-building the image.

    I mean I thought the below command would map $(pwd)/config.py from Docker host to /root/app/config.py into the container as a file.

    docker run -v $(pwd)/config.py:/root/app/config.py my_docker_image
    

    However, it always created a directory named config.py, not a file.

    while looking for clue, I found the reason(from here)

    If you use -v or --volume to bind-mount a file or directory that does not yet exist on the Docker host, -v will create the endpoint for you. It is always created as a directory.

    Therefore, it is always created as a directory because my docker host does not have $(pwd)/config.py.

    Even if I create config.py in docker host. $(pwd)/config.py just overwirte /root/app/config.py not exporting /root/app/config.py.

    0 讨论(0)
  • 2020-11-27 10:28

    Use mount (--mount) instead volume (-v)

    More info: https://docs.docker.com/storage/bind-mounts/

    Example:

    Ensure /tmp/a.txt exists on docker host

    docker run -it --mount type=bind,source=/tmp/a.txt,target=/root/a.txt alpine sh
    
    0 讨论(0)
  • 2020-11-27 10:32

    For me, the issue was that I had a broken symbolic link on the file I was trying to mount into the container

    0 讨论(0)
  • 2020-11-27 10:35

    You can also use a relative path in your docker-compose.yml file like this (tested on Windows host, Linux container):

    volumes:
        - ./test.conf:/fluentd/etc/test.conf
    
    0 讨论(0)
  • 2020-11-27 10:36

    TL;DR/Notice:

    If you experience a directory being created in place of the file you are trying to mount, you have probably failed to supply a valid and absolute path. This is a common mistake with a silent and confusing failure mode.

    File volumes are done this way in docker (absolute path example (can use env variables), and you need to mention the file name) :

        volumes:
          - /src/docker/myapp/upload:/var/www/html/upload
          - /src/docker/myapp/upload/config.php:/var/www/html/config.php
    

    You can also do:

        volumes:
          - ${PWD}/upload:/var/www/html/upload
          - ${PWD}/upload/config.php:/var/www/html/config.php
    

    If you fire the docker-compose from /src/docker/myapp folder

    0 讨论(0)
  • 2020-11-27 10:37

    Maybe this helps someone.

    I had this problem and tried everything. Volume bindings looked well and even if I mounted directory (not files), I had the file names in the mounted directory correctly but mounted as dirs.

    I tried to re-enable shared drives and Docker complained the firewall is active.

    After disabling the firewall all was working fine.

    0 讨论(0)
提交回复
热议问题