Home > Net >  How to connect two Docker conteiners using "hostname" not the Internal IP [duplicate]
How to connect two Docker conteiners using "hostname" not the Internal IP [duplicate]

Time:09-27

I have to Docker containers:

CONTAINER ID   IMAGE             COMMAND                  CREATED             STATUS             PORTS                                       NAMES
a1b6770dcbd4   my-docker-flask   "python ./app.py"        About an hour ago   Up About an hour   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   api
708fbf7aa8e7   postgres          "docker-entrypoint.s…"   23 hours ago        Up 3 hours         0.0.0.0:5432->5432/tcp, :::5432->5432/tcp   mypostgres

And I have the two internal IPs from this Containers:

"Containers": {
    "708fbf7aa8e7a62c78b1c95fda84c857b15b7ade067123cc228e9c82bd0a889e": {
        "Name": "mypostgres",
        "EndpointID": "cdf534c3506b64c6c182c9a9d7ed8de0f9a536e7a540124fe64385db1d77ca65",
        "MacAddress": "02:42:ac:11:00:02",
        "IPv4Address": "172.17.0.2/16",
        "IPv6Address": ""
    },
    "a1b6770dcbd44b2eee5f83afde3f571fe43330b577a2f11d2bbe900d62736920": {
        "Name": "api",
        "EndpointID": "be820c3e651f01935f3549fb40c66120858c7d36879d1341dc33f316419a2645",
        "MacAddress": "02:42:ac:11:00:03",
        "IPv4Address": "172.17.0.3/16",
        "IPv6Address": ""
    }
},

But I would like that my "API" access the Postgres not using the IP "172.0.2.2". Because this IP can change... But I would like to one way to "API" access the "Postgres" by "Name", like hostname. Have some way to do this?

Thanks for your time!

CodePudding user response:

You can add a docker-compose.yml file and put those containers in the same network.

Communication between multiple docker-compose projects

https://docs.docker.com/compose/networking/

  • Related