Start a Project

Docker Containers Linking

Before reading this blog, do read about the docker containers, otherwise, you may not understand how all commands are working in this blog.

How to link 2 containers?

To link containers, first, you need to install Docker and make sure you installed the 1.8.2 version.

Let’s start now

First, pull the fresh container by typing:-

after pulling it just run the container by typing:-

now it will provide a terminal for you,

now you can install apache2 in this container by typing:-

after that, just commit your container by some name, I would prefer the “apache_container” name.

Let execute the command to commit it:-

now stop all the running containers by typing:-

docker stop $(docker ps -aq)

remove all running containers by typing:-

now to see your container images, just hit the command:-

you will get two container names:

apache_container
ubuntu:14.04

Your first container is ready, now let’s proceed further with the second container.

In the second container, we will install the MySQL database & then we will link these two containers with each other.

ok, let’s hit the command and start your default container ubuntu:14.04 by typing:-

now install the database in it by typing:-

then restart MySQL service once by typing:-

After this, check your MySQL user and password by typing:-

And grant permission to your database so it would be accessible from the remote side too, by typing:-

here you are granting complete access to your database to everyone.

First * star stands for all databases, another one stands for all tables and % stands for everyone…

If you face any problem regarding this, feel free to ask by replying to this post.

Further, Change the bind_address in my.cnf file with your editor (which is located in /etc/mysql/ path) by typing:-

now search the line where bind-address=127.0.0.1 & change it to bind-address=0.0.0.0

Let’s commit this container by name, I would prefer the “mysql_db” name, ok now let’s commit this container by typing:-

now stop all the running containers by typing:-

remove all running containers by typing:-

After that, check your container images by typing the command:-

Now, you will get three container names:

apache_container
mysql_db
ubuntu:14.04

Now, the time is to link these two containers……

Let’s run these containers to link with each other by typing:-

now you will have to restart MySQL service once by typing:-

after the previous command you will get the terminal of your docker container, now just restart MySQL service and exit from that container.

Your MySQL container is running in daemonize mode, and we named it “db” for easy linking during connection.

Let’s link your “db” named container with another container by typing:

Further, run this command to access MySQL:-

Now you can access MySQL from another container.

So, Today we learned how to link two containers and share databases between them.

In the next blog, we will discuss how to host a website on docker conatiner.

FOR ANY TYPE OF QUERY OR HELP, KINDLY CONTACT US

Exit mobile version