Answers: <p>I do not know why but a working version of mine is to use <code>volumes</code> like this:</p>
  <pre><code>
  version: &quot;3.3&quot;
  services:
  
    elasticsearch:
     build:
        context: .
        dockerfile: ./dockerfiles/DockerfileElasticSearch
     networks:
        private:
        public:
          ipv4_address: ${IP_BASE}.4
     env_file: env/elasticsearch.env
     volumes:
        - &quot;elasticsearch:/usr/share/elasticsearch/data:rw&quot;
     environment:
       - &quot;discovery.type=single-node&quot;
       ## Avoid test failures due to small disks
       ## More info at https://github.com/markshust/docker-magento/issues/488
       - &quot;cluster.routing.allocation.disk.threshold_enabled=false&quot;
       - &quot;index.blocks.read_only_allow_delete&quot;
  
  volumes:
    elasticsearch:
      driver: local
      driver_opts:
        type: none
        o: bind
        device: './volumes/elasticsearch'
  
  networks:
    private:
    public:
      ipam:
        config:
          - subnet: ${IP_BASE}.0/24
            gateway: ${IP_BASE}.1 
  </code></pre>
  <p>But you must make the folder <code>./volumes/elasticsearch</code> first.</p>
  
Answers: CC BY-SA 4.0
Answers: 2023-09-26T07:57:00.453Z
Answers: 2023-09-26T07:57:00.453Z
Answers: Dimitrios Desyllas
Body: <p>In my docker-compose I setup an elasticsearch container:</p>
  <pre><code>version: &quot;3.3&quot;
  services:
  
    elasticsearch:
     build:
        context: .
        dockerfile: ./dockerfiles/DockerfileElasticSearch
     networks:
        private:
        public:
          ipv4_address: ${IP_BASE}.4
     env_file: env/elasticsearch.env
     volumes:
        - &quot;./volumes/elasticsearch:/usr/share/elasticsearch/data:rw&quot;
     environment:
       - &quot;discovery.type=single-node&quot;
       ## Avoid test failures due to small disks
       ## More info at https://github.com/markshust/docker-magento/issues/488
       - &quot;cluster.routing.allocation.disk.threshold_enabled=false&quot;
       - &quot;index.blocks.read_only_allow_delete&quot;
  
  networks:
    private:
    public:
      ipam:
        config:
          - subnet: ${IP_BASE}.0/24
            gateway: ${IP_BASE}.1 
  </code></pre>
  <p>But the elasticsearch container upon <code>docker-compose up -d</code> is terminated:</p>
  <pre><code>{&quot;type&quot;: &quot;server&quot;, &quot;timestamp&quot;: &quot;2023-09-25T07:45:25,470Z&quot;, &quot;level&quot;: &quot;ERROR&quot;, &quot;component&quot;: &quot;o.e.b.ElasticsearchUncaughtExceptionHandler&quot;, &quot;cluster.name&quot;: &quot;docker-cluster&quot;, &quot;node.name&quot;: &quot;d87be4eb6a30&quot;, &quot;message&quot;: &quot;uncaught exception in thread [main]&quot;, 
  &quot;stacktrace&quot;: [&quot;org.elasticsearch.bootstrap.StartupException: ElasticsearchException[failed to bind service]; nested: AccessDeniedException[/usr/share/elasticsearch/data/nodes];&quot;,
  &quot;at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:174) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:127) ~[elasticsearch-cli-7.7.0.jar:7.7.0]&quot;,
  &quot;at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.7.0.jar:7.7.0]&quot;,
  &quot;at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;Caused by: org.elasticsearch.ElasticsearchException: failed to bind service&quot;,
  &quot;at org.elasticsearch.node.Node.&lt;init&gt;(Node.java:638) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;at org.elasticsearch.node.Node.&lt;init&gt;(Node.java:264) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;at org.elasticsearch.bootstrap.Bootstrap$5.&lt;init&gt;(Bootstrap.java:227) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:227) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:393) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;... 6 more&quot;,
  &quot;Caused by: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes&quot;,
  &quot;at sun.nio.fs.UnixException.translateToIOException(UnixException.java:90) ~[?:?]&quot;,
  &quot;at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111) ~[?:?]&quot;,
  &quot;at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116) ~[?:?]&quot;,
  &quot;at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:389) ~[?:?]&quot;,
  &quot;at java.nio.file.Files.createDirectory(Files.java:694) ~[?:?]&quot;,
  &quot;at java.nio.file.Files.createAndCheckIsDirectory(Files.java:801) ~[?:?]&quot;,
  &quot;at java.nio.file.Files.createDirectories(Files.java:787) ~[?:?]&quot;,
  &quot;at org.elasticsearch.env.NodeEnvironment.lambda$new$0(NodeEnvironment.java:274) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;at org.elasticsearch.env.NodeEnvironment$NodeLock.&lt;init&gt;(NodeEnvironment.java:211) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;at org.elasticsearch.env.NodeEnvironment.&lt;init&gt;(NodeEnvironment.java:271) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;at org.elasticsearch.node.Node.&lt;init&gt;(Node.java:284) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;at org.elasticsearch.node.Node.&lt;init&gt;(Node.java:264) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;at org.elasticsearch.bootstrap.Bootstrap$5.&lt;init&gt;(Bootstrap.java:227) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:227) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:393) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170) ~[elasticsearch-7.7.0.jar:7.7.0]&quot;,
  &quot;... 6 more&quot;] }
  uncaught exception in thread [main]
  ElasticsearchException[failed to bind service]; nested: AccessDeniedException[/usr/share/elasticsearch/data/nodes];
  Likely root cause: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes
      at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:90)
      at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
      at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
      at java.base/sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:389)
      at java.base/java.nio.file.Files.createDirectory(Files.java:694)
      at java.base/java.nio.file.Files.createAndCheckIsDirectory(Files.java:801)
      at java.base/java.nio.file.Files.createDirectories(Files.java:787)
      at org.elasticsearch.env.NodeEnvironment.lambda$new$0(NodeEnvironment.java:274)
      at org.elasticsearch.env.NodeEnvironment$NodeLock.&lt;init&gt;(NodeEnvironment.java:211)
      at org.elasticsearch.env.NodeEnvironment.&lt;init&gt;(NodeEnvironment.java:271)
      at org.elasticsearch.node.Node.&lt;init&gt;(Node.java:284)
      at org.elasticsearch.node.Node.&lt;init&gt;(Node.java:264)
      at org.elasticsearch.bootstrap.Bootstrap$5.&lt;init&gt;(Bootstrap.java:227)
      at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:227)
      at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:393)
      at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170)
      at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161)
      at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86)
      at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:127)
      at org.elasticsearch.cli.Command.main(Command.java:90)
      at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126)
      at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92)
  For complete error details, refer to the log at /usr/share/elasticsearch/logs/docker-cluster.log
  
  </code></pre>
  <p>Instead if I remove the volumes section runs fine:</p>
  <pre><code>version: &quot;3.3&quot;
  services:
  
    elasticsearch:
     build:
        context: .
        dockerfile: ./dockerfiles/DockerfileElasticSearch
     networks:
        private:
        public:
          ipv4_address: ${IP_BASE}.4
     env_file: env/elasticsearch.env
     environment:
       - &quot;discovery.type=single-node&quot;
       ## Avoid test failures due to small disks
       ## More info at https://github.com/markshust/docker-magento/issues/488
       - &quot;cluster.routing.allocation.disk.threshold_enabled=false&quot;
       - &quot;index.blocks.read_only_allow_delete&quot;
  
  networks:
    private:
    public:
      ipam:
        config:
          - subnet: ${IP_BASE}.0/24
            gateway: ${IP_BASE}.1 
  </code></pre>
  <p>Any idea why? How I can retain the elasticsearch data?</p>
  
Tags: <docker-compose><elasticsearch>
Title: Why Elasticsearch is terminated if I bind mount a volume?
UserDisplayName: Dimitrios Desyllas
ThreadText: # Why Elasticsearch is terminated if I bind mount a volume?
  
  (Asked by: username_0 on 2023-09-25)
  
  In my docker-compose I setup an elasticsearch container:
  
  ```
  version: "3.3"
  services:
  
    elasticsearch:
     build:
        context: .
        dockerfile: ./dockerfiles/DockerfileElasticSearch
     networks:
        private:
        public:
          ipv4_address: ${IP_BASE}.4
     env_file: env/elasticsearch.env
     volumes:
        - "./volumes/elasticsearch:/usr/share/elasticsearch/data:rw"
     environment:
       - "discovery.type=single-node"
       ## Avoid test failures due to small disks
       ## More info at https://github.com/markshust/docker-magento/issues/488
       - "cluster.routing.allocation.disk.threshold_enabled=false"
       - "index.blocks.read_only_allow_delete"
  
  networks:
    private:
    public:
      ipam:
        config:
          - subnet: ${IP_BASE}.0/24
            gateway: ${IP_BASE}.1 
  
  ```
  
  But the elasticsearch container upon `docker-compose up -d` is terminated:
  
  ```
  {"type": "server", "timestamp": "2023-09-25T07:45:25,470Z", "level": "ERROR", "component": "o.e.b.ElasticsearchUncaughtExceptionHandler", "cluster.name": "docker-cluster", "node.name": "d87be4eb6a30", "message": "uncaught exception in thread [main]", 
  "stacktrace": ["org.elasticsearch.bootstrap.StartupException: ElasticsearchException[failed to bind service]; nested: AccessDeniedException[/usr/share/elasticsearch/data/nodes];",
  "at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:174) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:127) ~[elasticsearch-cli-7.7.0.jar:7.7.0]",
  "at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.7.0.jar:7.7.0]",
  "at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "Caused by: org.elasticsearch.ElasticsearchException: failed to bind service",
  "at org.elasticsearch.node.Node.<init>(Node.java:638) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "at org.elasticsearch.node.Node.<init>(Node.java:264) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:227) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:227) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:393) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "... 6 more",
  "Caused by: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes",
  "at sun.nio.fs.UnixException.translateToIOException(UnixException.java:90) ~[?:?]",
  "at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111) ~[?:?]",
  "at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116) ~[?:?]",
  "at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:389) ~[?:?]",
  "at java.nio.file.Files.createDirectory(Files.java:694) ~[?:?]",
  "at java.nio.file.Files.createAndCheckIsDirectory(Files.java:801) ~[?:?]",
  "at java.nio.file.Files.createDirectories(Files.java:787) ~[?:?]",
  "at org.elasticsearch.env.NodeEnvironment.lambda$new$0(NodeEnvironment.java:274) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "at org.elasticsearch.env.NodeEnvironment$NodeLock.<init>(NodeEnvironment.java:211) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "at org.elasticsearch.env.NodeEnvironment.<init>(NodeEnvironment.java:271) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "at org.elasticsearch.node.Node.<init>(Node.java:284) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "at org.elasticsearch.node.Node.<init>(Node.java:264) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:227) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:227) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:393) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170) ~[elasticsearch-7.7.0.jar:7.7.0]",
  "... 6 more"] }
  uncaught exception in thread [main]
  ElasticsearchException[failed to bind service]; nested: AccessDeniedException[/usr/share/elasticsearch/data/nodes];
  Likely root cause: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes
      at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:90)
      at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
      at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
      at java.base/sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:389)
      at java.base/java.nio.file.Files.createDirectory(Files.java:694)
      at java.base/java.nio.file.Files.createAndCheckIsDirectory(Files.java:801)
      at java.base/java.nio.file.Files.createDirectories(Files.java:787)
      at org.elasticsearch.env.NodeEnvironment.lambda$new$0(NodeEnvironment.java:274)
      at org.elasticsearch.env.NodeEnvironment$NodeLock.<init>(NodeEnvironment.java:211)
      at org.elasticsearch.env.NodeEnvironment.<init>(NodeEnvironment.java:271)
      at org.elasticsearch.node.Node.<init>(Node.java:284)
      at org.elasticsearch.node.Node.<init>(Node.java:264)
      at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:227)
      at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:227)
      at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:393)
      at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170)
      at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161)
      at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86)
      at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:127)
      at org.elasticsearch.cli.Command.main(Command.java:90)
      at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126)
      at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92)
  For complete error details, refer to the log at /usr/share/elasticsearch/logs/docker-cluster.log
  
  ```
  
  Instead if I remove the volumes section runs fine:
  
  ```
  version: "3.3"
  services:
  
    elasticsearch:
     build:
        context: .
        dockerfile: ./dockerfiles/DockerfileElasticSearch
     networks:
        private:
        public:
          ipv4_address: ${IP_BASE}.4
     env_file: env/elasticsearch.env
     environment:
       - "discovery.type=single-node"
       ## Avoid test failures due to small disks
       ## More info at https://github.com/markshust/docker-magento/issues/488
       - "cluster.routing.allocation.disk.threshold_enabled=false"
       - "index.blocks.read_only_allow_delete"
  
  networks:
    private:
    public:
      ipam:
        config:
          - subnet: ${IP_BASE}.0/24
            gateway: ${IP_BASE}.1 
  
  ```
  
  Any idea why? How I can retain the elasticsearch data?
  
  **Tags:** docker-compose, elasticsearch
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2023-09-26. Score: 0
  
  I do not know why but a working version of mine is to use `volumes` like this:
  
  ```
  
  version: "3.3"
  services:
  
    elasticsearch:
     build:
        context: .
        dockerfile: ./dockerfiles/DockerfileElasticSearch
     networks:
        private:
        public:
          ipv4_address: ${IP_BASE}.4
     env_file: env/elasticsearch.env
     volumes:
        - "elasticsearch:/usr/share/elasticsearch/data:rw"
     environment:
       - "discovery.type=single-node"
       ## Avoid test failures due to small disks
       ## More info at https://github.com/markshust/docker-magento/issues/488
       - "cluster.routing.allocation.disk.threshold_enabled=false"
       - "index.blocks.read_only_allow_delete"
  
  volumes:
    elasticsearch:
      driver: local
      driver_opts:
        type: none
        o: bind
        device: './volumes/elasticsearch'
  
  networks:
    private:
    public:
      ipam:
        config:
          - subnet: ${IP_BASE}.0/24
            gateway: ${IP_BASE}.1 
  
  ```
  
  But you must make the folder `./volumes/elasticsearch` first.
  
  
  ---
  
  