简体   繁体   English

Docker:更改文件夹存储docker卷的位置

[英]Docker: change folder where to store docker volumes

On my Ubuntu EC2 I host an application using docker containers. 在我的Ubuntu EC2我使用docker容器托管应用程序。 db data and upload data is being stored in volumes CaseBook-data-db and CaseBook-data-uploads which are being created with this commands: db数据和upload数据存储在使用以下命令创建的卷CaseBook-data-dbCaseBook-data-uploads

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

Volumes being attached through docker-compose file: 通过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

I need to store those docker volumes in different folder(for example /home/ubuntu/data/ ) of the host machine. 我需要将这些docker卷存储在主机的不同文件夹(例如/home/ubuntu/data/ )中。 How to change docker storage folder for volumes? 如何更改卷的docker存储文件夹? Or there is a better way in doing this? 或者有更好的方法来做到这一点? Thank you in advance. 先感谢您。

Named volumes will be stored inside docker's folder (/var/lib/docker). 命名卷将存储在docker的文件夹(/ 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

Or from your compose file: 或者从你的撰写文件中:

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"

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. 对于主机卷,映像中卷的任何内容都将与主机文件夹的确切内容重叠,包括主机文件夹的UID。 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. UID映射往往是使用主机卷最困难的部分。


Edit: 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 . 编辑:从下面的评论中,如果您需要一个充当主机卷的命名卷,则会在docker的插件列表中列出一个本地持久卷插件 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

They also include a v2 compose file with the following volume example: 它们还包含一个v2 compose文件,其中包含以下卷示例:

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

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中的主机卷类似,但有以下区别:

  • If the directory doesn't exist, trying to start a container with a named volume pointing to a bind mount will fail. 如果该目录不存在,则尝试启动具有指向绑定装入的命名卷的容器将失败。 With host volumes, docker will initialize it to an empty directory owned by root. 对于主机卷,docker会将其初始化为root拥有的空目录。
  • If the directory is empty, a named volume will initialize the bind mount with the contents of the image at the mount location, including file and directory ownership/permissions. 如果目录为空,则命名卷将使用安装位置处的映像内容初始化绑定装入,包括文件和目录所有权/权限。 With a host volume, there is no initialization of the host directory contents. 使用主机卷时,不会初始化主机目录内容。

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

From a docker run command, this can be done with --mount : --mount docker run命令,可以使用--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 

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. 如果您需要命名的卷功能,我的首选是使用本地驱动程序而不是local-persist第三方驱动程序使用命名卷。

Another way with build-in driver local : 内置驱动程序本地的另一种方法:

docker volume create --opt type=none --opt device=/home/ubuntu/data/ --opt o=bind data_db

(This use DimonVersace example with: data_db declared as external named volume in docker-compose and /home/ubuntu/data/ as the folder on the host machine) (这使用DimonVersace示例: data_db在docker -compose中声明为外部命名卷,而/ home / ubuntu / data /在主机上声明为文件夹)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM