21

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.

L. Faros
  • 1,754
  • 3
  • 16
  • 35

3 Answers3

33

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:
pacuna
  • 1,831
  • 16
  • 15
  • Thank you for your answer, but is there a way to still do so ? I am thinking about some command that would allow the folder to be read. for instance, I also run a mongodb container, and there is no issue putting volume in the app folder – L. Faros Sep 17 '18 at 18:31
  • yes, like I mentioned, add . to the first volumes line (./esdata). That mounts the volume in the same folder. – pacuna Sep 17 '18 at 18:33
  • Yes good call, I'll accept your answer since unlike the other one,it has the advantage of dealing with relative paths. And my apologies, I didn' read your answer thoroughly the first time since I was pretty sure that I had already tried the `./esdata:/usr/share/elasticsearch/data` – L. Faros Sep 17 '18 at 18:45
3

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.

Binita Bharati
  • 5,239
  • 1
  • 43
  • 24
0

Your config is almost ok but you need to use absolute path as device: ./ is causing mount of root file system instead of current directory (that's why you receive access denied)

Use absolute path in device like device: /opt/your-project/data - please also note that directory must exist before use.

Jakub Bujny
  • 4,400
  • 13
  • 27