How to link 2 containers?
Note:- Before reading this blog, do read my previous blog carefully, otherwise, you may not understand how all commands are working in this blog.
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:-
1 |
docker pull ubuntu:14.04 |
after pulling it just run the container by typing:-
1 |
docker run -i -t -d ubuntu:14.04 |
now it will provide a terminal for you,
now you can install apache2 in this container by typing:-
1 2 3 |
sudo apt-get update sudo apt-get install -y apache2 |
after that, just commit your container by some name, I would prefer the “apache_container” name, lets execute the command to commit it:-
1 |
docker commit container_id apache_container |
now stop all the running containers by typing:-
docker stop $(docker ps -aq)
remove all running containers by typing:-
1 |
docker rm $(docker ps -aq) |
now to see your container images, just hit the command:-
1 |
docker images |
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:-
1 |
docker run -i -t ubuntu:14.04 |
now install the database in it by typing:-
1 2 3 |
sudo apt-get update sudo apt-get install mysql-server |
then restart MySQL service once by typing:-
1 |
service mysql restart |
After this, check your MySQL user and password by typing:-
1 2 |
mysql -u root -p"give_your_password" |
And grant permission to your database so it would be accessible from the remote side too, by typing:-
1 |
grant all privileges on *.* to root@"%" identified by "your_password"; |
here you are granting complete access to your database to everyone.
First * star stands for all databases and 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:-
1 |
nano /etc/mysql/my.cnf |
now search the line where bind-address=127.0.0.1 & change it to bind-address=0.0.0.0
1 |
save & exit |
Let’s commit this container by name, I would prefer the “mysql_db” name, ok now let’s commit this container by typing:-
1 |
docker commit container_id mysql_db |
now stop all the running containers by typing:-
1 |
docker stop $(docker ps -aq) |
remove all running containers by typing:-
1 |
docker rm $(docker ps -aq) |
After that, check your container images by typing the command:-
1 |
docker images |
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:-
1 |
docker run --name db -p 3306:3306 -id mysql_db |
now you will have to restart MySQL service once by typing:-
1 |
docker exec -i -t db bash |
after the previous command you will get the terminal of your docker container, now just restart MySQL service and exit from that container.
now in daemonize mode your mysql_db container is running, also would like to tell you that we kept the “db” name of your mysql_db container because, at the time of linking, we will need to hit your container by name.
Let’s link your “db” named container with another container by typing:
1 2 3 |
docker run --name web --link db:db -p 80:80 -i -d apache_container In daemonize mode your both container are running and linked as well, now access your web (apache_container) by typing:- |
1 |
docker exec -i -t web bash |
Further, run this command to access MySQL:-
1 |
mysql -h db -u root -p"your_password" |
Now you are able to access MySQL from another container.
So, Today we learned how to link two containers and share databases between them, in my next blog I will tell you about Dockerfile, automatic service restart, and installation of Prestashop Framework in the container.
Be the first to comment.