L. Faros
L. Faros

Reputation: 2008

Docker bind elasticsearch volume in app folder

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.

Upvotes: 23

Views: 41276

Answers (3)

Binita Bharati
Binita Bharati

Reputation: 5908

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.

Upvotes: 5

Jakub Bujny
Jakub Bujny

Reputation: 4628

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.

Upvotes: 0

pacuna
pacuna

Reputation: 2099

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:

Upvotes: 36

Related Questions