1

I've been developing a webpage with golang using Postgres. I could run the app without a docker container and I could connect to the database, no problem occurred. However, when I dockerized my app and run it, Postgres showed an error: dial tcp 127.0.0.1:5432: connect: connection refused.

Connecting to postgres:

db, err := sql.Open("postgres", "host=localhost port=5432 user=postgres password=postgres dbname=postgres sslmode=disable")

I searched for the internet and found that inside the container localhost will direct to the container. I tried to connect specifying the IP of my server:

db, err := sql.Open("postgres", "host=XXX.XXX.XX.XXX port=5432 user=postgres password=postgres dbname=postgres sslmode=disable")

it also showed the same error: dial tcp XXX.XXX.XX.XXX:5432: connect: connection refused

when I run it without dockerizing it works fine. What to do?

Here is my dockerfile. I was just trying to upload the simplest app to the dedicated server. I logged in using ssh root@IP and installed go and postgres. I pulled my app to the server and then started it with just go build and ./app. Everything worked fine. Now I wanna run it using the dockerfile below.

FROM golang:alpine RUN mkdir /app ADD . /app WORKDIR /app COPY go.mod . RUN go mod download COPY . . RUN go build -o main . CMD ["/app/main"]

  • you need to map your port of host to that of container, `-p 5432:5432`, while running. – nilsocket May 29 '20 at 22:29
  • @nilsocket I am using the command : `docker run -it -p 8001:8001 my_app` to run my docker. where 8001 is the port of my app. Where I should map port 5432? sorry Im stupid but how? – Yernar Duisebai May 29 '20 at 22:37
  • You are using port 5432 when you try to open a connection to the DB, therefore you'll need to map port 5432 on your container to that port on your own machine. So like @nilsocket said do something like `docker run -it -p 5432:5432 my_app` – darcycp May 30 '20 at 00:00
  • @darcycp you don't want to expose the DB outside of docker - so you do not want to use `-p 5432:5432`. The app inside the docker container is what needs to access the DB - this connection does not need to be exposed. – colm.anseo May 30 '20 at 00:08
  • @YernarDuisebai are you using docker-compose? if the app is in one container and postgres in another - they need to be networked. Please share your Dockerfile(s) or docker-compose file. – colm.anseo May 30 '20 at 00:11
  • @colm.anseo sorry your right, I thought Yenar was trying to connect to a running container with Postgres from Go code outside of the container on his local machine. – darcycp May 30 '20 at 00:12
  • @colm.anseo, I am using Dockerfile. Added it to the post with more information about my situation. Postgres is running on my server. I also checked all credentials used to connect the posters, everything is correct. – Yernar Duisebai May 30 '20 at 01:33
  • have you tried `host=host.docker.internal`? – Nameless May 30 '20 at 01:45
  • @YernarDuisebai add another `-p ` option as `docker run -it -p 5432:5432 -p 8001:8001 my_app` try and say it. – nilsocket May 30 '20 at 06:26
  • @nilsocket The `-p` is only used/needed for accessing ports in a container. postgres is running on the host computer - not in the container. – colm.anseo May 30 '20 at 12:27
  • If you are looking to deploy this into a docker/k8s type env. I'd suggest using `docker-compose` and run both `postgres` and your `app` as containers in the same network: all the network, container hostnames, docker image names are neatly organized in one yaml config. If you must connect your docker container to the host, review [this answer](https://stackoverflow.com/a/24326540/1218512). As @Nameless suggested, `host=host.docker.internal` will work, as will many other techniques in the cited answer. – colm.anseo May 30 '20 at 12:32

1 Answers1

0

After thoroughly checking the comments by @colm.anseo, I figured out how to access the host machine in the docker container. I used the flag --network="host" to run my docker container. So I used: docker run -it --network="host" myapp to run the container. As I understood, this will bind the IP of the docker container and host machine

you can read more about it in: From inside of a Docker container, how do I connect to the localhost of the machine?