Links and Container Naming

## Links - Spec

Links are a way of expressing a relationship between one container and another.  When you link a container, you create a parent/child relationship between the two.  As a side effect links also allow you to name containers and reference them by name.  Containers can be references by more than one name.

#### Breaking changes
This the new link feature we will be disabling intercontainer communication by default.  Containers will not be able to communicate with each other over the docker0 bridge without having a link to that container.  Even with a link, a parent can only communicate to the ports exposed by the child and nothing more.

Initially links will be expressed by injecting the child's data into the parent's environment accessible via namespaced variables:


#### Examples
* `REDIS_ENV_{key}={value}` 
* `REDIS_PORT={value}` 
* `REDIS_PORT_6379_TCP={value}`.


### Rename
By default a link will be created for every container with the name set as the automatically generated container id.

```bash
REDIS=$(docker run -d crosbymichael/redis)
echo $REDIS
32da9f6caf76

docker ps
ID                  IMAGE                        CMD                         EXPOSES        NAMES
32da9f6caf76        crosbymichael/redis:latest   /redis/src/redis-ser        6379/tcp


docker link 32da9f6caf76 db1

docker ps
ID                  IMAGE                        CMD                         EXPOSES        NAMES
32da9f6caf76        crosbymichael/redis:latest   /redis/src/redis-ser        6379/tcp       db1

```

### Basic link

Using the container named 'db1' from our previous example we will create a relationship between our webapp container and redis.

To create a link between two containers you add the `-link` flag to your `docker run` command passing the name and alias of the container that you want to link as the child.  The alias will be used as the namespace to set environment variables in the parent container.  The format is `-link <name>:<alias>`.  The alias will be converted to ALL_CAPS when environment variables are created.  

```bash

# run a new webapp container and add our redis database as a child
docker run -link db1:redis -t -i webapp sh


# access the environment inside our webapp conatiner to collect to redis

BusyBox v1.19.3 (Ubuntu 1:1.19.3-7ubuntu1.1) built-in shell (ash)
Enter 'help' for a list of built-in commands.

/ #

echo $REDIS_PORT_6379_ADDR
tcp://172.17.0.2:6379
./redis-cli -h 172.17.0.2
redis 172.17.0.2:6379>

# back on the host

docker ps
ID                  IMAGE                        CMD                         EXPOSES        NAMES
32da9f6caf76        crosbymichael/redis:latest   /redis/src/redis-ser        6379/tcp       db1
37ewg328232g        webapp                       sh                                         

# List all the links 

# we can see that our redis container is now a child of our webapp

# Give a name to our webapp container
docker link 37ewg328232g frontend

```

### Remove redis as a child of frontend
```bash

docker rm -link frontend/redis

```


### Introspecting the linked container's environment

Lets run another redis container with a password that the client must use to connect.  We store the password in the environment of the container so we can use introspection to retreive the password by the parent.

```bash
REDIS=$(docker run -d -e PASSWORD=dockerpass crosbymichael/redis --requirepass dockerpass)

docker link $REDIS db1

docker run -t -i -link db1:redis webapp sh

# inside the webapp container

BusyBox v1.19.3 (Ubuntu 1:1.19.3-7ubuntu1.1) built-in shell (ash)
Enter 'help' for a list of built-in commands.

echo $db_PORT_6379_ADDR
tcp://172.17.0.2:6379
./redis-cli -h 172.17.0.2 -a $db_ENV_PASSWORD
redis 172.17.0.2:6379> set hello world
OK
redis 172.17.0.2:6379>

```

### Client Libs
With client libs you can simplify links more.

```python
import docker
import time
from redis import Redis
 
proto, ip, port = docker.require('redis')
password = docker.require_env('redis', 'password')
 
r = Redis(host=ip, port=int(port), password=password)
 
print 'Connected to redis...'
 
r.set('name', 'docker')
while True:
        print r.get('name')
        time.sleep(1)
```

### TLDR
Links are a way to express relationships between containers and allow introspection based on those relationships.