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

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

问题描述

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

<前>mysql:图像:mysql:5.7容器名称:sqldb卷:- ./mysql_data/_data:/var/lib/mysql

和文件夹结构:

<前>--mysql_data--static_contentdocker-compose.yml

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

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

解决方案

Docker 中的数据持久化

安装任何卷有四种可能的选项:

  1. 相对路径
  2. 绝对路径
  3. Docker 卷默认路径
  4. 具有绝对路径的 Docker 卷

这是上面的例子:

版本:'3'服务:样本:图像:样品卷:- ./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卷:docker-volume-default-path-volume:{}docker-volume-absolute-path-volume:司机:本地驱动程序选择:o:绑定类型:无设备:/home/path/of/your/folder

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

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

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

Docker Volume with Absolute Path:docker-volume-absolute-path-volume:/var/data-four

这适用于任何服务器,因为我们将卷设备属性自定义为相应的目录路径.

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

解决方案

Persistence of data in Docker

There are four possible options to mount any volume:

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

Here is the example for above:

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: ./relative-path-volume:/var/data-two

Absolute Path: /home/ubuntu/absolute-path-volume:/var/data-one

Docker Volume Default Path: docker-volume-default-path-volume:/var/data-three

Docker Volume with Absolute Path: docker-volume-absolute-path-volume:/var/data-four

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

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

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