简体   繁体   English

docker-compose 类型:卷保存在外部文件夹中

[英]docker-compose type: volume persist in external folder

version: '3.2'
    
services:
  elasticsearch:
    container_name: elasticsearch
    build:
      context: elasticsearch/
      args:
        ELK_VERSION: $ELK_VERSION
    volumes:
      - type: bind
        source: ./elasticsearch/config/elasticsearch.yml
        target: /usr/share/elasticsearch/config/elasticsearch.yml
        read_only: true
      - type: volume
        source: elasticsearch
        target: /usr/share/elasticsearch/data
    ports:
      - "9200:9200"
      - "9300:9300"
    environment:
      LOGSPOUT: ignore
      ES_JAVA_OPTS: "-Xmx256m -Xms256m"
      ELASTIC_PASSWORD: changeme
      # Use single node discovery in order to disable production mode and avoid bootstrap checks.
      # see: https://www.elastic.co/guide/en/elasticsearch/reference/current/bootstrap-checks.html
      discovery.type: single-node
    networks:
      - elk

networks:
  elk:
    driver: bridge

volumes:
  elasticsearch:

I want to use elasticsearch to pesrist data in folder /WDC1TB/docker/volumes/elasticsearch/data but I can not set it correctly.我想使用 elasticsearch 来保留文件夹/WDC1TB/docker/volumes/elasticsearch/data ,但我无法正确设置它。 Or I am getting that volume is not a string or similar error.或者我得到该卷不是字符串或类似错误。

How to correctly use docker-compose and persist data in external folder /WDC1TB/docker/volumes/elasticsearch/data如何正确使用docker-compose并将数据持久化到外部文件夹/WDC1TB/docker/volumes/elasticsearch/data

When you use a volume with type: volume it will be saved where Docker service says to, in my case is in /var/lib/docker/volumes/{projectname_containername}/_data/ .当您使用type: volume为卷的卷时,它将保存在 Docker 服务所说的位置,在我的情况下是/var/lib/docker/volumes/{projectname_containername}/_data/

If you want to save it in a specific folder you will need a type: bind volume that points to the desired folder in your host, in your case /WDC1TB/docker/volumes/elasticsearch/data .如果要将其保存在特定文件夹中,则需要一个type: bind卷,它指向主机中所需的文件夹,在您的情况下是/WDC1TB/docker/volumes/elasticsearch/data

You should replace:你应该更换:

  - type: volume
    source: elasticsearch
    target: /usr/share/elasticsearch/data

with

  - type: bind
    source: /WDC1TB/docker/volumes/elasticsearch/data
    target: /usr/share/elasticsearch/data

If /WDC1TB/docker/volumes/elasticsearch/data doesn't exist, Docker will create it.如果/WDC1TB/docker/volumes/elasticsearch/data不存在,Docker 将创建它。 In this case check file permission afterwards.在这种情况下,请检查文件权限。

Also, now that you won't use named volumes you can delete此外,既然您不会使用命名卷,您可以删除

volumes:
  elasticsearch:

With your current configuration:使用您当前的配置:

services:
  elasticsearch:
    container_name: elasticsearch
    build:
      context: elasticsearch/
      args:
        ELK_VERSION: $ELK_VERSION
    volumes:
      - type: bind
        source: ./elasticsearch/config/elasticsearch.yml
        target: /usr/share/elasticsearch/config/elasticsearch.yml
        read_only: true
      - type: volume
        source: elasticsearch
        target: /usr/share/elasticsearch/data
    ports:
      - "9200:9200"
      - "9300:9300"
    environment:
      LOGSPOUT: ignore
      ES_JAVA_OPTS: "-Xmx256m -Xms256m"
      ELASTIC_PASSWORD: changeme
      # Use single node discovery in order to disable production mode and avoid bootstrap checks.
      # see: https://www.elastic.co/guide/en/elasticsearch/reference/current/bootstrap-checks.html
      discovery.type: single-node
    networks:
      - elk


networks:
  elk:
    driver: bridge 
volumes:
    elasticsearch:

You are creating a Volume on top of the container directory /usr/share/elasticsearch/data specified in:您正在容器目录 /usr/share/elasticsearch/data 之上创建一个,其中指定:

 - type: volume
        source: elasticsearch
        target: /usr/share/elasticsearch/data

At the same time you are indicating to docker to create this volume with:同时,您指示 docker 创建此卷:

volumes:
  elasticsearch:

This means docker engine is creating a volume named elasticsearch and is mounting the container specified directory there.这意味着 docker 引擎正在创建一个名为 elasticsearch 的卷,并将容器指定的目录挂载在那里。

You can check the volume mountpoint with:您可以使用以下命令检查卷挂载点:

$ docker volume inspect elasticsearch

In order to mount the data to host filesystem directory, you should use bind mounts with the next compose file:为了将数据挂载到主机文件系统目录,您应该使用绑定挂载和下一个撰写文件:

services:
  elasticsearch:
    container_name: elasticsearch
    build:
      context: elasticsearch/
      args:
        ELK_VERSION: $ELK_VERSION
    volumes:
      - type: bind
        source: ./elasticsearch/config/elasticsearch.yml
        target: /usr/share/elasticsearch/config/elasticsearch.yml
        read_only: true
      - type: bind
        source: /WDC1TB/docker/volumes/elasticsearch/data
        target: /usr/share/elasticsearch/data
    ports:
      - "9200:9200"
      - "9300:9300"
    environment:
      LOGSPOUT: ignore
      ES_JAVA_OPTS: "-Xmx256m -Xms256m"
      ELASTIC_PASSWORD: changeme
      # Use single node discovery in order to disable production mode and avoid bootstrap checks.
      # see: https://www.elastic.co/guide/en/elasticsearch/reference/current/bootstrap-checks.html
      discovery.type: single-node
    networks:
      - elk


networks:
  elk:
    driver: bridge 

With that, using a bind mount, a file or directory /WDC1TB/docker/volumes/elasticsearch/data on the host machine is mounted into a container.这样,使用绑定挂载,将主机上的文件或目录 /WDC1TB/docker/volumes/elasticsearch/data 挂载到容器中。

In the opposite case, the way your current solution is done, you are using a volume that are completely managed by Docker and is the engine itself responsible to assing a mount point for the volume.在相反的情况下,您当前解决方案的完成方式是,您使用的卷完全由 Docker 管理,并且引擎本身负责为该卷分配安装点。

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

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