Hello Everyone,
I am back with some practical approach. Today we will learn, How to link 2 containers?
Note:- Before reading this blog, do read my previous blog carefully, otherwise you will not understand some important points in this blog, how all commands are working….
To link containers, first you need to install Docker from my previous blog, make sure you installed 1.8.2 version.
Lets start now
First pull the fresh container by typing:-
docker pull ubuntu:14.04
after pulling it just run the container by typing:-
docker run -i -t -d ubuntu:14.04
now it will provide terminal to you,
now you can install apache2 in this container by typing:-
sudo apt-get update sudo apt-get install -y apache2
after installation, just commit your container by some name, i would prefer “apache_container” name, lets execute command to commit it:-
docker commit container_id apache_container
now stop all the running container by typing:-
docker stop $(docker ps -aq)
remove all running container by typing:-
docker rm $(docker ps -aq)
now to see your container images, just hit the command:-
docker images
you will get two container name:
apache_container
ubuntu:14.04
Now your first container is ready, now lets start with second container & in the second container we will install mysql database & then we will link these two container with each other.
ok lets hit the command and start your default container ubuntu:14.04 by typing:-
docker run -i -t ubuntu:14.04
now install database in it by typing:-
sudo apt-get update sudo apt-get install mysql-server
then restart mysql service once by typing:-
service mysql restart
now check your mysql user and password by typing:-
mysql -u root -p"give_your_password"
now grant the permission of your database so it would be accessible from remote side too, by typing:-
grant all privileges on *.* to root@"%" identified by "your_password";
in the previous command you are granting the complete access of your database to everyone.
First * star stands for all databases and the another one stands for all tables and % stands for everyone…
If you face any problem regarding this, feel free to ask by replying on this post…
now change the bind_address in my.cnf file with your editor (which is located in /etc/mysql/ path) by typing:-
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
save & exit
now lets commit this container by name, i would prefer “mysql_db” name, ok now lets commit this container by typing:-
docker commit container_id mysql_db
now stop all the running container by typing:-
docker stop $(docker ps -aq)
remove all running container by typing:-
docker rm $(docker ps -aq)
now check your container images by typing the command:-
docker images
you will get three container name:
apache_container
mysql_db
ubuntu:14.04
now the time is linking these two containers……
Ok now lets run these container to link with each other by typing:-
docker run --name db -p 3306:3306 -id mysql_db
now you will have to restart mysql service once by typing:-
docker exec -i -t db bash
after previous command you will get 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 “db” name of your mysql_db container because at the time of linking, we will need to hit your container by name.
now lets link your “db” named container with another container by typing:
docker run --name web --link db:db -p 80:80 -i -d apache_container
now in daemonize mode your both container are running and linked as well, now access your web (apache_container) by typing:-
docker exec -i -t web bash
and run the command to access mysql:-
mysql -h db -u root -p"your_password"
Now you are able to access mysql from another container.
So, Today we learnt how to link two container and share database between them, in my next blog i will tell you about Dockerfile, automatically service restart and installation of Prestashop Framework in container….
So till then just wait & keep exploring webkul’s Blog……