Docker Community Forums

Share and learn in the Docker community.

IP Addr for Postgres

Hello Experts,

I am new to docker. I have vm player and running ubuntu LTS image running on it.

I have a docker image of postgres DB running in it.
From my laptop, I have spring boot based application running and I have to specify the ip address of postgres.
How to get ip address of postgres container? Please help.

Did you expose the port of your container? Probably not.
If you expose a port the container behaves like a normal programm on your computer. So your ip is localhost.

If your spring boot application runs on the host and the postgres container publishes the container port 5432 to the contaienr port 5432, then localhost:5432 is correct.

If your spring boot application runs in a different container in the default docker network, you need to link your spring boot container with the postgres container and use the link to address the database. The default docker network has no buildin dns server.

If your spring boot application runs in a different container in the same custom network, you can simply rely on the networks dns and use servicename:5432 to access the database.
.

Hello meyay,
Thanks for your reply.
It is my dev environment. In my laptop, I have IDE and im trying connect to postgres docker deployed on a ubuntu image using VM Player. How to go about it? Please suggest.

The response is still vague: is the IDE running in the VM Player VM, like the docker engine - or is it running on the host?

Sorry. IDE is in my laptop as well as VM workstation player.
Ubunte image is running on VM workstation player and postgres DB is in ubuntu image.

Okay, I uderstand that you want to be abe to run the spring boot application within your IDE (Eclipse, IntelliJ or something else…) on the host AND inside the VM. Technicaly both is possible.

On last thing: please share the exact docker run command or docker-compose.yml that you use to start the postgres container.

Hi,

Thanks for the quick reply.
I can start the docker without any issues.

I have a program running in Intellij to connect postgres. How to configure the IP address? Please help.

let me repeat myself:

Create Docker container with Postgres database:

docker create --name postgres-demo -e POSTGRES_PASSWORD= -p 5432:5432 postgres:11.5-alpine
Start container:

docker start postgres-demo

Okay, so you already publish a port.

If intelliJ is inside the vm, you need to use localhost:5432 to access the postgres database.

If intelliJ is running on the host, it pretty much depends if your VM uses the NAT or Host-only network.

If the network uses the Host-only network, you need to use ip of the vm:5432 to connect to postgres.

If the network uses NAT network, you need to start “Virtual Network Editor” as admin, select on the NAT network, click on “NAT Settings…” button, click “add” to add your port forwarding. Then you need to use localhost:5432 (or whatever host port you defined) to connect to postgres.

If you need help with NAT portforwarding, I would suggest to raise a topic in a vmware forum.

Thanks. I will give a try