Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not resolve postgres hostname from other service on docker-compose

I have a docker-compose file which run 2 containers. One of the service is postgres and the other one is my backend service. When I try to resolve postgres or try to connect it from cli of the backend service , it is successfull. But when I try to initialize database with hostname , I got following error:

2022/09/01 08:49:53 /src/domain/domain.go:29 [error] failed to initialize database, got error failed to connect to host=postgres user=devuser database=backenddev: hostname resolving error (lookup postgres: device or resource busy) panic: failed to connect to host=postgres user=devuser database=backenddev: hostname resolving error (lookup postgres: device or resource busy)

goroutine 1 [running]: github.com/anilkusc/backend/api.(*Api).Init(0xc00000e150) /src/api/api.go:34 +0x76b github.com/anilkusc/backend/api.(*Api).Start(0xc00000e150) /src/api/api.go:87 +0x25 main.main() /src/main.go:48 +0x15f

when I try to connect postgresql from another container cli , I got the following:

root@8a0824fca084:/# nc -vz postgres 5432
Connection to postgres (172.25.0.2) 5432 port [tcp/postgresql] succeeded!
root@8a0824fca084:/# curl postgres:5432
curl: (52) Empty reply from server

Here is related code block:

        d.Database, err = gorm.Open(postgres.Open(os.Getenv("DB_CONN")), &gorm.Config{})
        if err != nil {
            return err
        }

Here is compose file :

version: '3.6'
services:
  postgres:
    image: postgres:14.5
    restart: always
    environment:
      POSTGRES_PASSWORD: <>
      POSTGRES_DB: backenddev
      POSTGRES_USER: devuser 
    ports:
      - 5432:5432

  backend:
    image: my-service:v0.0.2
    restart: always
    environment:
      ENV: dev
      STORE_KEY: 1234
      DB_CONN: host=postgres user=devuser password=<> dbname=backenddev port=5432
    ports:
      - 8080:8080
    depends_on:
      - postgres

Here is dockerfile of backend service :

FROM golang:1.18.4 as build
WORKDIR /src
COPY go.sum go.mod ./
RUN go mod download
COPY . . 
RUN go build -a -ldflags "-linkmode external -extldflags '-static' -s -w" -o /bin/app.

FROM alpine
WORKDIR /app
COPY --from=build /bin/app .
CMD ["./app"]

If I try to connect to database with external ip and port on backend service it is also successfull but no luck for internal connection to postgresql.

Why my application can not resolve postgres host even though its host container can ?

like image 856
anilkuscu Avatar asked Oct 16 '25 13:10

anilkuscu


1 Answers

thanks to @Brits. It is probably related with incompatibility between alpine container and golang 1.18.4 container dns libraries. I changed my container image to debian from alpine and the issue has been resolved.

FROM golang:1.18.4 as build
WORKDIR /src
COPY go.sum go.mod ./
RUN go mod download
COPY . . 
RUN go build -a -ldflags "-linkmode external -extldflags '-static' -s -w" -o /bin/app.

FROM debian:buster-slim #just changed here!
WORKDIR /app
COPY --from=build /bin/app .
CMD ["./app"]
like image 186
anilkuscu Avatar answered Oct 18 '25 06:10

anilkuscu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!