The concept of Linking Docker Container allows a user to get one container to talk to another. This concept of linking is important as it allows for easier management of containers. Before diving into the world of container linking, perhaps it would be best if you quickly read-up on my blog post about Containers in general.
Linking Docker Container, Why?
We all know what a container is by know, if not then checkout my previous post on Containers. Moving on, why is it important that we are able to link multiple containers together. Well, as discussed, a container is something that can be used to contain a specific package of information in the form of OS, apps etc. Containers can also contain servers, applications etc. Do you think it would be a good idea to bundle a backend server and front end application into a single container?
No, why would you want to couple two types of application into a single container? Decoupling the two different types of applications means that you can hook up different types of backend servers (provided by a container) to a different front end application. The beauty of having containers with different types of application is that you can hook up different containers.
WordPress and MySQL
Let’s have a look at a real example where we link a WordPress container to a MySql container:
In the image above, we are hooking up a WordPress container to a MySql container. They both have their own variables and are also able to talk to each other. For instance, let’s follow the instruction set below:
docker pull mysql docker pull wordpress docker run –-nameqas-sql–eMYSQL_ROOT_PASSWORD=password -d mysql:latest docker run –-name qas-wordpress –-linkqas-sql:mysql–p 8080:80 –d wordpress
In the above commands, we are firstly pulling both images into our local drive. We then run the MySQL image as a container and set it some additional properties such as database password, version of the image etc. This gives us control over the MySql server in isolation.
We then run the WordPress image as a container but also link it to the already running MySql server. Without the server, the WordPress container would not work. We are also able to set the port of the WordPress site.
If you now navigate to http://localhost:8080, this should now redirect you to your running instance of WordPress.
What Have We Learned?
This blog post is really an extension of the Containers post. In this post, we discussed the topic of linking Docker container. More importantly, we discussed the benefits of decoupling containers.
Well, what do you think?