Docker Compose相对路径与Docker卷 [英] Docker Compose Relative paths vs Docker volume

查看:1119
本文介绍了Docker Compose相对路径与Docker卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站的docker compose文件,其中有许多用于各种目的的其他容器,其中包括一个将包含持久数据的mysql数据库。目前,撰写文件指定了数据的相对路径,例如:

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

和文件夹结构:


 --mysql_data
 --static_content
 docker-compose.yml

这意味着在任何时候我都可以通过复制整个文件夹并运行docker-compose将整个站点(包括持久性内容)移动到另一台服务器。

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.

但是读到docker卷听起来像是首选方法(加上相对绑定安装路径似乎不支持使用 docker run,但是可以在撰写中),所以我想知道是否需要更改此方法以使用卷?这种相对绑定方法存在天生的错误吗?如果我确实切换到卷,则在移动容器时是否必须手动移动卷(例如,此方法如何将仅数据卷从一台主机移植到另一台?

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?)?

推荐答案

Docker中数据的持久性


有四个可能的选项来装载任何卷

Persistence of data in Docker

There are four possible options to mount any volume


  1. 相对路径

  2. 绝对路径

  3. Docker卷默认路径

  4. 具有绝对路径的Docker VOlume

  1. Relative Path
  2. Absolute Path
  3. Docker Volume Default Path
  4. Docker VOlume with Absolute Path

以下是上面的示例

version: '3'
services:
    sample:
        image: sample
        volumes:
            - ./relative-path-volume:/var/data-two
            - /home/ubuntu/absolute-path-volume:/var/data-one
            - docker-volume-default-path-volume:/var/data-three
            - docker-volume-absolute-path-volume:/var/data-four
volumes:
  docker-volume-default-path-volume: {}
  docker-volume-absolute-path-volume:
    driver: local
    driver_opts:
      o: bind
      type: none
      device: /home/path/of/your/folder

相对路径 ./ relative-path-volume:/ var / data-two

绝对路径 / home / ubuntu / absolute-path-volume:/ var / data-one

Docker Volume默认路径 docker-volume-default-path-volume:/ var / data-three

具有绝对路径的Docker VOlume docker-volume-absolute-path-volume:/ var / data-four

此功能适用于我们在文件夹中使用的任何服务器,并将卷设备属性自定义为各自的目录路径。

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

这篇关于Docker Compose相对路径与Docker卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆