简体   繁体   中英

setting up elasticsearch in a docker image - seems to start but curl not responding

I'm trying to create a docker image that uses a local [single-node] elasticsearch instance. (In the docker container I intend to (1) start the elasticsearch instance; (2) create an index and load the documents through a python script; (3) make the index available to queries through a flask server.) I'm having problem with starting up the es instance. It looks like it is starting, but when I try curl localhost:9200 , it's getting connection refused. Here is what my Dockerfile looks like:

FROM ubuntu:latest

SHELL ["/bin/bash", "-c"]
RUN apt-get -y update && apt-get install -y --no-install-recommends \
         sudo \
         wget \
         gnupg \
         curl \
         unzip \
         tar \
         ca-certificates \
         lsb-release \
         python3-pip \
         python3-setuptools \
         nginx \
    && rm -rf /var/lib/apt/lists/*

#setup elasticsearch
RUN wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.6.0-linux-aarch64.tar.gz
RUN tar -xzvf elasticsearch-8.6.0-linux-aarch64.tar.gz

##create a new user and give es permissions to it to start a node
RUN useradd es_user
RUN chown es_user:es_user -R elasticsearch-8.6.0
USER es_user
RUN echo "discovery.type: single-node" >> elasticsearch-8.6.0/config/elasticsearch.yml
RUN echo "xpack.security.enabled: false" >> elasticsearch-8.6.0/config/elasticsearch.yml
#RUN echo "network.host: 0.0.0.0" >> elasticsearch-8.6.0/config/elasticsearch.yml
#RUN echo "http.port: 9200" >> elasticsearch-8.6.0/config/elasticsearch.yml
#RUN echo "http.host: 0.0.0.0" >> elasticsearch-8.6.0/config/elasticsearch.yml
RUN  elasticsearch-8.6.0/bin/elasticsearch -d
RUN sleep 30
RUN echo $(curl -s -w '%{http_code}' localhost:9200)

All the steps seem to be working fine, but the curl command is returning 000 rather than 200:

#18 [15/15] RUN echo $(curl -s -w '%{http_code}' localhost:9200)
#18 sha256:94cb4cd9486b0bf9a80c77c7648ba19ab373c3f8ec8e1131b3d68a0a55e0b268
#18 0.182 000
#18 DONE 0.2s

On the other hand, if I open a terminal into this container through docker desktop (if I comment out the call to elasticsearch so that the image can actually be built), I can start the elasticsearch using elasticsearch-8.6.0/bin/elasticsearch -d (the same way I have in the dockerfile). And then if I try the same curl command I will get 200.

{ "name" : "5bced6805917", "cluster_name" : "elasticsearch", "cluster_uuid" : "IAcnbmcfTmC84MjI3Y-jiA", "version" : { "number" : "8.6.0", "build_flavor" : "default", "build_type" : "tar", "build_hash" : "f67ef2df40237445caa70e2fef79471cc608d70d", "build_date" : "2023-01-04T09:35:21.782467981Z", "build_snapshot" : false, "lucene_version" : "9.4.2", "minimum_wire_compatibility_version" : "7.17.0", "minimum_index_compatibility_version" : "7.0.0" }, "tagline" : "You Know, for Search" }
200

PS. I'm on an M1 mac and building the image using docker build --progress=plain -t custom_es --no-cache.

The image you posted is not working.

12 1.008 elasticsearch-8.6.0/bin/elasticsearch-cli: line 14: /elasticsearch-8.6.0/jdk/bin/java: cannot execute binary file: Exec format error

I dont see anywhere a java-sdk installation, which is required for elasticsearch. That must be installed and exectutable inside the container.

On the other hand why should you execute elasticsearch directly in the image?

Think about a container like a Computer and about an image as an harddisk with OS and everything you need already installed. If you would install that harddisk in a computer and turn the power on, it would start and execute everything you have prepared.

A container needs an image. So what would the container when he start with the image? He will execute the CMD in dockerfile, or overwrite that with a command you indicate when the container is build.

To debug your image you can do following. Let everything reguarding the elasticsearch comment out.

Try it so:

FROM ubuntu:latest

SHELL ["/bin/bash", "-c"]
RUN apt-get -y update && apt-get install -y --no-install-recommends \
         sudo \
         wget \
         gnupg \
         curl \
         unzip \
         tar \
         ca-certificates \
         lsb-release \
         python3-pip \
         python3-setuptools \
         nginx \
    && rm -rf /var/lib/apt/lists/*

#setup elasticsearch
RUN wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.6.0-linux-aarch64.tar.gz
RUN tar -xzvf elasticsearch-8.6.0-linux-aarch64.tar.gz

##create a new user and give es permissions to it to start a node
RUN useradd es_user
RUN chown es_user:es_user -R elasticsearch-8.6.0
USER es_user

CMD ["sleep","999999"]

then run it and exec it or with Dockerdesktop or by command line docker exec.........

Then inside the container install step by step elasticsearch.

Each step which dowsnt generate an error add it to the Dockerfile.

Finally in your Docker file change the CMD with

CMD ["elasticserachcomand","option1","option2"]

CMD aspect an array of the command and its args.

Last thing:

It is not a good practice writing the configuration inside the image itselfs.

Prepare a file on your host. Lets say elastic.yaml. When you start the container with docker run (-v ) or docker compose (volumes), map this file inside the container.

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