questions about docker –link parameter
As we know, in one host with docker daemon, containers connect to the
docker0 bridge, and so containers can access each other by default.
Then what’s the use of
--link option? Is it any different with the
direct access by ip way?
What does it actually do?
3 Solutions collect form web for “questions about docker –link parameter”
From the Docker docs:
When you set up a link, you create a conduit between a source container and a recipient container. The recipient can then access select data about the source
When two containers are linked, Docker will set some environment variables in the target container to enable programmatic discovery of information related to the source container.
And some more:
In addition to the environment variables, Docker adds a host entry for the source container to the
/etc/hostsfile. Here’s an entry for the web container:
--link creates a set of environment variables and adds some entries to the
/etc/hosts file in order to ease communication. But, the containers are still directly accessed via IP.
When you create a container using
--link option, Docker exposes the linked container into the new one in two ways:
- It creates a entry in
/etc/hostswith the IP of the linked container and the alias given when creating the link.
- It exposes some information as environmental variables about the linked container. As Docker documentation shows:
Docker will then also define a set of environment variables for each port that is exposed by the source container. The pattern followed is:
<name>_PORT_<port>_<protocol> will contain a URL reference to the port. Where <name> is the alias name specified in the --link parameter (e.g. webdb), <port> is the port number being exposed, and <protocol> is either TCP or UDP. The format of the URL will be: <protocol>://<container_ip_address>:<port> (e.g. tcp://172.17.0.82:8080). This URL will then be split into the following 3 environment variables for convenience: <name>_PORT_<port>_<protocol>_ADDR will contain just the IP address from the URL (e.g. WEBDB_PORT_8080_TCP_ADDR=172.17.0.82). <name>_PORT_<port>_<protocol>_PORT will contain just the port number from the URL (e.g. WEBDB_PORT_8080_TCP_PORT=8080). <name>_PORT_<port>_<protocol>_PROTO will contain just the protocol from the URL (e.g. WEBDB_PORT_8080_TCP_PROTO=tcp).
It is not differences if you access via IP, but using links let setting the container ignoring the ip that will be assigned by Docker Daemon. Check Docker documentation for further information.
If you start the docker using
--icc=false option, the container can’t communicate to each other by default. You must use
--link to connect two containers.