我今天设置了一个运行golang:1.13-alpine
图像以及Elasticsearch和Kibana的最新图像的环境。
从本地计算机访问时,Elasticsearch和Kibana运行正常,但是我无法通过Go服务器连接到Elasticsearch。我将这些内容与我找到并遵循的指南放在一起。
使用Docker我还是有点绿。我有一个想法,我指向的是容器中错误的ip地址,但不确定如何解决。希望有人可以指引我正确的方向。
docker-compose.yml:
version: "3.7"
services:
web:
image: go-docker-webserver
build: .
ports:
- "8080:8080"
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.4.2
environment:
node.name: elasticsearch
cluster.initial_master_nodes: elasticsearch
cluster.name: docker-cluster
bootstrap.memory_lock: "true"
ES_JAVA_OPTS: -Xms256m -Xmx256m
ulimits:
memlock:
soft: -1
hard: -1
ports:
- "9200:9200"
kibana:
image: docker.elastic.co/kibana/kibana:7.4.2
ports:
- "5601:5601"
links:
- elasticsearch
文档文件:
FROM golang:1.13-alpine as builder
RUN apk add --no-cache --virtual .build-deps \
bash \
gcc \
git \
musl-dev
RUN mkdir build
COPY . /build
WORKDIR /build
RUN go get
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-extldflags "-static"' -o webserver .
RUN adduser -S -D -H -h /build webserver
USER webserver
FROM scratch
COPY --from=builder /build/webserver /app/
WORKDIR /app
EXPOSE 8080
EXPOSE 9200
CMD ["./webserver"]
main.go :
func webserver(logger *log.Logger) *http.Server {
router := http.NewServeMux()
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
es, err := elasticsearch.NewDefaultClient()
if err != nil {
log.Fatalf("Error creating the client: %s", err)
}
res, err := es.Info()
if err != nil {
log.Fatalf("Error getting response: %s", err)
}
log.Println(res)
})
return &http.Server{
Addr: listenAddr,
Handler: router,
ErrorLog: logger,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 15 * time.Second,
}
}
启动服务器时,一切运行良好,我可以访问Kibana并查询已索引的数据,但是一旦在Postman中点击localhost:8080
,服务器就会死机并输出:
web_1 | 2019/11/26 16:40:40 Error getting response: dial tcp 127.0.0.1:9200: connect: connection refused
go-api_web_1 exited with code 1
答案 0 :(得分:-2)
您可以在docker-compose文件中将网络称为网桥,并进行一次验证,其工作方式。
version: "3.7"
services:
web:
image: go-docker-webserver
build: .
ports:
- "8080:8080"
networks:
- elastic
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.4.2
environment:
node.name: elasticsearch
cluster.initial_master_nodes: elasticsearch
cluster.name: docker-cluster
bootstrap.memory_lock: "true"
ES_JAVA_OPTS: -Xms256m -Xmx256m
ulimits:
memlock:
soft: -1
hard: -1
ports:
- "9200:9200"
networks:
- elastic
kibana:
image: docker.elastic.co/kibana/kibana:7.4.2
ports:
- "5601:5601"
links:
- elasticsearch
networks:
- elastic
networks:
elastic:
driver: bridge
将内核设置设置为262144:
sysctl -w vm.max_map_count=262144