Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 661
L. Faros Avatar asked Sep 17 '18 17:09

L. Faros


People also ask

How do you attach a volume to docker container?

You bind local directories and volumes to a container by providing the Docker run -v parameter. You need to give the absolute local path or a volume name and map it to a directory within the container -v <source>:<target> .

How do I change docker volume location?

First stop the docker service. Then the volumes can be moved from the default location at /var/lib/docker to the new location. Next the configuration of the docker daemon is edited to point to the new location of the volumes. The next step may not be necessary, but it is a good habit to do it anyway.

What is the difference between docker volume and bind mount?

Though both methods are similar, there is a slight difference. Docker manages Volumes and is usually not affected by other processes running on the same host. In contrast, Bind Mounts are just a directory on the host file system and may be modified by other processes other than docker.

How do I bind a folder into a container?

Use the following command to bind-mount the target/ directory into your container at /app/ . Run the command from within the source directory. The $(pwd) sub-command expands to the current working directory on Linux or macOS hosts. If you're on Windows, see also Path conversions on Windows.


1 Answers

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:
like image 145
pacuna Avatar answered Sep 21 '22 13:09

pacuna