github.com/a4a881d4/docker@v1.9.0-rc2/docs/userguide/dockerlinks.md (about) 1 <!--[metadata]> 2 +++ 3 title = "Linking containers together" 4 description = "Learn how to connect Docker containers together." 5 keywords = ["Examples, Usage, user guide, links, linking, docker, documentation, examples, names, name, container naming, port, map, network port, network"] 6 [menu.main] 7 parent = "smn_containers" 8 weight = 4 9 +++ 10 <![end-metadata]--> 11 12 # Linking containers together 13 14 In [the Using Docker section](usingdocker.md), you saw how you can 15 connect to a service running inside a Docker container via a network 16 port. But a port connection is only one way you can interact with services and 17 applications running inside Docker containers. In this section, we'll briefly revisit 18 connecting via a network port and then we'll introduce you to another method of access: 19 container linking. 20 21 ## Connect using network port mapping 22 23 In [the Using Docker section](usingdocker.md), you created a 24 container that ran a Python Flask application: 25 26 $ docker run -d -P training/webapp python app.py 27 28 > **Note:** 29 > Containers have an internal network and an IP address 30 > (as we saw when we used the `docker inspect` command to show the container's 31 > IP address in the [Using Docker](usingdocker.md) section). 32 > Docker can have a variety of network configurations. You can see more 33 > information on Docker networking [here](../articles/networking.md). 34 35 When that container was created, the `-P` flag was used to automatically map 36 any network port inside it to a random high port within an *ephemeral port 37 range* on your Docker host. Next, when `docker ps` was run, you saw that port 38 5000 in the container was bound to port 49155 on the host. 39 40 $ docker ps nostalgic_morse 41 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 42 bc533791f3f5 training/webapp:latest python app.py 5 seconds ago Up 2 seconds 0.0.0.0:49155->5000/tcp nostalgic_morse 43 44 You also saw how you can bind a container's ports to a specific port using 45 the `-p` flag. Here port 80 of the host is mapped to port 5000 of the 46 container: 47 48 $ docker run -d -p 80:5000 training/webapp python app.py 49 50 And you saw why this isn't such a great idea because it constrains you to 51 only one container on that specific port. 52 53 Instead, you may specify a range of host ports to bind a container port to 54 that is different than the default *ephemeral port range*: 55 56 $ docker run -d -p 8000-9000:5000 training/webapp python app.py 57 58 This would bind port 5000 in the container to a randomly available port 59 between 8000 and 9000 on the host. 60 61 There are also a few other ways you can configure the `-p` flag. By 62 default the `-p` flag will bind the specified port to all interfaces on 63 the host machine. But you can also specify a binding to a specific 64 interface, for example only to the `localhost`. 65 66 $ docker run -d -p 127.0.0.1:80:5000 training/webapp python app.py 67 68 This would bind port 5000 inside the container to port 80 on the 69 `localhost` or `127.0.0.1` interface on the host machine. 70 71 Or, to bind port 5000 of the container to a dynamic port but only on the 72 `localhost`, you could use: 73 74 $ docker run -d -p 127.0.0.1::5000 training/webapp python app.py 75 76 You can also bind UDP ports by adding a trailing `/udp`. For example: 77 78 $ docker run -d -p 127.0.0.1:80:5000/udp training/webapp python app.py 79 80 You also learned about the useful `docker port` shortcut which showed us the 81 current port bindings. This is also useful for showing you specific port 82 configurations. For example, if you've bound the container port to the 83 `localhost` on the host machine, then the `docker port` output will reflect that. 84 85 $ docker port nostalgic_morse 5000 86 127.0.0.1:49155 87 88 > **Note:** 89 > The `-p` flag can be used multiple times to configure multiple ports. 90 91 ## Connect with the linking system 92 93 Network port mappings are not the only way Docker containers can connect 94 to one another. Docker also has a linking system that allows you to link 95 multiple containers together and send connection information from one to another. 96 When containers are linked, information about a source container can be sent to a 97 recipient container. This allows the recipient to see selected data describing 98 aspects of the source container. 99 100 ### The importance of naming 101 102 To establish links, Docker relies on the names of your containers. 103 You've already seen that each container you create has an automatically 104 created name; indeed you've become familiar with our old friend 105 `nostalgic_morse` during this guide. You can also name containers 106 yourself. This naming provides two useful functions: 107 108 1. It can be useful to name containers that do specific functions in a way 109 that makes it easier for you to remember them, for example naming a 110 container containing a web application `web`. 111 112 2. It provides Docker with a reference point that allows it to refer to other 113 containers, for example, you can specify to link the container `web` to container `db`. 114 115 You can name your container by using the `--name` flag, for example: 116 117 $ docker run -d -P --name web training/webapp python app.py 118 119 This launches a new container and uses the `--name` flag to 120 name the container `web`. You can see the container's name using the 121 `docker ps` command. 122 123 $ docker ps -l 124 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 125 aed84ee21bde training/webapp:latest python app.py 12 hours ago Up 2 seconds 0.0.0.0:49154->5000/tcp web 126 127 You can also use `docker inspect` to return the container's name. 128 129 130 > **Note:** 131 > Container names have to be unique. That means you can only call 132 > one container `web`. If you want to re-use a container name you must delete 133 > the old container (with `docker rm`) before you can create a new 134 > container with the same name. As an alternative you can use the `--rm` 135 > flag with the `docker run` command. This will delete the container 136 > immediately after it is stopped. 137 138 ## Communication across links 139 140 Links allow containers to discover each other and securely transfer information about one 141 container to another container. When you set up a link, you create a conduit between a 142 source container and a recipient container. The recipient can then access select data 143 about the source. To create a link, you use the `--link` flag. First, create a new 144 container, this time one containing a database. 145 146 $ docker run -d --name db training/postgres 147 148 This creates a new container called `db` from the `training/postgres` 149 image, which contains a PostgreSQL database. 150 151 Now, you need to delete the `web` container you created previously so you can replace it 152 with a linked one: 153 154 $ docker rm -f web 155 156 Now, create a new `web` container and link it with your `db` container. 157 158 $ docker run -d -P --name web --link db:db training/webapp python app.py 159 160 This will link the new `web` container with the `db` container you created 161 earlier. The `--link` flag takes the form: 162 163 --link <name or id>:alias 164 165 Where `name` is the name of the container we're linking to and `alias` is an 166 alias for the link name. You'll see how that alias gets used shortly. 167 The `--link` flag also takes the form: 168 169 --link <name or id> 170 171 In which case the alias will match the name. You could have written the previous 172 example as: 173 174 $ docker run -d -P --name web --link db training/webapp python app.py 175 176 Next, inspect your linked containers with `docker inspect`: 177 178 $ docker inspect -f "{{ .HostConfig.Links }}" web 179 [/db:/web/db] 180 181 You can see that the `web` container is now linked to the `db` container 182 `web/db`. Which allows it to access information about the `db` container. 183 184 So what does linking the containers actually do? You've learned that a link allows a 185 source container to provide information about itself to a recipient container. In 186 our example, the recipient, `web`, can access information about the source `db`. To do 187 this, Docker creates a secure tunnel between the containers that doesn't need to 188 expose any ports externally on the container; you'll note when we started the 189 `db` container we did not use either the `-P` or `-p` flags. That's a big benefit of 190 linking: we don't need to expose the source container, here the PostgreSQL database, to 191 the network. 192 193 Docker exposes connectivity information for the source container to the 194 recipient container in two ways: 195 196 * Environment variables, 197 * Updating the `/etc/hosts` file. 198 199 ### Environment variables 200 201 Docker creates several environment variables when you link containers. Docker 202 automatically creates environment variables in the target container based on 203 the `--link` parameters. It will also expose all environment variables 204 originating from Docker from the source container. These include variables from: 205 206 * the `ENV` commands in the source container's Dockerfile 207 * the `-e`, `--env` and `--env-file` options on the `docker run` 208 command when the source container is started 209 210 These environment variables enable programmatic discovery from within the 211 target container of information related to the source container. 212 213 > **Warning**: 214 > It is important to understand that *all* environment variables originating 215 > from Docker within a container are made available to *any* container 216 > that links to it. This could have serious security implications if sensitive 217 > data is stored in them. 218 219 Docker sets an `<alias>_NAME` environment variable for each target container 220 listed in the `--link` parameter. For example, if a new container called 221 `web` is linked to a database container called `db` via `--link db:webdb`, 222 then Docker creates a `WEBDB_NAME=/web/webdb` variable in the `web` container. 223 224 Docker also defines a set of environment variables for each port exposed by the 225 source container. Each variable has a unique prefix in the form: 226 227 `<name>_PORT_<port>_<protocol>` 228 229 The components in this prefix are: 230 231 * the alias `<name>` specified in the `--link` parameter (for example, `webdb`) 232 * the `<port>` number exposed 233 * a `<protocol>` which is either TCP or UDP 234 235 Docker uses this prefix format to define three distinct environment variables: 236 237 * The `prefix_ADDR` variable contains the IP Address from the URL, for 238 example `WEBDB_PORT_5432_TCP_ADDR=172.17.0.82`. 239 * The `prefix_PORT` variable contains just the port number from the URL for 240 example `WEBDB_PORT_5432_TCP_PORT=5432`. 241 * The `prefix_PROTO` variable contains just the protocol from the URL for 242 example `WEBDB_PORT_5432_TCP_PROTO=tcp`. 243 244 If the container exposes multiple ports, an environment variable set is 245 defined for each one. This means, for example, if a container exposes 4 ports 246 that Docker creates 12 environment variables, 3 for each port. 247 248 Additionally, Docker creates an environment variable called `<alias>_PORT`. 249 This variable contains the URL of the source container's first exposed port. 250 The 'first' port is defined as the exposed port with the lowest number. 251 For example, consider the `WEBDB_PORT=tcp://172.17.0.82:5432` variable. If 252 that port is used for both tcp and udp, then the tcp one is specified. 253 254 Finally, Docker also exposes each Docker originated environment variable 255 from the source container as an environment variable in the target. For each 256 variable Docker creates an `<alias>_ENV_<name>` variable in the target 257 container. The variable's value is set to the value Docker used when it 258 started the source container. 259 260 Returning back to our database example, you can run the `env` 261 command to list the specified container's environment variables. 262 263 ``` 264 $ docker run --rm --name web2 --link db:db training/webapp env 265 . . . 266 DB_NAME=/web2/db 267 DB_PORT=tcp://172.17.0.5:5432 268 DB_PORT_5432_TCP=tcp://172.17.0.5:5432 269 DB_PORT_5432_TCP_PROTO=tcp 270 DB_PORT_5432_TCP_PORT=5432 271 DB_PORT_5432_TCP_ADDR=172.17.0.5 272 . . . 273 ``` 274 275 You can see that Docker has created a series of environment variables with 276 useful information about the source `db` container. Each variable is prefixed 277 with 278 `DB_`, which is populated from the `alias` you specified above. If the `alias` 279 were `db1`, the variables would be prefixed with `DB1_`. You can use these 280 environment variables to configure your applications to connect to the database 281 on the `db` container. The connection will be secure and private; only the 282 linked `web` container will be able to talk to the `db` container. 283 284 ### Important notes on Docker environment variables 285 286 Unlike host entries in the [`/etc/hosts` file](#updating-the-etchosts-file), 287 IP addresses stored in the environment variables are not automatically updated 288 if the source container is restarted. We recommend using the host entries in 289 `/etc/hosts` to resolve the IP address of linked containers. 290 291 These environment variables are only set for the first process in the 292 container. Some daemons, such as `sshd`, will scrub them when spawning shells 293 for connection. 294 295 ### Updating the `/etc/hosts` file 296 297 In addition to the environment variables, Docker adds a host entry for the 298 source container to the `/etc/hosts` file. Here's an entry for the `web` 299 container: 300 301 $ docker run -t -i --rm --link db:webdb training/webapp /bin/bash 302 root@aed84ee21bde:/opt/webapp# cat /etc/hosts 303 172.17.0.7 aed84ee21bde 304 . . . 305 172.17.0.5 webdb 6e5cdeb2d300 db 306 307 You can see two relevant host entries. The first is an entry for the `web` 308 container that uses the Container ID as a host name. The second entry uses the 309 link alias to reference the IP address of the `db` container. In addition to 310 the alias you provide, the linked container's name--if unique from the alias 311 provided to the `--link` parameter--and the linked container's hostname will 312 also be added in `/etc/hosts` for the linked container's IP address. You can ping 313 that host now via any of these entries: 314 315 root@aed84ee21bde:/opt/webapp# apt-get install -yqq inetutils-ping 316 root@aed84ee21bde:/opt/webapp# ping webdb 317 PING webdb (172.17.0.5): 48 data bytes 318 56 bytes from 172.17.0.5: icmp_seq=0 ttl=64 time=0.267 ms 319 56 bytes from 172.17.0.5: icmp_seq=1 ttl=64 time=0.250 ms 320 56 bytes from 172.17.0.5: icmp_seq=2 ttl=64 time=0.256 ms 321 322 > **Note:** 323 > In the example, you'll note you had to install `ping` because it was not included 324 > in the container initially. 325 326 Here, you used the `ping` command to ping the `db` container using its host entry, 327 which resolves to `172.17.0.5`. You can use this host entry to configure an application 328 to make use of your `db` container. 329 330 > **Note:** 331 > You can link multiple recipient containers to a single source. For 332 > example, you could have multiple (differently named) web containers attached to your 333 >`db` container. 334 335 If you restart the source container, the linked containers `/etc/hosts` files 336 will be automatically updated with the source container's new IP address, 337 allowing linked communication to continue. 338 339 $ docker restart db 340 db 341 $ docker run -t -i --rm --link db:db training/webapp /bin/bash 342 root@aed84ee21bde:/opt/webapp# cat /etc/hosts 343 172.17.0.7 aed84ee21bde 344 . . . 345 172.17.0.9 db 346 347 # Next step 348 349 Now that you know how to link Docker containers together, the next step is 350 learning how to take complete control over docker networking. 351 352 Go to [Docker Networking](dockernetworks.md). 353