How To Link Docker Containers Across Hosts – The Ambassador Pattern

link docker containers across hosts

One of the limitations of Docker is that they cannot link containers across  hosts using the native link feature. However, various orchestration tools help you deploy containers across hosts and link them together. Another way of cross linking containers is by using the ambassador pattern. In this pattern we you can deploy an ambassador container in each host, and the ambassadors will link the containers together across the hosts.

In this post we will look into a practical demonstration of linking a WordPress container with a MySQL container deployed in two different hosts. We will use “svendowideit/ambassador” image from Docker hub to act as an ambassador. This is a pre-built image with all the necessary configurations to act as an ambassador.

Follow the steps given below to set up a WordPress website in ambassador pattern.

Requirements: Two hosts running Docker server.

Launch a mysql container in host2 using the official MySql image with the required environment variables as shown below.

docker run -d --name db -e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_USER=mycloud -e MYSQL_PASSWORD=mycloud \
-e MYSQL_DATABASE=wordpress mysql

Launch an ambassador container in host2 by exposing mysql port using svendowideit/ambassador docker  image.

sudo docker run -d --link db:db --name mysql_ambassador \
-p 3306:3306 svendowideit/ambassador

At host1 launch an ambassador container by exposing port 3306 and set an environment variable for the db port information with the host2 IP address for proxying the requests from host1 to host2.

sudo docker run -d --name mysql_ambassador --expose 3306 \
-e DB_PORT_3306_TCP=tcp://172.0.0.194:3306 svendowideit/ambassador

DB_PORT_3306_TCP is the environment variable used by the mysql_ambassador container in host2. You can list all the environment variable’s used by this container by executing the following command.

docker exec -it mysql_ambassador env
root@devopscube:~# docker exec -it mysql_ambassador env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=5ecdaa4b2a00
DB_PORT=tcp://172.17.0.3:3306
DB_PORT_3306_TCP=tcp://172.17.0.3:3306
DB_PORT_3306_TCP_ADDR=172.17.0.3
DB_PORT_3306_TCP_PORT=3306
DB_PORT_3306_TCP_PROTO=tcp
DB_NAME=/mysql_ambassador/db
DB_ENV_MYSQL_ROOT_PASSWORD=root
DB_ENV_MYSQL_USER=mycloud
DB_ENV_MYSQL_PASSWORD=mycloud
DB_ENV_MYSQL_DATABASE=wordpress
DB_ENV_MYSQL_MAJOR=5.6
DB_ENV_MYSQL_VERSION=5.6.23
HOME=/
root@devopscube:~#

Create a WordPress container with required environment variable to connect to MySQL database.

docker run -d -p 80:80 --name wordpress --link \
 mysql_ambassador:db -e WORDPRESS_DB_HOST=db \
-e WORDPRESS_DB_USER=mycloud -e WORDPRESS_DB_PASSWORD=mycloud \
-e WORDPRESS_DB_NAME=wordpress wordpress

Now we have a WordPress application running with MySQL database in a different host. You can test this by accessing the WordPress application using the host IP. If it was configured and the links are properly established between the containers and ambassadors, it will take you to the install page as shown below.

docker ambassador wordpress

 

1 comment
  1. Pingback: Docker Networking Tutorial : Connect Containers Using Weave
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like