Docker Compose 相对路径与 Docker 卷 [英] Docker Compose Relative paths vs Docker volume
问题描述
我有一个用于网站的 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 中的数据持久化
安装任何卷有四种可能的选项:
- 相对路径
- 绝对路径
- Docker 卷默认路径
- 具有绝对路径的 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:
- Relative Path
- Absolute Path
- Docker Volume Default Path
- 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屋!