How To Link Docker Containers Across Hosts – The Ambassador Pattern
- Last Updated On: April 7, 2015
- By: devopscube
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
[email protected]:~# 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=/ [email protected]:~#
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.
devopscube
Other Interesting Blogs
List of 10 Best Frameworks used by Data Scientists
The practice of data science requires the use of machine learning frameworks extensively. Now, this could be for many reasons but largely
Sitting On the Fence About DevOps Integration? Don’t Miss Your Chance to Jump on the Bandwagon
DevOps practices are becoming a staple of companies worldwide and include industry giants such as Netflix, Etsy and Google. However, even though
List of Popular Open Source Java Build Tools
In this article, we will look into a few popular and widely used, open-source java build tools with its pros and cons.
How to Setup and Configure Docker Swarm Cluster
In this tutorial, you will learn how to set up a four node docker swarm cluster. Prerequisites Minimum two nodes with Docker
How to Provision Docker Hosts on Azure using Docker Machine
Docker machine helps you to spin up docker hosts locally as well as with various cloud providers. This tutorial will teach to
Comments