【问题标题】:Using a Docker-Volume with a Mount to a Symlink, But It's Persisting Data to the Host Too. Why?使用带有挂载到符号链接的 Docker-Volume,但它也将数据持久化到主机。为什么?
【发布时间】:2020-06-30 00:16:03
【问题描述】:

我这样创建了一个 Docker 卷:

sudo docker volume create --driver=local --name=es-data1 --opt type=none --opt o=bind --opt device=/usr/local/contoso/data1/elasticsearch/data1
  • /usr/local/contoso/data1/elasticsearch/data1 是一个符号链接。

我正在我的docker-compose.yml 文件中实例化三个 Elasticsearch Docker 容器:

version: '3.7'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.6.0
    logging:
      driver: none
    container_name: elasticsearch1
    environment:
      - node.name=elasticsearch1
      - cluster.name=docker-cluster
      - cluster.initial_master_nodes=elasticsearch1
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms1G -Xmx1G"
      - http.cors.enabled=true
      - http.cors.allow-origin=*
      - network.host=_eth0_          
    ulimits:
      nproc: 65535
      memlock:
        soft: -1
        hard: -1
    cap_add:
      - ALL
    # privileged: true
    deploy:
      replicas: 1
      update_config:
        parallelism: 1
        delay: 10s
      resources:
        limits:
          cpus: '1'
          memory: 1G
        reservations:
          cpus: '1'
          memory: 1G
      restart_policy:
        condition: unless-stopped
        delay: 5s
        max_attempts: 3
        window: 10s
    volumes:
      - es-logs:/var/log
      - es-data1:/usr/share/elasticsearch/data
    networks:
      - elastic
      - ingress
    ports:
      - 9200:9200
      - 9300:9300
    healthcheck:
      test: wget -q -O - http://127.0.0.1:9200/_cat/health
  elasticsearch2:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.6.0
    logging:
      driver: none
    container_name: elasticsearch2
    environment:
      - node.name=elasticsearch2
      - cluster.name=docker-cluster
      - cluster.initial_master_nodes=elasticsearch1
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms1G -Xmx1G"
      - "discovery.zen.ping.unicast.hosts=elasticsearch1"
      - http.cors.enabled=true
      - http.cors.allow-origin=*
      - network.host=_eth0_          
    ulimits:
      nproc: 65535
      memlock:
        soft: -1
        hard: -1
    cap_add:
      - ALL
    # privileged: true
    deploy:
      replicas: 1
      update_config:
        parallelism: 1
        delay: 10s
      resources:
        limits:
          cpus: '1'
          memory: 1G
        reservations:
          cpus: '1'
          memory: 1G
      restart_policy:
        condition: unless-stopped
        delay: 5s
        max_attempts: 3
        window: 10s
    volumes:
      - es-logs:/var/log
      - es-data2:/usr/share/elasticsearch/data
    networks:
      - elastic
      - ingress
    ports:
      - 9201:9200
    healthcheck:
      test: wget -q -O - http://127.0.0.1:9200/_cat/health
  elasticsearch3:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.6.0
    logging:
      driver: none
    container_name: elasticsearch3
    environment:
      - node.name=elasticsearch3
      - cluster.name=docker-cluster
      - cluster.initial_master_nodes=elasticsearch1
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms1G -Xmx1G"
      - "discovery.zen.ping.unicast.hosts=elasticsearch1"
      - http.cors.enabled=true
      - http.cors.allow-origin=*
      - network.host=_eth0_          
    ulimits:
      nproc: 65535
      memlock:
        soft: -1
        hard: -1
    cap_add:
      - ALL
    # privileged: true
    deploy:
      replicas: 1
      update_config:
        parallelism: 1
        delay: 10s
      resources:
        limits:
          cpus: '1'
          memory: 1G
        reservations:
          cpus: '1'
          memory: 1G
      restart_policy:
        condition: unless-stopped
        delay: 5s
        max_attempts: 3
        window: 10s
    volumes:
      - es-logs:/var/log
      - es-data3:/usr/share/elasticsearch/data
    networks:
      - elastic
      - ingress
    ports:
      - 9202:9200
    healthcheck:
      test: wget -q -O - http://127.0.0.1:9200/_cat/health
volumes:
  es-data1:
    driver: local
    external: true
  es-data2:
    driver: local
    external: true
  es-data3:
    driver: local
    external: true
  es-logs:
    driver: local
    external: true     
networks:
  elastic:
    external: true
  ingress:
    external: true

我的问题:

  • Elasticsearch 容器将索引数据保存到主机文件系统和挂载的符号链接。

我的问题:

  • 如何修改我的配置以使 Elasticsearch 容器将索引数据持久保存到已挂载的符号链接?

【问题讨论】:

  • 我更新/改进了我的答案,因为它对 docker-compose.yml 的更改有点模糊。请让我知道,它是否适合你。谢谢。

标签: docker-compose symlink mount docker-volume elasticsearch-7


【解决方案1】:

文件额外存储在主机上似乎是local 卷驱动程序的默认行为。您可以更改 docker-compose.yml 中的音​​量设置,以防止 docker 在主机文件系统上持久(复制)文件(请参阅 nocopy: true),如下所示:

version: '3.7'
services:
  elasticsearch:
    ....
    volumes:
      - type: volume
        source: es-data1
        target: /usr/share/elasticsearch/data
        volume:
          nocopy: true
    ....
volumes:
  es-data1:
    driver: local
    external: true

您可能还想在此处检查此问题:Docker-compose - volumes driver local meaning。因此,似乎有一些 docker volume 插件是专门出于可移植性原因而制作的;例如flockerhedvig。但是我没有为此目的使用插件,所以我还不能真正推荐一个。

【讨论】:

    猜你喜欢
    • 2016-12-20
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多