Cannot connect to Elasticsearch from Go server running in Docker : <p>I have set up an environment today that runs a <code>golang:1.13-alpine</code> image, along with the latest images for Elasticsearch and Kibana.</p>

<p>Elasticsearch and Kibana are running fine when accessing from my local machine, but I cannot connect to Elasticsearch through the Go server. I have put this together from guides I have found and followed. </p>

<p>I am still a bit green using Docker. I have an idea that I am pointing at the wrong ip address in the container, but I am unsure how to fix it. Hope someone can guide me in the right direction.</p>

<p><strong>docker-compose.yml:</strong></p>

<pre><code>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
</code></pre>

<p><strong>Dockefile:</strong></p>

<pre><code>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"]
</code></pre>

<p><strong>main.go</strong>:</p>

<pre><code>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 &amp;http.Server{
        Addr:         listenAddr,
        Handler:      router,
        ErrorLog:     logger,
        ReadTimeout:  5 * time.Second,
        WriteTimeout: 10 * time.Second,
        IdleTimeout:  15 * time.Second,
    }
}
</code></pre>

<p>When I boot the server, everything is running fine and I can access Kibana and query the data that I have indexed, but as soon as I hit <code>localhost:8080</code> in Postman, the server dies and outputs: </p>

<pre><code>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
</code></pre>
