Joff
Joff

Reputation: 12187

docker networking: How to know which ip will be assigned?

Trying to get acquainted with docker, so bear with me...

If I create a database container (psql) with port 5432 exposed, and then create another webapp which wants to connect on 5432, they get assigned some ip addresses on the bridge network from docker...

probably 172.0.0.1 and 172.0.0.2 respectively. if I fire up the containers, inspect their ips with docker network inspect <bridge id>

if I then take those ips and plug in the port on my webapp settings, everything works great...

BUT I shouldn't have to run my webapp, shell into it, change settings, and then run a server, I should be able to just run the container...

So what am I missing here, is there a way to have these two containers networked without having to do all of that?

Upvotes: 0

Views: 131

Answers (1)

Dan Lowe
Dan Lowe

Reputation: 56657

Use a Docker network

docker create network myapp
docker run --network myapp --name db [first-container...]
docker run --network myapp --name webapp [second-container...]
# ... and so on

Now you can refer to containers by their names, from within other containers. Just like they were hostnames in DNS.

In the application running in the webapp container, you can configure the database server using db as if it is a hostname.

Upvotes: 2

Related Questions