I have host with PostgreSQL and Docker container. PostgreSQL work on 5432 port. Docker container must connect to database. How to connect container with database through Dockerfile or run command? EXPOSE 5432
and docker run -p 5432:5432 ...
did not help.
Asked
Active
Viewed 661 times
1

Vladimir37
- 538
- 1
- 6
- 21
1 Answers
1
From the documentation page:
Sometimes you need to connect to the Docker host from within your container. To enable this, pass the Docker host’s IP address to the container using the
--add-host
flag. To find the host’s address, use theip addr show
command.
$ HOSTIP=`ip -4 addr show scope global dev eth0 | grep inet | awk '{print \$2}' | cut -d / -f 1`
$ docker run --add-host=docker:${HOSTIP} --rm -it busybox telnet docker 5432
EXPOSE
or -p
flag work the other way around e.g. publish container ports to host which you don't want here.

hassansin
- 16,918
- 3
- 43
- 49
-
Or just connect to the IP of the PostgreSQL host. – Patryk Sidzina Feb 12 '16 at 09:56