How to set a path on host for a named volume in docker-compose.yml

后端 未结 3 1665
执笔经年
执笔经年 2020-12-02 08:08

Example below creates dbdata named volume and references it inside db service:

version: \'2\'
services:
  db:
    image: mysql
    volumes:         


        
相关标签:
3条回答
  • 2020-12-02 08:20

    The location of named volumes is managed by docker; if you want to specify the location yourself, you can either "bind mount" a host directory, or use a volume plugin that allows you to specify a path.

    You can find some details in another answer I posted recently; https://stackoverflow.com/a/36321403/1811501

    0 讨论(0)
  • 2020-12-02 08:23

    With the local volume driver comes the ability to use arbitrary mounts; by using a bind mount you can achieve exactly this.

    For setting up a named volume that gets mounted into /srv/db-data, your docker-compose.yml would look like this:

    version: '2'
    services:
      db:
        image: mysql
        volumes:
          - dbdata:/var/lib/mysql
    volumes:
      dbdata:
        driver: local
        driver_opts:
          type: 'none'
          o: 'bind'
          device: '/srv/db-data'
    

    I have not tested it with the version 2 of the compose file format, but https://docs.docker.com/compose/compose-file/compose-versioning/#version-2 does not indicate, that it should not work.

    I've also not tested it on Windows...

    0 讨论(0)
  • 2020-12-02 08:38

    As of docker-compose v3.2 you can now do it as follows:

    Note: Many, as I figured if you put version: "3" it would use the newest/latest V3, it doesn't it uses v3.0, you MUST specify at a minimum 3.2 to use the below configuration method.

    https://docs.docker.com/compose/compose-file/#volumes

    version: "3.2"
    services:
      db:
        image: postgres:latest
        volumes: 
          - type: volume
            source: /opt/db/vols/dbdata
            target: /var/lib/postgresql/data
            volume:
              nocopy: true
    
    
    
    volumes:
       dbdata:
    

    Note:

    Named volumes must be listed under the top-level volumes key, as shown.

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