简体   繁体   中英

How to access docker container by name within a bridge network?

I created a bridge network, so that all my containers are inside of it.

This is info about my docker network (output is truncated for readability):

docker network inspect bridge

"Name": "bridge",
"Containers": {
  "4ae08..d80d": {
    "Name": "zimidy.chat-server.staging",
  },
  "61cdb..1c4b": {
    "Name": "zimidy.web-server.staging",
  },
  "8c45..d391": {
    "Name": "zimidy.neo4j.staging",
  },
  "bcf..093": {
    "Name": "zimidy.api.staging",
  },
  "ca1f..c5aa": {
    "Name": "zimidy.web.staging",
  }
}, 
"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"
}

But I can't access "zimidy.web.staging" from "zimidy.web-server.staging".

I try to access it using the following code:

HttpClient client = HttpClients.createDefault();
HttpGet requestToWebModule = new HttpGet("http://zimidy.web.staging:88/app");
HttpResponse responseFromWebModule = client.execute(requestToWebModule);

Which throws the following error:

java.net.UnknownHostException: zimidy.web.staging: Name or service not known

How to fix such a problem?

"com.docker.network.bridge.default_bridge": "true"

(note: you actually edited your question and removed this piece while I was writing my answer)

From the docker networking documentation

Containers connected to the default bridge network can communicate with each other by IP address. Docker does not support automatic service discovery on the default bridge network. If you want containers to be able to resolve IP addresses by container name, you should use user-defined networks instead.

I understand you said you created a new network, but for whatever reason that network is behaving like the default network, which means you cannot resolve by host name.

The containers on the default bridge network can only access each other by IP addresses , unless you use the --link option🔗 , which is considered legacy.

The better idea is to use user-defined bridge networks🔗 in which from within a container you can ping other containers by their name (this capability is called automatic service discovery.).


[ NOTE ]:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM