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.
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}
.
By default a link will be created for every container with the name set as the automatically generated container id.
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
If we want to view all names for our docker host you can use the new docker ls
command.
docker ls
NAME ID IMAGE
db1 32da9f6caf76 crosbymichael/redis:latest
Using the container named 'db1' from our previous example we will create a relationship between our webapp container and redis.
docker ls
NAME ID IMAGE
db1 32da9f6caf76 crosbymichael/redis:latest
# run a new webapp container and add our redis database as a child
docker run -link db1:db -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
docker ls
NAME CONTAINER IMAGE
db1 32da9f6caf76 crosbymichael/redis:latest
37ewg328232g 37ewg328232g webapp
37ewg328232g/db 32da9f6caf76 crosbymichael/redis:latest
# 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
docker ls
NAME CONTAINER IMAGE
db1 32da9f6caf76 crosbymichael/redis:latest
frontend 37ewg328232g webapp
frontend/db 32da9f6caf76 crosbymichael/redis:latest
You can see the relationship between containers very clearly with the output from docker ls
.
docker ls
NAME CONTAINER IMAGE
db1 32da9f6caf76 crosbymichael/redis:latest
frontend 37ewg328232g webapp
frontend/db 32da9f6caf76 crosbymichael/redis:latest
docker rm frontend/db
docker ls
NAME CONTAINER IMAGE
db1 32da9f6caf76 crosbymichael/redis:latest
frontend 37ewg328232g webapp
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.
REDIS=$(docker run -d -e PASSWORD=dockerpass crosbymichael/redis --requirepass dockerpass)
docker link $REDIS db1
docker run -t -i -link db 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>
Links are a way to express relationships between containers and allow introspection based on those relationships.