I am trying to set up a docker for golang app with Postgres.
The go app works fine in a container if I remove/comment Postgres. And similarly, I am able to spin up Postgres container and log into it. I am able do docker-compose up.
But when I make a API call, like for eg: localhost:3000/api/admin/users
. It gives and error:
error: {
"error": "+dial tcp 127.0.0.1:5432: connect: connection refused"
}
The Postgres connection string is like this:
connStr := fmt.Sprintf("host=postgres user=anurag password=anu_12345 dbname=bankingapp sslmode=disable")
db, err := sql.Open("postgres", connStr)
Dockerfile
FROM golang:1.13
WORKDIR /go/src/banking-app
COPY . .
RUN go get -d -v ./...
RUN go install -v ./...
CMD ["go" , "run", "main.go"]
docker-compose.yml
version: '3'
services:
web:
build: .
ports:
- "3000:3000"
postgres:
image: "postgres"
environment:
POSTGRES_USER: 'anurag'
POSTGRES_PASSWORD: 'anu_12345'
POSTGRES_DB: 'bankingapp'
I found the answer. Just need to rebuild image or load using mount. The code was not refreshed.
Sorry to bother.