How to mount a single file in a volume

后端 未结 14 1051
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  萌比男神i
    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.

提交回复
热议问题