How to mount a single file in a volume

后端 未结 14 845
隐瞒了意图╮
隐瞒了意图╮ 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:52

    As of docker-compose file version 3.2, you can specify a volume mount of type "bind" (instead of the default type "volume") that allows you to mount a single file into the container. Search for "bind mount" in the docker-compose volume docs: https://docs.docker.com/compose/compose-file/#volumes

    In my case, I was trying to mount a single ".secrets" file into my application that contained secrets for local development and testing only. In production, my application fetches these secrets from AWS instead.

    If I mounted this file as a volume using the shorthand syntax:

    volumes:
     - ./.secrets:/data/app/.secrets
    

    Docker would create a ".secrets" directory inside the container instead of mapping to the file outside of the container. My code would then raise an error like "IsADirectoryError: [Errno 21] Is a directory: '.secrets'".

    I fixed this by using the long-hand syntax instead, specifying my secrets file using a read-only "bind" volume mount:

    volumes:
     - type: bind
       source: ./.secrets
       target: /data/app/.secrets
       read_only: true
    

    Now Docker correctly mounts my .secrets file into the container, creating a file inside the container instead of a directory.

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

    You can mount files or directories/folders it all depends on Source file or directory. And also you need to provide full path or if you are not sure you can use PWD. Here is a simple working example.

    In this example, I am mounting env-commands file which already exists in my working directory

    $ docker run  --rm -it -v ${PWD}/env-commands:/env-commands aravindgv/eosdt:1.0.5 /bin/bash -c "cat /env-commands"
    
    0 讨论(0)
提交回复
热议问题