Closes #5169
Signed-off-by: Doug Davis <dug@us.ibm.com>
| ... | ... |
@@ -11,7 +11,7 @@ applications running inside Docker containers. In this section, we'll briefly re |
| 11 | 11 |
connecting via a network port and then we'll introduce you to another method of access: |
| 12 | 12 |
container linking. |
| 13 | 13 |
|
| 14 |
-## Network port mapping refresher |
|
| 14 |
+## Connect using Network port mapping |
|
| 15 | 15 |
|
| 16 | 16 |
In [the Using Docker section](/userguide/usingdocker), you created a |
| 17 | 17 |
container that ran a Python Flask application: |
| ... | ... |
@@ -72,7 +72,7 @@ configurations. For example, if you've bound the container port to the |
| 72 | 72 |
> **Note:** |
| 73 | 73 |
> The `-p` flag can be used multiple times to configure multiple ports. |
| 74 | 74 |
|
| 75 |
-## Docker Container Linking |
|
| 75 |
+## Connect with the linking system |
|
| 76 | 76 |
|
| 77 | 77 |
Network port mappings are not the only way Docker containers can connect |
| 78 | 78 |
to one another. Docker also has a linking system that allows you to link |
| ... | ... |
@@ -81,7 +81,7 @@ When containers are linked, information about a source container can be sent to |
| 81 | 81 |
recipient container. This allows the recipient to see selected data describing |
| 82 | 82 |
aspects of the source container. |
| 83 | 83 |
|
| 84 |
-## Container naming |
|
| 84 |
+### The importance of naming |
|
| 85 | 85 |
|
| 86 | 86 |
To establish links, Docker relies on the names of your containers. |
| 87 | 87 |
You've already seen that each container you create has an automatically |
| ... | ... |
@@ -121,7 +121,7 @@ You can also use `docker inspect` to return the container's name. |
| 121 | 121 |
> flag with the `docker run` command. This will delete the container |
| 122 | 122 |
> immediately after it is stopped. |
| 123 | 123 |
|
| 124 |
-## Container Linking |
|
| 124 |
+## Communication across links |
|
| 125 | 125 |
|
| 126 | 126 |
Links allow containers to discover each other and securely transfer information about one |
| 127 | 127 |
container to another container. When you set up a link, you create a conduit between a |
| ... | ... |
@@ -176,41 +176,64 @@ recipient container in two ways: |
| 176 | 176 |
|
| 177 | 177 |
### Environment Variables |
| 178 | 178 |
|
| 179 |
-When two containers are linked, Docker will set some environment variables |
|
| 180 |
-in the target container to enable programmatic discovery of information |
|
| 181 |
-related to the source container. |
|
| 182 |
- |
|
| 183 |
-First, Docker will set an `<alias>_NAME` environment variable specifying the |
|
| 184 |
-alias of each target container that was given in a `--link` parameter. So, |
|
| 185 |
-for example, if a new container called `web` is being linked to a database |
|
| 186 |
-container called `db` via `--link db:webdb` then in the `web` container |
|
| 187 |
-would be `WEBDB_NAME=/web/webdb`. |
|
| 188 |
- |
|
| 189 |
-Docker will then also define a set of environment variables for each |
|
| 190 |
-port that is exposed by the source container. The pattern followed is: |
|
| 191 |
- |
|
| 192 |
-* `<name>_PORT_<port>_<protocol>` will contain a URL reference to the |
|
| 193 |
-port. Where `<name>` is the alias name specified in the `--link` parameter |
|
| 194 |
-(e.g. `webdb`), `<port>` is the port number being exposed, and `<protocol>` |
|
| 195 |
-is either `TCP` or `UDP`. The format of the URL will be: |
|
| 196 |
-`<protocol>://<container_ip_address>:<port>` |
|
| 197 |
-(e.g. `tcp://172.17.0.82:8080`). This URL will then be |
|
| 198 |
-split into the following 3 environment variables for convenience: |
|
| 199 |
-* `<name>_PORT_<port>_<protocol>_ADDR` will contain just the IP address |
|
| 200 |
-from the URL (e.g. `WEBDB_PORT_8080_TCP_ADDR=172.17.0.82`). |
|
| 201 |
-* `<name>_PORT_<port>_<protocol>_PORT` will contain just the port number |
|
| 202 |
-from the URL (e.g. `WEBDB_PORT_8080_TCP_PORT=8080`). |
|
| 203 |
-* `<name>_PORT_<port>_<protocol>_PROTO` will contain just the protocol |
|
| 204 |
-from the URL (e.g. `WEBDB_PORT_8080_TCP_PROTO=tcp`). |
|
| 205 |
- |
|
| 206 |
-If there are multiple ports exposed then the above set of environment |
|
| 207 |
-variables will be defined for each one. |
|
| 208 |
- |
|
| 209 |
-Finally, there will be an environment variable called `<alias>_PORT` that will |
|
| 210 |
-contain the URL of the first exposed port of the source container. |
|
| 211 |
-For example, `WEBDB_PORT=tcp://172.17.0.82:8080`. In this case, 'first' |
|
| 212 |
-is defined as the lowest numbered port that is exposed. If that port is |
|
| 213 |
-used for both tcp and udp, then the tcp one will be specified. |
|
| 179 |
+Docker creates several environment variables when you link containers. Docker |
|
| 180 |
+automatically creates environment variables in the target container based on |
|
| 181 |
+the `--link` parameters. It will also expose all environment variables |
|
| 182 |
+originating from Docker from the source container. These include variables from: |
|
| 183 |
+ |
|
| 184 |
+* the `ENV` commands in the source container's Dockerfile |
|
| 185 |
+* the `-e`, `--env` and `--env-file` options on the `docker run` |
|
| 186 |
+command when the source container is started |
|
| 187 |
+ |
|
| 188 |
+These environment variables enable programmatic discovery from within the |
|
| 189 |
+target container of information related to the source container. |
|
| 190 |
+ |
|
| 191 |
+> **Warning**: |
|
| 192 |
+> It is important to understand that *all* environment variables originating |
|
| 193 |
+> from Docker within a container are made available to *any* container |
|
| 194 |
+> that links to it. This could have serious security implications if sensitive |
|
| 195 |
+> data is stored in them. |
|
| 196 |
+ |
|
| 197 |
+Docker sets an `<alias>_NAME` environment variable for each target container |
|
| 198 |
+listed in the `--link` parameter. For example, if a new container called |
|
| 199 |
+`web` is linked to a database container called `db` via `--link db:webdb`, |
|
| 200 |
+then Docker creates a `WEBDB_NAME=/web/webdb` variable in the `web` container. |
|
| 201 |
+ |
|
| 202 |
+Docker also defines a set of environment variables for each port exposed by the |
|
| 203 |
+source container. Each variable has a unique prefix in the form: |
|
| 204 |
+ |
|
| 205 |
+`<name>_PORT_<port>_<protocol>` |
|
| 206 |
+ |
|
| 207 |
+The components in this prefix are: |
|
| 208 |
+ |
|
| 209 |
+* the alias `<name>` specified in the `--link` parameter (for example, `webdb`) |
|
| 210 |
+* the `<port>` number exposed |
|
| 211 |
+* a `<protocol>` which is either TCP or UDP |
|
| 212 |
+ |
|
| 213 |
+Docker uses this prefix format to define three distinct environment variables: |
|
| 214 |
+ |
|
| 215 |
+* The `prefix_ADDR` variable contains the IP Address from the URL, for |
|
| 216 |
+example `WEBDB_PORT_8080_TCP_ADDR=172.17.0.82`. |
|
| 217 |
+* The `prefix_PORT` variable contains just the port number from the URL for |
|
| 218 |
+example `WEBDB_PORT_8080_TCP_PORT=8080`. |
|
| 219 |
+* The `prefix_PROTO` variable contains just the protocol from the URL for |
|
| 220 |
+example `WEBDB_PORT_8080_TCP_PROTO=tcp`. |
|
| 221 |
+ |
|
| 222 |
+If the container exposes multiple ports, an environment variable set is |
|
| 223 |
+defined for each one. This means, for example, if a container exposes 4 ports |
|
| 224 |
+that Docker creates 12 environment variables, 3 for each port. |
|
| 225 |
+ |
|
| 226 |
+Additionally, Docker creates an environment variable called `<alias>_PORT`. |
|
| 227 |
+This variable contains the URL of the source container's first exposed port. |
|
| 228 |
+The 'first' port is defined as the exposed port with the lowest number. |
|
| 229 |
+For example, consider the `WEBDB_PORT=tcp://172.17.0.82:8080` variable. If |
|
| 230 |
+that port is used for both tcp and udp, then the tcp one is specified. |
|
| 231 |
+ |
|
| 232 |
+Finally, Docker also exposes each Docker originated environment variable |
|
| 233 |
+from the source container as an environment variable in the target. For each |
|
| 234 |
+variable Docker creates an `<alias>_ENV_<name>` variable in the target |
|
| 235 |
+container. The variable's value is set to the value Docker used when it |
|
| 236 |
+started the source container. |
|
| 214 | 237 |
|
| 215 | 238 |
Returning back to our database example, you can run the `env` |
| 216 | 239 |
command to list the specified container's environment variables. |
| ... | ... |
@@ -227,25 +250,26 @@ command to list the specified container's environment variables. |
| 227 | 227 |
. . . |
| 228 | 228 |
``` |
| 229 | 229 |
|
| 230 |
-> **Note**: |
|
| 231 |
-> These Environment variables are only set for the first process in the |
|
| 232 |
-> container. Similarly, some daemons (such as `sshd`) |
|
| 233 |
-> will scrub them when spawning shells for connection. |
|
| 234 |
- |
|
| 235 |
-> **Note**: |
|
| 236 |
-> Unlike host entries in the [`/etc/hosts` file](#updating-the-etchosts-file), |
|
| 237 |
-> IP addresses stored in the environment variables are not automatically updated |
|
| 238 |
-> if the source container is restarted. We recommend using the host entries in |
|
| 239 |
-> `/etc/hosts` to resolve the IP address of linked containers. |
|
| 240 |
- |
|
| 241 | 230 |
You can see that Docker has created a series of environment variables with |
| 242 |
-useful information about the source `db` container. Each variable is prefixed with |
|
| 231 |
+useful information about the source `db` container. Each variable is prefixed |
|
| 232 |
+with |
|
| 243 | 233 |
`DB_`, which is populated from the `alias` you specified above. If the `alias` |
| 244 | 234 |
were `db1`, the variables would be prefixed with `DB1_`. You can use these |
| 245 | 235 |
environment variables to configure your applications to connect to the database |
| 246 | 236 |
on the `db` container. The connection will be secure and private; only the |
| 247 | 237 |
linked `web` container will be able to talk to the `db` container. |
| 248 | 238 |
|
| 239 |
+### Important notes on Docker environment variables |
|
| 240 |
+ |
|
| 241 |
+Unlike host entries in the [`/etc/hosts` file](#updating-the-etchosts-file), |
|
| 242 |
+IP addresses stored in the environment variables are not automatically updated |
|
| 243 |
+if the source container is restarted. We recommend using the host entries in |
|
| 244 |
+`/etc/hosts` to resolve the IP address of linked containers. |
|
| 245 |
+ |
|
| 246 |
+These environment variables are only set for the first process in the |
|
| 247 |
+container. Some daemons, such as `sshd`, will scrub them when spawning shells |
|
| 248 |
+for connection. |
|
| 249 |
+ |
|
| 249 | 250 |
### Updating the `/etc/hosts` file |
| 250 | 251 |
|
| 251 | 252 |
In addition to the environment variables, Docker adds a host entry for the |