Elasticsearch错误:无法连接到本地主机端口9200:连接被拒绝

时间:2019-03-07 16:28:50

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

我正在尝试创建安装了Elasticsearch的Docker映像。我知道Elasticsearch已经有他的自定义Docker映像,但是我需要使用安装了Python 3.6.5的AWS Lambda Function的相同环境。因此,我必须扩展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服务。我在做什么错了?

2 个答案:

答案 0 :(得分:0)

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

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

有关详细信息,请参见https://docs.docker.com/engine/reference/commandline/run/

答案 1 :(得分:0)

您要做的是使用应用程序的内部IP地址。这就是您的获得方式

dokku elasticsearch:info <Your elasticsearch container name here>

那么您现在就可以

curl http://172.75.0.7:9200

假设您的内部IP是172.75.0.7