skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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.


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

    Reference: multi container

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search