Docker bind elasticsearch volume in app folder

19,613

Solution 1

If you use ./ the volume will be mounted in the same folder (I have had permissions issues before by doing this just so you know)

version: "3.3"
services:    
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.1.1
    volumes:
      - ./esdata:/usr/share/elasticsearch/data
    environment:
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - discovery.type=single-node
    ports:
      - "9300:9300"
      - "9200:9200"

volumes:
  esdata:

Solution 2

This answer is applicable for ElasticSearch 7.14. As, other users have mentioned, you can get the volume to be created under the same directory as docker-compose.yml using relative path notation. i.e, an entry like this:

volumes:
      - ./esdata:/usr/share/elasticsearch/data

will cause the /usr/share/elasticsearch/data directory within the elastic search container to be loaded onto the ./esdata directory in the host. Here, . is the directory where the docker-compose.yml is present. There's an important catch though. The ./esdata directory should be owned by elasticsearch:elasticsearch mapping to uid:gid = 1000:1000. So, it's mandatory that the elasticsearch:elasticsearch user with the exact uid:gid is present on the host.

Share:
19,613
L. Faros
Author by

L. Faros

Updated on June 16, 2022

Comments

  • L. Faros
    L. Faros almost 2 years

    I have the following docker-compose file :

    version: "3.3"
    services:    
      elasticsearch:
        image: docker.elastic.co/elasticsearch/elasticsearch:6.1.1
        volumes:
          - esdata:/usr/share/elasticsearch/data
        environment:
          - bootstrap.memory_lock=true
          - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
          - discovery.type=single-node
        ports:
          - "9300:9300"
          - "9200:9200"
    
    volumes:
      esdata:
    

    (I removed other services for clarity) I can see the volume in /var/lib/docker/volumes/project_name_esdata but I would like to be able to create the volume in the directory where the docker-compose.yml is but I didn't find a way to do so.

    Inspired from How to set a path on host for a named volume in docker-compose.yml, I tried

    version: "3.3"
    services:    
      elasticsearch:
        image: docker.elastic.co/elasticsearch/elasticsearch:6.1.1
        volumes:
          - esdata:/usr/share/elasticsearch/data
        environment:
          - bootstrap.memory_lock=true
          - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
          - discovery.type=single-node
        ports:
          - "9300:9300"
          - "9200:9200"
    
    volumes:
      esdata:
        driver: local
        driver_opts:
          type: 'none'
          o: 'bind'
          device: './' 
    

    But that raise the following exception :

    Caused by: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes 
    

    Please, let me know if I should post the full stack trace or any other relevant informations.