sqlx icon indicating copy to clipboard operation
sqlx copied to clipboard

sqlx.Connect("postgres", addr) freezes

Open mraxus opened this issue 9 years ago • 12 comments

I'm building a docker image with a simple sqlx connection and running it in docker-compose. For some reason, the statement see snippet below does not connect or error out, it just freezes. No idea why. It is related to using alpine:latest docker base image. I have not tracked down the issue completely but connecting to the image from the psql cli works like a charm (with the same connection string as used for in the statement (addr).

The code I've used in my setup:

package main

import (
	"fmt"
	"github.com/jmoiron/sqlx"
	_ "github.com/lib/pq"
	"os"
)

func main() {
	addr := os.Getenv("DB")
	fmt.Println("Postgres addr: " + addr)

	_, err := sqlx.Connect("postgres", addr)

	if err != nil {
		fmt.Println("Could not connect...")
	} else {
		fmt.Println("Connecting successful")
	}
}

As stated above, this code causes problem when building a go executable that is run within a container based on the image alpine:latest, together with *docker-compose`.

I have set up a test project to reproduce the problems I am facing with: https://github.com/mraxus/mystery-golang-alpine

Would love to get some help into this and hope this is an actual bug in your lib (otherwise I am wasting your time, which is not my intention)

Cheers, Marcus

mraxus avatar Sep 11 '17 09:09 mraxus

@mraxus The connection string of the pq driver includes a connect_timeout parameter. If it is not specified it means the Connect method will wait indefinitely. Have you tried setting this parameter? Any error returned because of a timeout may provide a clue:

      _, err := sqlx.Connect("postgres", addr)

See: https://godoc.org/github.com/lib/pq#hdr-Connection_String_Parameters

jhngrant avatar Sep 11 '17 09:09 jhngrant

Thanks, I will look into that @jhngrant!

I have also discovered that sqlx is not the issue to this. It's something more basic. I made a test excluding sqlx where I use only lib/pq. I get the same results: https://github.com/mraxus/mystery-golang-alpine/pull/2

So I will close the issue for now but will comment here if I find out anything more.

mraxus avatar Sep 11 '17 09:09 mraxus

So I got this when setting the timeout (after adding a panic(err) in the error path):

alpine_1    | Could not connect...
alpine_1    | panic: dial tcp 205.251.219.44:5432: i/o timeout
alpine_1    |
alpine_1    | goroutine 1 [running]:
alpine_1    | main.main()
alpine_1    | 	/go/src/project.com/me/main.go:18 +0x219
o2_alpine_1 exited with code 2

mraxus avatar Sep 11 '17 09:09 mraxus

@mraxus Looks like you might need to open port TCP 5432 in your Docker file.

jhngrant avatar Sep 11 '17 09:09 jhngrant

@jhngrant

In the docker-compose.yml it is open:

postgres:
    image: postgres:latest
    ports:
        - "5432"
...

mraxus avatar Sep 11 '17 09:09 mraxus

Or do you mean the Dockerfile?

When going into (docker exec -it e321a743fdde /bin/sh) to the alpine (as named in the yml file) docker container and installing the postgresql package and connecting the the same connection string as used in sqlx.Connect(), I connect without issues. @

mraxus avatar Sep 11 '17 09:09 mraxus

@mraxus I'm guessing the port needs to be mapped:

ports:
      - 5432:5432

jhngrant avatar Sep 11 '17 09:09 jhngrant

@mraxus Have a look at: https://docs.docker.com/engine/examples/postgresql_service/

jhngrant avatar Sep 11 '17 09:09 jhngrant

Hmm, I think that it's the DNS lookup that is weird somehow. Thanks to your timeout I noticed that the IP used from within the golang program is different from what I get when pinging from within the container:

dial tcp 205.251.219.236:5432: i/o timeout

vs

/ # ping postgres
PING postgres (172.17.0.2): 56 data bytes

mraxus avatar Sep 11 '17 09:09 mraxus

So I assume this is the reason. Thanks @jhngrant for helping me tracking this issue down. Must be a DNS lookup issue within the container.

mraxus avatar Sep 11 '17 10:09 mraxus

After some more digging with a colleague, we have found a solution to my problem (with running connecting between docker-compose containers by using the GODEBUG=netdns=cgo environment variable. That forces go to use the default resolver, and not Go's own. So just adding that in the docker-compose.yml solved my problem.

See more about name resolution: https://golang.org/pkg/net/#hdr-Name_Resolution This docker issue seems related: https://github.com/docker-library/golang/issues/109

mraxus avatar Sep 11 '17 11:09 mraxus

@jhngrant The real problem turned out to be a search domain set up on our corporate network. This is by default propagated to each container in their /etc/resolv.conf and for some reason that entry affected to alpine containers, but not the golang. By overwriting the config in docker-compose.yml, the issue has been completely resolved. Such a crazy hunt, elusive prey...

The docker issue referenced above is not the cause for this.

mraxus avatar Sep 13 '17 06:09 mraxus