问题
Seems like docker service discovery just works with user defined networks and not with default bridge (docker0), but I didn't find anything in the docs.
docker run --rm -d --name c1 alpine sleep 2h
docker run --rm -d --name c2 alpine sleep 2h
docker exec -ti c1 ping c2
It gives me ping: bad address 'c2'
But if I create a custom bridge network everthing works fine:docker network create u-bridge
docker run --rm -d --name u1 --net u-bridge alpine sleep 2h
docker run --rm -d --name u2 --net u-bridge alpine sleep 2h
docker exec -ti u1 ping u2
It gives me: PING u2 (172.18.0.3): 56 data bytes (...)
Shouldn't default bridge network have service discovery?
回答1:
Containers on the default bridge need to be explicitly linked, which is considered legacy/deprecated behaviour.
Warning: The
--link
flag is a deprecated legacy feature of Docker. It may eventually be removed. Unless you absolutely need to continue using it, we recommend that you use user-defined networks to facilitate communication between two containers instead of using--link
. One feature that user-defined networks do not support that you can do with--link
is sharing environmental variables between containers. However, you can use other mechanisms such as volumes to share environment variables between containers in a more controlled way.
User defined networks should be used instead, as you have demonstrated.
回答2:
DNS lookup for default bridge and user-defined network works differently.
Refer to this: https://docs.docker.com/engine/userguide/networking/configure-dns/
来源:https://stackoverflow.com/questions/46781444/docker-service-discovery-does-not-work-with-default-bridge