Hello,
I'm running 2 Docker containers linked together:
* application written in Go
* database (Postgres)
If you are not familiar with Docker, link creates entry in /etc/hosts with IP of the other container (in this case database). So after linking database container, /etc/hosts in app container looks as follows:
# cat /etc/hosts
172.17.0.32 8c70febffc58
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.31 db 23fa128ea263 vagrant_db_1
I tried to connect to database using following code (using
github.com/lib/pq driver):
database, _ = sql.Open("postgres", "postgres://postgres@db:5432/test?sslmode=disable")
err = database.Ping()
if err != nil {
log.Println(err)
}
It always seemed to ignore /etc/hosts, failing with "dial tcp: lookup db on
10.0.2.3:53: server misbehaving". I expect it bypassed my hosts file and tried to resolve it via DNS. I tried to debug it using GODEBUG=netdns=... as described in
https://golang.org/pkg/net/#hdr-Name_Resolution and found out that connection was successfully established when I used GODEBUG=netdns=go or GODEBUG=netdns=cgo. I find that odd since the documentation mentions that "go" is used by default, but it behaved differently when I explicitly set it.
I'm not sure if it's some problem with lib/pq, Golang or I'm misunderstading something. Any ideas what could be happening?
I'm using Go 1.5.2, running inside Alpine Linux Docker container, running inside virtual Ubuntu (Linux vagrant-ubuntu-trusty-64 3.13.0-48-generic #80-Ubuntu SMP Thu Mar 12 11:16:15 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux) on OSX El Capitan.