Access one docker container from another - by container name, not IP

Hello,

I’ve two docker containers on the same network (bridge). I’m able to reach an application from one application to another using the IP address of the container, but not able to do it with an alias. What could be wrong? I verified that both containers are on the same bridge network and below is the output of ‘docker network inspect bridge’:

[
{
    "Name": "bridge",
    "Id": "060e2c55d8bbb94982eeb1743898bd4004a865772c93c78129de4a9721a51096",
    "Created": "2019-03-25T06:01:23.455449996-04:00",
    "Scope": "local",
    "Driver": "bridge",
    "EnableIPv6": false,
    "IPAM": {
        "Driver": "default",
        "Options": null,
        "Config": [
            {
                "Subnet": "172.17.0.0/16",
                "Gateway": "172.17.0.1"
            }
        ]
    },
    "Internal": false,
    "Attachable": false,
    "Ingress": false,
    "ConfigFrom": {
        "Network": ""
    },
    "ConfigOnly": false,
    "Containers": {
        "7d9f6a20d29afbc30d3d0b69ee7acfb3a5430904a26c132806a9e496e274a7a8": {
            "Name": "local-broker",
            "EndpointID": "315d2b2bdfd1cbc3e298037d5dd9463532e01c755356b7a2a7466d8ee81c67a7",
            "MacAddress": "02:42:ac:11:00:02",
            "IPv4Address": "172.17.0.7/16",
            "IPv6Address": ""
        },
        "e6da46790a66d4865d7da41a47413dcdfaba34adef7c0b8d1aa695f13d8fe432": {
            "Name": "payment",
            "EndpointID": "055c3bac83516fa6498f5ad3efd7bc7728dbd86d693f5fd96370c83b8bc40256",
            "MacAddress": "02:42:ac:11:00:03",
            "IPv4Address": "172.17.0.8/16",
            "IPv6Address": ""
        }
    },
    "Options": {
        "com.docker.network.bridge.default_bridge": "true",
        "com.docker.network.bridge.enable_icc": "true",
        "com.docker.network.bridge.enable_ip_masquerade": "true",
        "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
        "com.docker.network.bridge.name": "docker0",
        "com.docker.network.driver.mtu": "1500"
    },
    "Labels": {}
}
]

What do I need to do so as I can connect to the container from ‘payment’ container using http://local-broker:8080/xyz as opposed to http://172.17.0.7:8080/xyz ? And how can I dynamically get IP address of the host if I cannot resolve by the container name?

Any help would be appreciated… I tried by best to follow through the docs. Do I need to create another network for it to be able to connect as I cannot use the default ‘bridge’ network?

Thanks!

Looks like my answer is here: https://docs.docker.com/engine/userguide/networking/work-with-networks/#basic-container-networking-example : Basically I cannot use the default bridge network, I need to create a user-defined network.

2 Likes

If these are connected services, then you should consider using docker-compose or Kubernetes services rather than running standalone containers and manually connecting via the same docker network.

as you’ve discovered,

  1. there is no visibility to container names across the network
  2. there is no DNS server on a docker network
  3. YOU have to do all the work. I started up a redis server container and wrote a wrapper script to save container names and ip addresses and make the data inside containers