I'm using docker-compose to run my golang app and a single node elastic search in my local machine and using https://github.com/vishnubob/wait-for-it for ordering start. I faced connection refused during connecting my golang app and elastic. I think i'm pointing correct ip from my golang container to my es container. I think i dont setting my es container correctly
error log :
web_1 | wait-for-it.sh: waiting 15 seconds for elastic:9200
web_1 | wait-for-it.sh: timeout occurred after waiting 15 seconds for elastic:9200
web_1 | 2020/11/16 04:05:12 Setup ElasticSearch: failed cause, dial tcp 172.28.0.3:9200: connect: connection refused
this is my configuration :
docker-compose.yml
version: "3.8"
services:
web:
build: .
command: ["./wait-for-it.sh","mongo:27017","redis:6379","elastic:9200","--","./server"]
ports:
- "8080:8080"
networks:
- s2l_network
depends_on:
- mongo
- redis
- elastic
mongo:
container_name: mongo
image: mongo
restart: always
ports:
- "27017:27017"
#volumes:
# - "s2l_mongo:/data/db"
networks:
- s2l_network
redis:
container_name: redis
image: redis
ports:
- "6379:6379"
networks:
- s2l_network
elastic:
container_name: elastic
image: docker.elastic.co/elasticsearch/elasticsearch:7.9.3
ports:
- "9200:9200"
- "9300:9300"
environment:
- discovery.type=single-node
- network.host=0.0.0.0
networks:
- s2l_network
networks:
s2l_network:
Dockerfile
FROM golang:1.14
ENV GO111MODULE=on
WORKDIR /app
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN chmod +x ./wait-for-it.sh
RUN go build
conection.go
cfg := elasticsearch.Config{
MaxRetries: 10,
Transport: &http.Transport{
ResponseHeaderTimeout: 10 * time.Second,
},
Addresses: []string{
"http://elastic:9200",
},
}
es, err := elasticsearch.NewClient(cfg)
if err != nil {
log.Printf("Setup ElasticSearch: failed cause, %s", err)
return nil, err
}
_, err = es.Info()
if err != nil {
log.Printf("Setup ElasticSearch: failed cause, %s", err)
return nil, err
}
elastic container inspect :
...
"NetworkSettings": {
"Bridge": "",
"SandboxID": "69cd26188f8e9c1b82df2ad6991b25c8e0bd27da965c2cfcd22a83d60127fd62",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"9200/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "9200"
}
],
"9300/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "9300"
}
]
},
"SandboxKey": "/var/run/docker/netns/69cd26188f8e",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"market-place_s2l_network": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"0060c27403e7",
"elastic"
],
"NetworkID": "c9181ea9396ed3fedb26852f130b0cec1f567951a433c5261eb9b4d0564a86d4",
"EndpointID": "f1adce667b74abf83967ba0115afe39f873b3611c25951b488c6be90b3563ec8",
"Gateway": "172.28.0.1",
"IPAddress": "172.28.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:1c:00:03",
"DriverOpts": null
}
}
}
}
what i'm thinking about my problem: because i run my golang app and elastic in two separate container, i must connect my golang app with elastic not by localhost network. so myconnection string uri should be "http://elastic:9200".
from elastic documentation :
https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html.
Elasticsearch binds to localhost only by default
my interpretation about that statement is we just only can connect by 127.0.0.1:9200 if we don't setting any other additional configuration. So we need this additional configuration setting to allow us connect from outside localhost.
network.host=0.0.0.0
i hope someone can explain me, if my basic understanding correct or false
Thanks