title: connect: cannot assign requested address
body: <p>I have the following error message from a Go app in Docker:</p>
  
  <pre class="lang-sh prettyprint-override"><code>panic: failed to connect to `host=localhost user=postgres-dev database=dev`: dial error (dial tcp [::1]:5432: connect: cannot assign requested address)
  </code></pre>
  
  <p>which appear in the environment of the next <code>Dockerfile</code> and <code>docker-compose.yml</code> file:</p>
  
  <pre><code>FROM golang:latest
  
  WORKDIR /WD
  
  COPY go.mod go.sum ./
  
  RUN go mod download
  
  COPY . .
  </code></pre>
  
  <p>the <code>docker-compose.yml</code> file:</p>
  
  <pre><code>version: '3'
  services:
      db:
          image: postgres:latest
          environment:
              POSTGRES_DB: dev
              POSTGRES_USER: postgres-dev
              POSTGRES_PASSWORD: [~secret~]
              ports: 5432:5432
      app:
          build: .
          command: ["./wait-for-it.sh", "db:5432", "--", "go", "run", "main.go"]
          volumes:
              - .:/WD
          ports:
              - "8000:8000"
          depends_on: 
              - db
          links: 
              - db
  
  </code></pre>
  
  <p>here the <code>main.go</code> file:</p>
  
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      "context"
      "fmt"
      "log"
      "net/http"
  
      api "github.com/[placeholder]/[placeholder]/api"
  
      db "github.com/[placeholder]/[placeholder]/db"
  
      pgx "github.com/jackc/pgx/v4"
  )
  
  func main() {
      fmt.Println("Init")
      r := api.InitRoutes()
  
      conn, err := pgx.Connect(context.Background(), "postgresql://postgres-dev:[~secret~]@localhost:5432/dev")
      if err != nil {
          panic(err) // the error appears from this line.
      }
  
      dbInstance := &amp;db.DbService{Conn: conn}
  
      dbInstance.Conn.Ping(context.Background())
  
      dbInstance.Migrate("/db/db.sql")
  
      http.ListenAndServe(":8000", r)
  }
  </code></pre>
  
  <h1>May this be helpful?</h1>
  
  <p>in console logs I found the next lines, which I think related to the problem:</p>
  
  <pre class="lang-sh prettyprint-override"><code>db_1   | 2019-12-07 08:08:59.350 UTC [1] LOG:  starting PostgreSQL 12.1 (Debian 12.1-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
  db_1   | 2019-12-07 08:08:59.351 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
  # read the next line:
  db_1   | 2019-12-07 08:08:59.351 UTC [1] LOG:  listening on IPv6 address "::", port 5432 
  </code></pre>
  
  <p>the address of the database is <code>:: with port 5432</code> and while the program try to connect to <code>::1 with port 5432</code>, could this be the cause of the problem?</p>