Docker Containers Linking

Updated 2 January 2023

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:-

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, lets 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 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:-

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.

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:

Further, run this command to access MySQL:-

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.

FOR ANY TYPE OF QUERY OR HELP, KINDLY CONTACT US

Tag(s) docker
Category(s) Uncategorized
author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project






    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home