【问题标题】:Elasticsearch error: Failed to connect to localhost port 9200: Connection refusedElasticsearch 错误:无法连接到 localhost 端口 9200:连接被拒绝
【发布时间】:2019-07-29 14:56:52
【问题描述】:

我正在尝试创建一个安装了 Elasticsearch 的 Docker 映像。我知道 Elasticsearch 已经有他的自定义 Docker 映像,但我需要使用安装了 Python 3.6.5 的 AWS Lambda 函数的相同环境。所以我不得不扩展 AWS Docker 镜像,并且必须安装 Elasticsearch 服务。

在这里你可以找到我的 Dockerfile:

FROM lambci/lambda-base:build

ENV PATH=/var/lang/bin:$PATH \
    LD_LIBRARY_PATH=/var/lang/lib:$LD_LIBRARY_PATH \
    AWS_EXECUTION_ENV=AWS_Lambda_python3.6 \
    PYTHONPATH=/var/runtime \
    PKG_CONFIG_PATH=/var/lang/lib/pkgconfig:/usr/lib64/pkgconfig:/usr/share/pkgconfig

RUN rm -rf /var/runtime /var/lang && \
  curl https://lambci.s3.amazonaws.com/fs/python3.6.tgz | tar -xz -C / && \
  sed -i '/^prefix=/c\prefix=/var/lang' /var/lang/lib/pkgconfig/python-3.6.pc && \
  curl https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tar.xz | tar -xJ && \
  cd Python-3.6.8 && \
  LIBS="$LIBS -lutil -lrt" ./configure --prefix=/var/lang && \
  make -j$(getconf _NPROCESSORS_ONLN) libinstall libainstall inclinstall && \
  cd .. && \
  rm -rf Python-3.6.8

EXPOSE 9200 9300

# Add these as a separate layer as they get updated frequently
RUN pip install -U pip setuptools --no-cache-dir && \
    pip install -U virtualenv pipenv --no-cache-dir && \
    pip install -U awscli boto3 aws-sam-cli==0.10.0 aws-lambda-builders==0.1.0 --no-cache-dir && \
    yum install wget -y && \
    echo "vm.max_map_count=262144" >> /etc/sysctl.conf && \
    # I try to install elasticsearch manually
    wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.1.rpm && \
    rpm --install elasticsearch-6.6.1.rpm && \
    service elasticsearch start && \
    chkconfig --add elasticsearch && \
    sed -i -e "s/#network.host: 192.168.0.1/network.host: 127.0.0.1/g" /etc/elasticsearch/elasticsearch.yml && \
    sed -i -e "s/#http.port: 9200/http.port: 9200/g" /etc/elasticsearch/elasticsearch.yml && \
    sed -i -e "s/# Elasticsearch performs poorly when the system is swapping the memory./network.bind_host: 127.0.0.1\nnetwork.publish_host: localhost/g" /etc/elasticsearch/elasticsearch.yml

RUN service elasticsearch start && \
    service elasticsearch status && \
    service elasticsearch status

当我构建镜像时,镜像构建正确,并且 elasticsearch 服务正在运行。我可以看到日志:elasticsearch (pid 87) is running...。问题是我无法对http://localhost:9200 进行API 调用,因为它说:Failed to connect to localhost port 9200: Connection refused

我需要在容器内部使用 elasticsearch 服务,而不是从外部使用。我做错了什么?

【问题讨论】:

  • 您无法在 Dockerfile 中启动后台服务(它们不会保留在映像中)。 Docker 中service 的典型用法由于几个原因不起作用,您应该直接启动该服务。 (尝试另一个RUN service elasticsearch status 命令,看看它说了什么。)
  • 另外,bind_host: 127.0.0.1 设置保证服务将无法从容器外部访问。在 Docker 中通常需要 0.0.0.0。
  • @DavidMaze 你是对的。如果我运行另一个RUN service elasticsearch status 命令,我会得到elasticsearch dead but pid file exists。如何直接启动服务?我不需要使用容器外的服务,但需要使用容器内的elasticsearch服务。
  • @DavidMaze 我还在 Dockerfile 的末尾添加了命令CMD elasticsearch start && bash,但错误是一样的。

标签: amazon-web-services docker elasticsearch dockerfile aws-codebuild


【解决方案1】:

您没有说明您实际运行 docker 容器的方式。仅在 docker 文件中公开端口是不够的。您还必须使用 -p 选项运行。例如:

docker run -p 9200:9200 <image name here>

详情请见https://docs.docker.com/engine/reference/commandline/run/

【讨论】:

  • 不幸的是,我无法决定如何运行 docker 容器,因为我在 AWS CodeBuild 中使用了这个镜像。在此服务中,您只能选择要使用的图像,然后它会自动运行。
【解决方案2】:

您想要做的是使用您应用的内部 IP 地址。这就是你得到它的方式

dokku elasticsearch:info <Your elasticsearch container name here>

那么你现在可以做

curl http://172.75.0.7:9200

假设你的内部 IP 是 172.75.0.7

【讨论】:

  • 是的,我想使用我的应用程序的内部 IP 地址。我发现这是 AWS CodeBuild 的问题。在此服务中,您不能使用localhost 字和内部 IP,因为存在网络限制。 (这是 AWS 解决方案架构师的回答)。我唯一要做的就是创建一个安装了 Elasticsearch 服务的新 Docker 容器并公开端口。现在我可以从我的 Docker 容器向 Elasticsearch Docker 进行 API 调用
猜你喜欢
  • 2015-10-19
  • 2016-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-20
  • 2015-12-03
  • 2016-03-24
  • 1970-01-01
相关资源
最近更新 更多