Overview
Docker supports the ability to link containers. This is similar to having the two containers on the same network. One container can access the other by using the container name or alias.
...
Warning: The --link
flag is a 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.
Linking Containers
The --link
flag takes the form:
...
In this case the alias matches the name.
How it works?
If we look into the container that is linked to another we will see that the /etc/hosts file has been updated with a hostname pointing to the other container.
...
Code Block | ||
---|---|---|
| ||
> docker exec -it confluence bash bash-4.4# cat /etc/hosts 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 10.0.3.4 mysql 4157a451ee8c 10.0.3.2 bac14530534d |
References
Reference | URL |
---|---|
Docker Links | https://docs.docker.com/network/links/ |
...