Home > Enterprise >  How to connect from docker to database, which is running in the same local network with host machine
How to connect from docker to database, which is running in the same local network with host machine

Time:01-11

I have two servers. First - with docker and certain container. Second - with database. These servers are in one local network. I want to connect to second server's database from container running on first server.

I would like to solve it using docker network, but i have no idea what should i do.

CodePudding user response:

Try this example connecting a DB to another container, like described at documentation

Reference: multi container

CodePudding user response:

Okay, i figured out. It is possible out of the box using network driver "bridge". By default, containers connect to network with driver "bridge", which allow connections from docker container by ip in host's local network.

Example:

First server with postgres in container has 192.168.0.4 local ip. We should use port forwarding to allow connections by 192.168.0.4:5432.

docker run -d -p 5432:5432 -e POSTGRES_PASSWORD=1234 -e POSTGRES_USER=admin -e POSTGRES_DB=mydb postgres:latest

Second server with postgres in container has 192.168.0.3 local ip. From inside this container connection by host local ip - works

psql --username="admin" --host="192.168.0.4" --dbname="mydb"

Removing network bridge from second server's container disallows connections like that.

  •  Tags:  
  • Related