Docker:更改用于存储 Docker 卷的文件夹 [英] Docker: change folder where to store docker volumes

查看:30
本文介绍了Docker:更改用于存储 Docker 卷的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Ubuntu EC2 上,我使用 docker 容器托管一个应用程序.db 数据和 upload 数据存储在卷 CaseBook-data-dbCaseBook-data-uploads 中正在使用以下命令创建:

On my Ubuntu EC2 I host an application using docker containers. db data and upload data is being stored in volumes CaseBook-data-db and CaseBook-data-uploads which are being created with this commands:

docker volume create --name=CaseBook-data-db
docker volume create --name=CaseBook-data-uploads

通过 docker-compose 文件附加卷:

version: '2'
services:
    mongo:
        container_name: "CaseBook-db"
        restart: always
        image: mongo:3.2.7
        ports:
            - "27017"
        volumes:
            - data_db:/data/db
        labels:
            - "ENVIRONMENT_TYPE=meteor"

    app:
        container_name: "CaseBook-app"
        restart: always
        image: "meteor/casebook"
        build: .
        depends_on:
            - mongo
        environment:
            - MONGO_URL=mongodb://mongo:27017/CaseBook
        ports:
            - "80:3000"
        volumes:
            - data_uploads:/Meteor-CaseBook-Container/.uploads
        labels:
            - "ENVIRONMENT_TYPE=meteor"
volumes:
     data_db:
        external:
            name: CaseBook-data-db
     data_uploads:
        external:
            name: CaseBook-data-uploads

我需要将这些 docker 卷存储在主机的不同文件夹(例如 /home/ubuntu/data/)中.如何更改卷的 docker 存储文件夹?或者有更好的方法来做到这一点?提前致谢.

I need to store those docker volumes in different folder(for example /home/ubuntu/data/) of the host machine. How to change docker storage folder for volumes? Or there is a better way in doing this? Thank you in advance.

推荐答案

命名卷将存储在 docker 的文件夹 (/var/lib/docker) 中.如果要在特定主机文件夹中创建卷,请使用具有以下语法的主机卷:

Named volumes will be stored inside docker's folder (/var/lib/docker). If you want to create a volume in a specific host folder, use a host volume with the following syntax:

docker run -v /home/ubuntu/data/app-data:/app-data my-image

或来自您的撰写文件:

version: '2'
services:
    mongo:
        container_name: "CaseBook-db"
        restart: always
        image: mongo:3.2.7
        ports:
            - "27017"
        volumes:
            - /home/ubuntu/data/db:/data/db
        labels:
            - "ENVIRONMENT_TYPE=meteor"

    app:
        container_name: "CaseBook-app"
        restart: always
        image: "meteor/casebook"
        build: .
        depends_on:
            - mongo
        environment:
            - MONGO_URL=mongodb://mongo:27017/CaseBook
        ports:
            - "80:3000"
        volumes:
            - /home/ubuntu/data/uploads:/Meteor-CaseBook-Container/.uploads
        labels:
            - "ENVIRONMENT_TYPE=meteor"

对于主机卷,图像内卷的任何内容都将与主机文件夹的确切内容重叠,包括主机文件夹的 UID.空主机文件夹不会像空命名卷那样从映像初始化.UID 映射往往是使用主机卷最困难的部分.

With host volumes, any contents of the volume inside the image will be overlaid with the exact contents of the host folder, including UID's of the host folder. An empty host folder is not initialized from the image the way an empty named volume is. UID mappings tend to be the most difficult part of using a host volume.

从下面的评论中,如果您需要一个作为主机卷的命名卷,有一个 本地持久卷插件,列在docker的插件列表中.安装插件后,您可以创建指向主机文件夹的卷,具有即使删除命名卷后,主机目录仍会保留的功能.插件的示例用法包括:

from the comments below, if you need a named volume that acts as a host volume, there is a local persist volume plugin that's listed on docker's plugin list. After installing the plugin, you can create volumes that point to host folders, with the feature that even after removing the named volume, the host directory is left behind. Sample usage from the plugin includes:

docker volume create -d local-persist -o mountpoint=/data/images --name=images
docker run -d -v images:/path/to/images/on/one/ one
docker run -d -v images:/path/to/images/on/two/ two

它们还包括一个 v2 撰写文件,其中包含以下卷示例:

They also include a v2 compose file with the following volume example:

volumes:
  data:
    driver: local-persist
    driver_opts:
      mountpoint: /data/local-persist/data

<小时>

我在上个月了解到的另一个选项是使用本地卷驱动程序的挂载选项手动创建绑定挂载.这类似于 docker 中的主机卷,但有以下区别:


One additional option that I've been made aware of in the past month is to use the local volume driver's mount options to manually create a bind mount. This is similar to a host volume in docker with the following differences:

  • 如果该目录不存在,则尝试启动具有指向绑定安装的命名卷的容器将失败.对于主机卷,docker 会将其初始化为 root 拥有的空目录.
  • 如果目录为空,命名卷将使用挂载位置的映像内容初始化绑定挂载,包括文件和目录所有权/权限.对于主机卷,没有对主机目录内容的初始化.

要创建一个命名卷作为绑定挂载,您可以预先创建它:

To create a named volume as a bind mount, you can create it in advance with:

docker volume create --driver local 
  --opt type=none 
  --opt device=/home/user/test 
  --opt o=bind 
  test_vol

docker run 命令,这可以通过 --mount 来完成:

From a docker run command, this can be done with --mount:

docker run -it --rm 
    --mount type=volume,dst=/container/path,volume-driver=local,volume-opt=type=none,volume-opt=o=bind,volume-opt=device=/home/user/test 
    foo

或者在撰写文件中,您可以使用以下命令创建命名卷:

Or in a compose file, you can create the named volume with:

volumes:
  data:
    driver: local
    driver_opts:
      type: none
      o: bind 
      device: /home/user/test 

如果您需要命名卷功能,我的首选是将命名卷与本地驱动程序一起使用,而不是使用 local-persist 3rd 方驱动程序.

My preference would be to use the named volume with the local driver instead of the local-persist 3rd party driver if you need the named volume features.

这篇关于Docker:更改用于存储 Docker 卷的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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