2014-05-21 21:05:19 +00:00
|
|
|
page_title: Linking Containers Together
|
|
|
|
page_description: Learn how to connect Docker containers together.
|
|
|
|
page_keywords: Examples, Usage, user guide, links, linking, docker, documentation, examples, names, name, container naming, port, map, network port, network
|
|
|
|
|
|
|
|
# Linking Containers Together
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
In [the Using Docker section](/userguide/usingdocker), you saw how you can
|
|
|
|
connect to a service running inside a Docker container via a network
|
|
|
|
port. But a port connection is only one way you can interact with services and
|
|
|
|
applications running inside Docker containers. In this section, we'll briefly revisit
|
|
|
|
connecting via a network port and then we'll introduce you to another method of access:
|
|
|
|
container linking.
|
2014-05-21 21:05:19 +00:00
|
|
|
|
|
|
|
## Network port mapping refresher
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
In [the Using Docker section](/userguide/usingdocker), you created a
|
|
|
|
container that ran a Python Flask application:
|
2014-05-21 21:05:19 +00:00
|
|
|
|
|
|
|
$ sudo docker run -d -P training/webapp python app.py
|
|
|
|
|
|
|
|
> **Note:**
|
|
|
|
> Containers have an internal network and an IP address
|
2014-07-25 23:22:41 +00:00
|
|
|
> (as we saw when we used the `docker inspect` command to show the container's
|
2014-05-21 21:05:19 +00:00
|
|
|
> IP address in the [Using Docker](/userguide/usingdocker/) section).
|
|
|
|
> Docker can have a variety of network configurations. You can see more
|
|
|
|
> information on Docker networking [here](/articles/networking/).
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
When that container was created, the `-P` flag was used to automatically map any
|
2014-09-11 19:49:07 +00:00
|
|
|
network ports inside it to a random high port from the range 49153
|
|
|
|
to 65535 on our Docker host. Next, when `docker ps` was run, you saw that
|
2014-07-25 23:22:41 +00:00
|
|
|
port 5000 in the container was bound to port 49155 on the host.
|
2014-05-21 21:05:19 +00:00
|
|
|
|
|
|
|
$ sudo docker ps nostalgic_morse
|
|
|
|
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
|
|
|
bc533791f3f5 training/webapp:latest python app.py 5 seconds ago Up 2 seconds 0.0.0.0:49155->5000/tcp nostalgic_morse
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
You also saw how you can bind a container's ports to a specific port using
|
|
|
|
the `-p` flag:
|
2014-05-21 21:05:19 +00:00
|
|
|
|
|
|
|
$ sudo docker run -d -p 5000:5000 training/webapp python app.py
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
And you saw why this isn't such a great idea because it constrains you to
|
2014-05-21 21:05:19 +00:00
|
|
|
only one container on that specific port.
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
There are also a few other ways you can configure the `-p` flag. By
|
2014-05-21 21:05:19 +00:00
|
|
|
default the `-p` flag will bind the specified port to all interfaces on
|
2014-07-25 23:22:41 +00:00
|
|
|
the host machine. But you can also specify a binding to a specific
|
2014-05-21 21:05:19 +00:00
|
|
|
interface, for example only to the `localhost`.
|
|
|
|
|
|
|
|
$ sudo docker run -d -p 127.0.0.1:5000:5000 training/webapp python app.py
|
|
|
|
|
|
|
|
This would bind port 5000 inside the container to port 5000 on the
|
|
|
|
`localhost` or `127.0.0.1` interface on the host machine.
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
Or, to bind port 5000 of the container to a dynamic port but only on the
|
|
|
|
`localhost`, you could use:
|
2014-05-21 21:05:19 +00:00
|
|
|
|
|
|
|
$ sudo docker run -d -p 127.0.0.1::5000 training/webapp python app.py
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
You can also bind UDP ports by adding a trailing `/udp`. For example:
|
2014-05-21 21:05:19 +00:00
|
|
|
|
|
|
|
$ sudo docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
You also learned about the useful `docker port` shortcut which showed us the
|
|
|
|
current port bindings. This is also useful for showing you specific port
|
|
|
|
configurations. For example, if you've bound the container port to the
|
|
|
|
`localhost` on the host machine, then the `docker port` output will reflect that.
|
2014-05-21 21:05:19 +00:00
|
|
|
|
2014-09-16 01:49:05 +00:00
|
|
|
$ sudo docker port nostalgic_morse 5000
|
2014-05-21 21:05:19 +00:00
|
|
|
127.0.0.1:49155
|
|
|
|
|
|
|
|
> **Note:**
|
|
|
|
> The `-p` flag can be used multiple times to configure multiple ports.
|
|
|
|
|
|
|
|
## Docker Container Linking
|
|
|
|
|
|
|
|
Network port mappings are not the only way Docker containers can connect
|
|
|
|
to one another. Docker also has a linking system that allows you to link
|
2014-07-25 23:22:41 +00:00
|
|
|
multiple containers together and send connection information from one to another.
|
|
|
|
When containers are linked, information about a source container can be sent to a
|
|
|
|
recipient container. This allows the recipient to see selected data describing
|
|
|
|
aspects of the source container.
|
2014-05-21 21:05:19 +00:00
|
|
|
|
|
|
|
## Container naming
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
To establish links, Docker relies on the names of your containers.
|
|
|
|
You've already seen that each container you create has an automatically
|
|
|
|
created name; indeed you've become familiar with our old friend
|
2014-05-21 21:05:19 +00:00
|
|
|
`nostalgic_morse` during this guide. You can also name containers
|
|
|
|
yourself. This naming provides two useful functions:
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
1. It can be useful to name containers that do specific functions in a way
|
2014-05-21 21:05:19 +00:00
|
|
|
that makes it easier for you to remember them, for example naming a
|
2014-07-25 23:22:41 +00:00
|
|
|
container containing a web application `web`.
|
2014-05-21 21:05:19 +00:00
|
|
|
|
2014-06-28 16:26:36 +00:00
|
|
|
2. It provides Docker with a reference point that allows it to refer to other
|
2014-07-25 23:22:41 +00:00
|
|
|
containers, for example, you can specify to link the container `web` to container `db`.
|
2014-05-21 21:05:19 +00:00
|
|
|
|
|
|
|
You can name your container by using the `--name` flag, for example:
|
|
|
|
|
|
|
|
$ sudo docker run -d -P --name web training/webapp python app.py
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
This launches a new container and uses the `--name` flag to
|
|
|
|
name the container `web`. You can see the container's name using the
|
2014-05-21 21:05:19 +00:00
|
|
|
`docker ps` command.
|
|
|
|
|
|
|
|
$ sudo docker ps -l
|
|
|
|
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
|
|
|
aed84ee21bde training/webapp:latest python app.py 12 hours ago Up 2 seconds 0.0.0.0:49154->5000/tcp web
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
You can also use `docker inspect` to return the container's name.
|
2014-05-21 21:05:19 +00:00
|
|
|
|
|
|
|
$ sudo docker inspect -f "{{ .Name }}" aed84ee21bde
|
|
|
|
/web
|
|
|
|
|
|
|
|
> **Note:**
|
|
|
|
> Container names have to be unique. That means you can only call
|
2014-07-07 00:28:53 +00:00
|
|
|
> one container `web`. If you want to re-use a container name you must delete
|
2014-07-25 23:22:41 +00:00
|
|
|
> the old container (with `docker rm`) before you can create a new
|
2014-05-21 21:05:19 +00:00
|
|
|
> container with the same name. As an alternative you can use the `--rm`
|
|
|
|
> flag with the `docker run` command. This will delete the container
|
2014-07-25 23:22:41 +00:00
|
|
|
> immediately after it is stopped.
|
2014-05-21 21:05:19 +00:00
|
|
|
|
|
|
|
## Container Linking
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
Links allow containers to discover each other and securely transfer information about one
|
|
|
|
container to another container. 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. To create a link, you use the `--link` flag. First, create a new
|
|
|
|
container, this time one containing a database.
|
2014-05-21 21:05:19 +00:00
|
|
|
|
|
|
|
$ sudo docker run -d --name db training/postgres
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
This creates a new container called `db` from the `training/postgres`
|
2014-05-21 21:05:19 +00:00
|
|
|
image, which contains a PostgreSQL database.
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
Now, you need to delete the `web` container you created previously so you can replace it
|
2014-07-07 00:28:53 +00:00
|
|
|
with a linked one:
|
|
|
|
|
2014-09-16 01:49:05 +00:00
|
|
|
$ sudo docker rm -f web
|
2014-07-07 00:28:53 +00:00
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
Now, create a new `web` container and link it with your `db` container.
|
2014-05-21 21:05:19 +00:00
|
|
|
|
|
|
|
$ sudo docker run -d -P --name web --link db:db training/webapp python app.py
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
This will link the new `web` container with the `db` container you created
|
2014-05-21 21:05:19 +00:00
|
|
|
earlier. The `--link` flag takes the form:
|
|
|
|
|
|
|
|
--link name:alias
|
|
|
|
|
|
|
|
Where `name` is the name of the container we're linking to and `alias` is an
|
2014-07-25 23:22:41 +00:00
|
|
|
alias for the link name. You'll see how that alias gets used shortly.
|
2014-05-21 21:05:19 +00:00
|
|
|
|
2014-10-10 06:12:12 +00:00
|
|
|
Next, look at the names of your linked containers by filtering the full output of
|
|
|
|
`docker ps` to the last column (NAMES) using `docker ps --no-trunc | awk '{print $NF}'`.
|
2014-05-21 21:05:19 +00:00
|
|
|
|
2014-10-10 06:12:12 +00:00
|
|
|
$ sudo docker ps --no-trunc | awk '{print $NF}'
|
|
|
|
NAMES
|
|
|
|
db, web/db
|
|
|
|
web
|
2014-05-21 21:05:19 +00:00
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
You can see your named containers, `db` and `web`, and you can see that the `db`
|
|
|
|
container also shows `web/db` in the `NAMES` column. This tells you that the
|
|
|
|
`web` container is linked to the `db` container, which allows it to access information
|
|
|
|
about the `db` container.
|
2014-05-21 21:05:19 +00:00
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
So what does linking the containers actually do? You've learned that a link creates a
|
|
|
|
source container that can provide information about itself to a recipient container. In
|
|
|
|
our example, the recipient, `web`, can access information about the source `db`. To do
|
|
|
|
this, Docker creates a secure tunnel between the containers that doesn't need to
|
|
|
|
expose any ports externally on the container; you'll note when we started the
|
|
|
|
`db` container we did not use either the `-P` or `-p` flags. That's a big benefit of
|
|
|
|
linking: we don't need to expose the source container, here the PostgreSQL database, to
|
|
|
|
the network.
|
2014-05-21 21:05:19 +00:00
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
Docker exposes connectivity information for the source container to the
|
|
|
|
recipient container in two ways:
|
2014-05-21 21:05:19 +00:00
|
|
|
|
|
|
|
* Environment variables,
|
2014-06-25 07:59:10 +00:00
|
|
|
* Updating the `/etc/hosts` file.
|
2014-05-21 21:05:19 +00:00
|
|
|
|
2014-10-11 16:15:54 +00:00
|
|
|
### Environment Variables
|
|
|
|
|
|
|
|
When two containers are linked, Docker will set some envrionment variables
|
|
|
|
in the target container to enable programmatic discovery of information
|
|
|
|
related to the source container.
|
|
|
|
|
|
|
|
First, Docker will set a `<alias>_NAME` environment variable specifying the
|
|
|
|
alias of each target container that was given in a `--link` parameter. So,
|
|
|
|
for example, if a new container called `web` is being linked to a database
|
|
|
|
container called `db` via `--link db:webdb` then in the `web` container
|
|
|
|
would be `WEBDB_NAME=/web/webdb`.
|
|
|
|
|
|
|
|
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 convinience:
|
|
|
|
* `<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`).
|
|
|
|
|
|
|
|
If there are multiple ports exposed then the above set of environment
|
|
|
|
variables will be defined for each one.
|
|
|
|
|
|
|
|
Finally, there will be an environment variable called `<alias>_PORT` that will
|
|
|
|
contain the URL of the first exposed port of the source container.
|
|
|
|
For example, `WEBDB_PORT=tcp://172.17.0.82:8080`. In this case, 'first'
|
|
|
|
is defined as the lowest numbered port that is exposed. If that port is
|
|
|
|
used for both tcp and udp, then the tcp one will be specified.
|
|
|
|
|
|
|
|
Returning back to our database example, you can run the `env`
|
2014-07-25 23:22:41 +00:00
|
|
|
command to list the specified container's environment variables.
|
2014-06-20 11:49:22 +00:00
|
|
|
|
2014-06-25 07:59:10 +00:00
|
|
|
```
|
2014-06-19 09:45:31 +00:00
|
|
|
$ sudo docker run --rm --name web2 --link db:db training/webapp env
|
2014-05-21 21:05:19 +00:00
|
|
|
. . .
|
2014-06-12 16:26:55 +00:00
|
|
|
DB_NAME=/web2/db
|
2014-05-21 21:05:19 +00:00
|
|
|
DB_PORT=tcp://172.17.0.5:5432
|
2014-09-12 10:04:40 +00:00
|
|
|
DB_PORT_5432_TCP=tcp://172.17.0.5:5432
|
|
|
|
DB_PORT_5432_TCP_PROTO=tcp
|
|
|
|
DB_PORT_5432_TCP_PORT=5432
|
|
|
|
DB_PORT_5432_TCP_ADDR=172.17.0.5
|
2014-05-21 21:05:19 +00:00
|
|
|
. . .
|
2014-06-25 07:59:10 +00:00
|
|
|
```
|
2014-05-21 21:05:19 +00:00
|
|
|
|
|
|
|
> **Note**:
|
|
|
|
> These Environment variables are only set for the first process in the
|
|
|
|
> container. Similarly, some daemons (such as `sshd`)
|
|
|
|
> will scrub them when spawning shells for connection.
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
You can see that Docker has created a series of environment variables with
|
|
|
|
useful information about the source `db` container. Each variable is prefixed with
|
|
|
|
`DB_`, which is populated from the `alias` you specified above. If the `alias`
|
|
|
|
were `db1`, the variables would be prefixed with `DB1_`. You can use these
|
2014-05-21 21:05:19 +00:00
|
|
|
environment variables to configure your applications to connect to the database
|
2014-07-25 23:22:41 +00:00
|
|
|
on the `db` container. The connection will be secure and private; only the
|
2014-05-21 21:05:19 +00:00
|
|
|
linked `web` container will be able to talk to the `db` container.
|
|
|
|
|
2014-10-11 16:15:54 +00:00
|
|
|
### Updating the `/etc/hosts` file
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
In addition to the environment variables, Docker adds a host entry for the
|
|
|
|
source container to the `/etc/hosts` file. Here's an entry for the `web`
|
|
|
|
container:
|
2014-05-21 21:05:19 +00:00
|
|
|
|
2014-07-19 04:38:11 +00:00
|
|
|
$ sudo docker run -t -i --rm --link db:db training/webapp /bin/bash
|
2014-05-21 21:05:19 +00:00
|
|
|
root@aed84ee21bde:/opt/webapp# cat /etc/hosts
|
|
|
|
172.17.0.7 aed84ee21bde
|
|
|
|
. . .
|
|
|
|
172.17.0.5 db
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
You can see two relevant host entries. The first is an entry for the `web`
|
2014-05-21 21:05:19 +00:00
|
|
|
container that uses the Container ID as a host name. The second entry uses the
|
2014-07-25 23:22:41 +00:00
|
|
|
link alias to reference the IP address of the `db` container. You can ping
|
2014-05-21 21:05:19 +00:00
|
|
|
that host now via this host name.
|
|
|
|
|
|
|
|
root@aed84ee21bde:/opt/webapp# apt-get install -yqq inetutils-ping
|
|
|
|
root@aed84ee21bde:/opt/webapp# ping db
|
|
|
|
PING db (172.17.0.5): 48 data bytes
|
|
|
|
56 bytes from 172.17.0.5: icmp_seq=0 ttl=64 time=0.267 ms
|
|
|
|
56 bytes from 172.17.0.5: icmp_seq=1 ttl=64 time=0.250 ms
|
|
|
|
56 bytes from 172.17.0.5: icmp_seq=2 ttl=64 time=0.256 ms
|
|
|
|
|
|
|
|
> **Note:**
|
2014-07-25 23:22:41 +00:00
|
|
|
> In the example, you'll note you had to install `ping` because it was not included
|
|
|
|
> in the container initially.
|
2014-05-21 21:05:19 +00:00
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
Here, you used the `ping` command to ping the `db` container using its host entry,
|
|
|
|
which resolves to `172.17.0.5`. You can use this host entry to configure an application
|
|
|
|
to make use of your `db` container.
|
2014-05-21 21:05:19 +00:00
|
|
|
|
|
|
|
> **Note:**
|
2014-07-25 23:22:41 +00:00
|
|
|
> You can link multiple recipient containers to a single source. For
|
|
|
|
> example, you could have multiple (differently named) web containers attached to your
|
|
|
|
>`db` container.
|
2014-05-21 21:05:19 +00:00
|
|
|
|
2014-07-14 23:19:37 +00:00
|
|
|
If you restart the source container, the linked containers `/etc/hosts` files
|
|
|
|
will be automatically updated with the source container's new IP address,
|
|
|
|
allowing linked communication to continue.
|
|
|
|
|
|
|
|
$ sudo docker restart db
|
|
|
|
root@aed84ee21bde:/opt/webapp# cat /etc/hosts
|
|
|
|
172.17.0.7 aed84ee21bde
|
|
|
|
. . .
|
|
|
|
172.17.0.9 db
|
|
|
|
|
2014-05-21 21:05:19 +00:00
|
|
|
# Next step
|
|
|
|
|
2014-07-25 23:22:41 +00:00
|
|
|
Now that you know how to link Docker containers together, the next step is
|
|
|
|
learning how to manage data, volumes and mounts inside your containers.
|
2014-05-21 21:05:19 +00:00
|
|
|
|
|
|
|
Go to [Managing Data in Containers](/userguide/dockervolumes).
|
|
|
|
|