Links are a way of expressing a relationship between one container and another. When you link a container, you create a parent/child rlationship between the two. As a side effect links also allow you to name containers and reference them by name. Containers can be referenced by more than one name.
Initially links will be expressed with the environments of the children being injected into the parent accessible via namespaced environment variables:
####Examples
REDIS_ENV_{key}={value}
REDIS_PORT={value}
REDIS_PORT_6379_TCP={value}
.REDIS_CHILD_{name}_{keys....}={value}
By default, every container has a name. The name will automatically be set to the ID of the container.
REDIS=$(docker run -d crosbymichael/redis)
echo $REDIS
32da9f6caf76
docker ps
NAME ID IMAGE CMD EXPOSES
32da9f6caf76 32da9f6caf76 crosbymichael/redis:latest /redis/src/redis-ser 6379/tcp
# Perform a rename to rename 32da9f6caf76 to 'redis'
docker rename 32da9f6caf76 redis
docker ps
NAME ID IMAGE CMD EXPOSES
redis 32da9f6caf76 crosbymichael/redis:latest /redis/src/redis-ser 6379/tcp
Using the container named 'redis' from our previous example we will create a relationship between our webapp container and redis.
docker ps
NAME ID IMAGE CMD EXPOSES
redis 32da9f6caf76 crosbymichael/redis:latest /redis/src/redis-ser 6379/tcp
# run a new linktesting container and add redis as a child
docker run -link redis -t -i linktesting sh
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>
docker ps
NAME ID IMAGE CMD EXPOSES
redis 32da9f6caf76 crosbymichael/redis:latest /redis/src/redis-ser 6379/tcp
37ewg328232g 37ewg328232g linktesting sh
37ewg328232g/redis 32da9f6caf76 crosbymichael/redis:latest /redis/src/redis-ser 6379/tcp
# rename our linktesting container
docker rename 37ewg328232g testing
docker ps
NAME ID IMAGE CMD EXPOSES
redis 32da9f6caf76 crosbymichael/redis:latest /redis/src/redis-ser 6379/tcp
testing 37ewg328232g linktesting sh
testing/redis 32da9f6caf76 crosbymichael/redis:latest /redis/src/redis-ser 6379/tcp
You can see the relationship between containers very clearly with the output from docker ps
.
docker ps
NAME ID IMAGE CMD EXPOSES
redis 32da9f6caf76 crosbymichael/redis:latest /redis/src/redis-ser 6379/tcp
testing 37ewg328232g linktesting sh
testing/redis 32da9f6caf76 crosbymichael/redis:latest /redis/src/redis-ser 6379/tcp
docker rm testing/redis
docker ps
NAME ID IMAGE CMD EXPOSES
redis 32da9f6caf76 crosbymichael/redis:latest /redis/src/redis-ser 6379/tcp
testing 37ewg328232g linktesting sh
You can interact and with links exactly the same way you interact with any other container.
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 rename $REDIS redis
docker run -t -i -link redis linktesting sh
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 -a $REDIS_ENV_PASSWORD
redis 172.17.0.2:6379> set name docker
OK
redis 172.17.0.2:6379>
When you add a link to another container the parent can also inspect the linked container and any children that the linked container may have.
LOGGER=$(docker run -d crosbymichael/logger)
docker rename $LOGGER logger
REDIS=$(docker run -d -link logger crosbymichael/redis)
docker rename $REDIS redis
docker ps
NAME ID IMAGE CMD EXPOSES
logger 37ewg328232g crosbymichael/logger:latest log --stdout --stderr
redis 32da9f6caf76 crosbymichael/redis:latest /redis/src/redis-ser 6379/tcp
redis/logger 37ewg328232g crosbymichael/logger:latest log --stdout --stderr
docker run -t -i -link redis linktesting sh
# rename the container id to 'testing'
...
docker ps
NAME ID IMAGE CMD EXPOSES
logger 37ewg328232g crosbymichael/logger:latest log --stdout --stderr
redis 32da9f6caf76 crosbymichael/redis:latest /redis/src/redis-ser 6379/tcp
redis/logger 37ewg328232g crosbymichael/logger:latest log --stdout --stderr
testing dsfljh23923f linktesting sh
testing/redis 32da9f6caf76 crosbymichael/redis:latest /redis/src/redis-ser 6379/tcp
testing/redis/logger 37ewg328232g crosbymichael/logger:latest log --stdout --stderr
The testing container can now inspect redis and logger via the namespace created.
REDIS_ENV_{key}={value}
REDIS_CHILD_LOGGER_ENV_{key}={value}
Links are a way to express relationships between containers and allow introspection based on those relationships.