无法连接docker-compose启动的Elasticsearch实例:NoNodeAvailableException
Hey there, let's figure out why you're getting that frustrating NoNodeAvailableException with your Dockerized Elasticsearch instance. Looking at your curl output, I can spot the key clues we need to fix this.
First, let's break down what the curl result is telling us:
{
"cluster_name" : "elasticsearch",
"nodes" : {
"qD18rHzhQaexExUw5sBgXg" : {
"name" : "Scanner",
"transport_address" : "172.19.0.3:9300",
"host" : "172.19.0.3",
"ip" : "172.19.0.3",
"version" : "6.2.3",
"build" : "fcbb46d",
"http_address" : "172.19.0.3:9200",
"http" : {
"bound_address" : [ "0.0.0.0:9200" ],
"publish_address" : "172.19.0.3:9200",
"max_cont..."
}
}
}
}
Your Elasticsearch instance is running inside a Docker container with the internal IP 172.19.0.3, and it's bound to 0.0.0.0:9200 (good, that means it accepts connections from any interface). The problem is likely how your client is trying to connect to it, or missing port mapping in your docker-compose.yml.
Here are the steps to fix this:
1. Check Docker Compose Port Mapping
First, make sure your docker-compose.yml maps the container's 9200 port to your host machine. Without this, localhost:9200 on your host won't reach the container. Add this to your Elasticsearch service definition:
services: elasticsearch: # ... other config like image, environment ports: - "9200:9200" # Map host port 9200 to container port 9200 - "9300:9300" # For transport client (if you're using it)
2. Use the Correct Connection Address
- If your client is running on your host machine (not in Docker), connect to
http://localhost:9200(not the container's internal IP172.19.0.3). - If your client is running in another Docker container, make sure both containers are in the same Docker network, then connect using the service name from your
docker-compose.yml(e.g.,http://elasticsearch:9200). Avoid using the container's internal IP because it can change when you restart the container.
3. Add Single-Node Discovery Config
Since you're running a single-node cluster, add this environment variable to your Elasticsearch service to avoid cluster discovery issues (a common cause of connection problems):
services: elasticsearch: environment: - discovery.type=single-node # ... other env vars like ES_JAVA_OPTS if needed
4. Verify the Container is Running
Double-check that your Elasticsearch container is up and healthy:
docker-compose ps
You should see elasticsearch listed with a state of Up (preferably Up (healthy) if you have health checks configured).
Bonus: For Java TransportClient Users
If you're using the older TransportClient (which uses port 9300), make sure:
- Port 9300 is mapped in
docker-compose.yml(if connecting from the host) - Your client uses the correct address:
localhost:9300(host) orelasticsearch:9300(container network) - The client's Elasticsearch version matches your server's version (6.2.3 in your case)
Once you've made these changes, restart your Elasticsearch container with:
docker-compose down && docker-compose up -d
Then test the connection again with your curl command or client.
内容的提问来源于stack exchange,提问作者powder366

