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.
This is helpful when connecting a database container with an app container.
--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.
--link flag takes the form:
--link <name or id>:alias
name is the name of the container we’re linking to and
alias is an alias for the link name. That alias is used shortly. The
--link flag also takes the form:
--link <name or id>
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.
> docker run --link mysql --name="confluence" -d -p 8090:8090 -p 8091:8091
In this example we linked a previous container called mysql with our confluence container. If we connect to the confluence container, we will see that the hosts file has been updated.