Docker Compose Relative paths vs Docker volume

﹥>﹥吖頭↗ 提交于 2020-03-22 09:21:53

问题


I have a docker compose file for a website, which amongst a bunch of other containers for various purposes, includes a mysql database that will have persistent data. At the moment the compose file specifies a relative path for the data, e.g.:

 
mysql: 
  image: mysql:5.7
  container_name: sqldb
  volumes:
   - ./mysql_data/_data:/var/lib/mysql

and the folder structure:

 --mysql_data
 --static_content
 docker-compose.yml

which means that at any point I can move the whole site (including persisted content) to another server by copying the whole folder and running docker-compose up.

But reading about docker volumes it sounds like it is the preferred method (plus relative bind mount paths don't seem to be supported using "docker run", but work in compose) so I'm wondering if I need to change this approach to use volumes? Is there something inherently wrong with this relative binding approach? If I do switch to volumes, when moving the containers do I have to manually move the volumes (e.g. this method How to port data-only volumes from one host to another?)?


回答1:


Here is the sample for docker-swarm or compose to maintain the persistancy of the data.

version: '3'
services:
    sample:
        image: sample
        volumes:
            - sample-date:/var/data
volumes:
  sample-date:
    driver: local
    driver_opts:
      o: bind
      type: none
      device: /home/path/of/your/folder

This works for any server as we folder and cutomise the volume device property to respective directory path.



来源:https://stackoverflow.com/questions/46907558/docker-compose-relative-paths-vs-docker-volume

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!