简体   繁体   中英

Elasticsearch error: Failed to connect to localhost port 9200: Connection refused

I'm trying to create a Docker image with Elasticsearch installed. I know that Elasticsearch already has his custom Docker image but I need to use the same environment of the AWS Lambda Function with Python 3.6.5 installed. So I had to extend the AWS Docker image and I had to install the Elasticsearch service.

Here you can find my 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

When I build the image, the image is built correctly and the elasticsearch service is running. I can see the log: elasticsearch (pid 87) is running... . The problem is that I can't make API call to http://localhost:9200 because it says: Failed to connect to localhost port 9200: Connection refused

I need to use the elasticsearch service inside the container and not from outside. What am I doing wrong?

You don't indicate how you are actually running the docker container. Simply exposing the port in the docker file isn't enough. You have to run with the -p option as well. For example:

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

See https://docs.docker.com/engine/reference/commandline/run/ for details.

What you want to do is use the internal ip address of your app. This is how you get it

dokku elasticsearch:info <Your elasticsearch container name here>

Then you can now do

curl http://172.75.0.7:9200

Assuming your internal IP is 172.75.0.7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM