Docker container network portability

Rather than hardcoding network links between a service consumer and provider, Docker encourages service portability.

eg, instead of 2 containers talking directly to each other:

$1

requiring you to restart the consumer to attach it to a different redis service, you can add ambassador containers:

$1

When you need to rewire your consumer to talk to a different resdis server, you can just restart the redis-ambassador container that the consumer is connected to.

This pattern also allows you to transparently move the redis server to a different docker host from the consumer.

Using the svendowideit/ambassador container, the link wiring is controlled entirely from the dockerrun parameters.

Two host Example

Start actual redis server on one Docker host

$1

Then add an ambassador linked to the redis server, mapping a port to the outside world

$1

On the other host, you can set up another ambassador setting environment variables for each remote port we want to proxy to the big-server

$1

Then on the client-server host, you can use a redis client container to talk to the remote redis server, just by linking to the local redis ambassador.

$1

How it works

The following example shows what the svendowideit/ambassador container does automatically (with a tiny amount of sed)

On the docker host (192.168.1.52) that redis will run on:

$1

in the redis_ambassador container, you can see the linked redis containers’s env

$1

This environment is used by the ambassador socat script to expose redis to the world (via the -p 6379:6379 port mapping)

$1

then ping the redis server via the ambassador

Now goto a different server

$1

and get the redis-cli image so we can talk over the ambassador bridge

$1

The svendowideit/ambassador Dockerfile

The svendowideit/ambassador image is a small busybox image with socat built in. When you start the container, it uses a small sed script to parse out the (possibly multiple) link environment variables to set up the port forwarding. On the remote host, you need to set the variable using the -e command line option.

-expose 1234 -e REDIS_PORT_1234_TCP=tcp://192.168.1.52:6379 will forward the local 1234port to the remote IP and port – in this case 192.168.1.52:6379.

$1