带有 Docker Compose 的 ElasticSearch 服务器:java.nio.file.AccessDeniedException:/usr/share/elasticsearch/data/nodes

Joh*_*lon 2 ruby-on-rails elasticsearch docker digital-ocean docker-compose

我正在运行带有 PostgreSQL 数据库和 ElasticSearch 服务器的 Ruby on Rails 应用程序。我将这些放在单独的 Docker 容器中,这些容器使用 Docker Compose 在网络中协同工作,并且它托管在我以 root 身份登录的 Digital Ocean Droplet (Ubuntu 20.04.1 LTS) 上。作为参考,以下是使用命令“docker-compose ps”列出的容器:

          Name                         Command               State    Ports
---------------------------------------------------------------------------
database                    docker-entrypoint.sh postgres    Exit 0
myApp_elasticsearch_1   /tini -- /usr/local/bin/do ...   Exit 1
myApp_web_1             entrypoint.sh bash -c rm - ...   Exit 1
Run Code Online (Sandbox Code Playgroud)

当我使用命令时:

docker-compose up
Run Code Online (Sandbox Code Playgroud)

应用程序和数据库正常运行,但 ElasticSearch 服务器遇到错误并停止运行。输出中最相关的部分似乎是:

ElasticsearchException[failed to bind service]; nested: AccessDeniedException[/usr/share/elasticsearch/data/nodes];
Likely root cause: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes
Run Code Online (Sandbox Code Playgroud)

我查看了此错误的类似答案,它似乎与 linux 文件系统和用户访问权限有关,但是我不确定如何解决此问题,因为我缺乏 linux 文件系统和用户权限的经验。

这个答案Using Persistent Host Volume for ElasticSearch with Docker-Compose似乎最相似,但我仍然不确定如何将其应用于我的问题。

感谢任何帮助。

我的 docker-compose.yml 文件如下:

version: "3.8"   
services:
  web:
    build: .
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - bundle-volume:/usr/local/bundle
    ports:
      - "3000:3000"
    depends_on:
      - database
      - elasticsearch
    environment:
      RAILS_ENV: development
      DATABASE_NAME: myApp_development
      DATABASE_USER: postgres
      DATABASE_PASSWORD: **********
      POSTGRES_PASSWORD: **********
      DATABASE_HOST: database
      ELASTICSEARCH_URL: http://elasticsearch:9200

  database:
    image: postgres:12.3
    container_name: database
    volumes:
      - db_volume:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    ports:
      - "5432:5432"
    environment: 
      DATABASE_PASSWORD: **********
      POSTGRES_PASSWORD: **********
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.9.3
    volumes:
      - ./docker_data/elasticsearch/data:/usr/share/elasticsearch/data
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - discovery.type=single-node
    ports:
      - 9200:9200
    ulimits:
      memlock:
        soft: -1
        hard: -1
volumes:
  bundle-volume:
  db_volume:
  data:
Run Code Online (Sandbox Code Playgroud)

我的 Dockerfile 如下:

FROM ruby:2.6.6-buster

RUN apt-get update -qq && \
    apt-get install -y curl \
    build-essential \
    libpq-dev \
    postgresql \
    postgresql-contrib \
    postgresql-client

RUN mkdir /myApp
RUN mkdir -p /usr/local/nvm
WORKDIR /myApp

RUN curl -sL https://deb.nodesource.com/setup_15.x | bash -
RUN apt-get install -y nodejs
RUN node -v
RUN npm -v

COPY Gemfile Gemfile.lock package.json yarn.lock ./

RUN gem install bundler && bundle update --bundler && bundle install
RUN npm install -g yarn && yarn install --check-files

COPY . /myApp

COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000

CMD ["rails", "server", "-b", "0.0.0.0"]
Run Code Online (Sandbox Code Playgroud)

完整的 ElasticSearch 响应,包括下面显示的错误以供参考。

elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:02,394Z", "level": "INFO", "component": "o.e.n.Node", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "version[7.9.3], pid[6], build[default/docker/c4138e51121ef06a6404866cddc601906fe5c868/2020-10-16T10:36:16.141335Z], OS[Linux/5.4.0-52-generic/amd64], JVM[Oracle Corporation/OpenJDK 64-Bit Server VM/15/15+36-1562]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:02,400Z", "level": "INFO", "component": "o.e.n.Node", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "JVM home [/usr/share/elasticsearch/jdk]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:02,401Z", "level": "INFO", "component": "o.e.n.Node", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "JVM arguments [-Xshare:auto, -Des.networkaddress.cache.ttl=60, -Des.networkaddress.cache.negative.ttl=10, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -XX:-OmitStackTraceInFastThrow, -XX:+ShowCodeDetailsInExceptionMessages, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dio.netty.allocator.numDirectArenas=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Djava.locale.providers=SPI,COMPAT, -Xms1g, -Xmx1g, -XX:+UseG1GC, -XX:G1ReservePercent=25, -XX:InitiatingHeapOccupancyPercent=30, -Djava.io.tmpdir=/tmp/elasticsearch-15197622989873108878, -XX:+HeapDumpOnOutOfMemoryError, -XX:HeapDumpPath=data, -XX:ErrorFile=logs/hs_err_pid%p.log, -Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m, -Des.cgroups.hierarchy.override=/, -Xms512m, -Xmx512m, -XX:MaxDirectMemorySize=268435456, -Des.path.home=/usr/share/elasticsearch, -Des.path.conf=/usr/share/elasticsearch/config, -Des.distribution.flavor=default, -Des.distribution.type=docker, -Des.bundled_jdk=true]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,138Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [aggs-matrix-stats]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,138Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [analysis-common]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,140Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [constant-keyword]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,140Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [flattened]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,141Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [frozen-indices]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,142Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [ingest-common]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,142Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [ingest-geoip]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,143Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [ingest-user-agent]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,143Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [kibana]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,145Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [lang-expression]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,147Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [lang-mustache]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,148Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [lang-painless]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,148Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [mapper-extras]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,149Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [parent-join]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,149Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [percolator]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,150Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [rank-eval]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,150Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [reindex]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,151Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [repository-url]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,152Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [search-business-rules]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,152Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [searchable-snapshots]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,153Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [spatial]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,153Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [tasks]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,154Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [transform]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,155Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [transport-netty4]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,155Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [vectors]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,156Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [wildcard]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,156Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-analytics]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,156Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-async]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,157Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-async-search]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,157Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-autoscaling]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,158Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-ccr]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,158Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-core]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,159Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-data-streams]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,159Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-deprecation]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,159Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-enrich]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,160Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-eql]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,160Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-graph]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,161Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-identity-provider]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,161Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-ilm]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,162Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-logstash]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,162Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-ml]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,163Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-monitoring]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,164Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-ql]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,164Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-rollup]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,164Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-security]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,165Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-sql]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,165Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-stack]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,166Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-voting-only-node]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,166Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "loaded module [x-pack-watcher]" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,167Z", "level": "INFO", "component": "o.e.p.PluginsService", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "no plugins loaded" }
elasticsearch_1  | {"type": "server", "timestamp": "2020-12-14T19:38:05,281Z", "level": "ERROR", "component": "o.e.b.ElasticsearchUncaughtExceptionHandler", "cluster.name": "docker-cluster", "node.name": "e33ecad0776c", "message": "uncaught exception in thread [main]",
elasticsearch_1  | "stacktrace": ["org.elasticsearch.bootstrap.StartupException: ElasticsearchException[failed to bind service]; nested: AccessDeniedException[/usr/share/elasticsearch/data/nodes];",
elasticsearch_1  | "at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:174) ~[elasticsearch-7.9.3.jar:7.9.3]",
elasticsearch_1  | "at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161) ~[elasticsearch-7.9.3.jar:7.9.3]",
elasticsearch_1  | "at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.9.3.jar:7.9.3]",
elasticsearch_1  | "at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:127) ~[elasticsearch-cli-7.9.3.jar:7.9.3]",
elasticsearch_1  | "at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.9.3.jar:7.9.3]",
elasticsearch_1  | "at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126) ~[elasticsearch-7.9.3.jar:7.9.3]",
elasticsearch_1  | "at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.9.3.jar:7.9.3]",
elasticsearch_1  | "Caused by: org.elasticsearch.ElasticsearchException: failed to bind service",
elasticsearch_1  | "at org.elasticsearch.node.Node.<init>(Node.java:695) ~[elasticsearch-7.9.3.jar:7.9.3]",
elasticsearch_1  | "at org.elasticsearch.node.Node.<init>(Node.java:277) ~[elasticsearch-7.9.3.jar:7.9.3]",
elasticsearch_1  | "at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:227) ~[elasticsearch-7.9.3.jar:7.9.3]",
elasticsearch_1  | "at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:227) ~[elasticsearch-7.9.3.jar:7.9.3]",
elasticsearch_1  | "at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:393) ~[elasticsearch-7.9.3.jar:7.9.3]",
elasticsearch_1  | "at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170) ~[elasticsearch-7.9.3.jar:7.9.3]",
elasticsearch_1  | "... 6 more",
elasticsearch_1  | "Caused by: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes",
elasticsearch_1  | "at sun.nio.fs.UnixException.translateToIOException(UnixException.java:90) ~[?:?]",
elasticsearch_1  | 

Joh*_*lon 10

解决此错误的方法是在 docker 容器内的应用程序主目录中使用以下命令:

sudo chown -R 1000:root docker_data
Run Code Online (Sandbox Code Playgroud)

这提供了对路径“/usr/share/elasticsearch/data/nodes”的所需访问