All examples I found online when demonstrating how to connect to another container through alias/name create their own network.
# create the network
$ docker network create my-network
# create the containers and connect them to the network
$ docker run --name service-one --network my-network ...
$ docker run --name service-two --network my-network ...
Now within the service-one container we can connect to service-two by its by its container name service-two (instead of localhost)! The same is true from service-two to service-one using the latter’s alias.
I wanted to do the same without creating my own network, using the default bridge
network. Is it possible? If not, why?
I started a container with --net-alias
and --name
on the default bridge
network:
docker run --rm --name=wa -dti -p 80:80 --net-alias=webapp docker/getting-started
Now from inside the other container I try to connect to it through the default bridge
network that both containers are part of:
docker run --rm curlimages/curl https://webapp
curl: (6) Could not resolve host: webapp
docker run --rm curlimages/curl https://wa
curl: (6) Could not resolve host: wa
No luck. But it works perfectly fine when connecting through IP address.
CodePudding user response:
I wanted to do the same without creating my own network, using the default bridge network. Is it possible? If not, why?
Unfortunately, it is not possible quotting from the docker docs: https://docs.docker.com/network/bridge/#differences-between-user-defined-bridges-and-the-default-bridge
User-defined bridges provide automatic DNS resolution between containers.
Containers on the default bridge network can only access each other by IP addresses, unless you use the --link option, which is considered legacy. On a user-defined bridge network, containers can resolve each other by name or alias.
Also from: https://docs.docker.com/config/containers/container-networking/#dns-services
DNS Services
By default, a container inherits the DNS settings of the host, as defined in the /etc/resolv.conf configuration file. Containers that use the default bridge network get a copy of this file, whereas containers that use a custom network use Docker’s embedded DNS server