Docker Networking Example

Docker Networking Example

 Docker Networking Example


As you already read our previous tutorial Docker Networking. In this tutorial, we will create a small docker network with 2 docker containers as follows.

> MySQL – A relational database server.
> PHPMyAdmin – A web-based interface to manage MySQL server.

In this tutorial, you will learn how to access MySQL server using PHPMyAdmin running on different containers.

1. Create Network

First of all, create a new docker network. Use the below command to create a new bridge network named my-bridge-network.

$ docker network create -d bridge my-bridge-network

2. Run MySQL Container

Now, run a new MySQL docker container. Set the default root user password with MYSQL_ROOT_PASSWORD variable as shown in the below command.

$ docker run --name mysql -e MYSQL_ROOT_PASSWORD=secret -d mysql/mysql-server

After creating the container add this to our network.

$ docker network connect my-bridge-network mysql

Now view the new IP address of the MySQL container, which is required in the next step.

$ docker inspect mysql | grep "IPAddress"

3. Run PHPMyAdmin Container

Now run a new Docker container of phpmyadmin using the following command. Change the PMA_HOST value with the IP address of MySQL container IP address get in the last step.

$ docker run --name phpmyadmin -d -e PMA_HOST=172.21.0.2 -p 8080:80 phpmyadmin/phpmyadmin

Add this container to our network.

$ docker network inspect my-bridge-network

4. Inspect Our Network

As you have attached both containers to our new network. Let’s inspect the current network settings.

$ docker network inspect my-bridge-network

You will get the result some like below.

5. Allow MySQL to PHPMyAdmin Host

The default MySQL doesn’t allow remote hosts to connect. So allow phpmyadmin for MySQL connection. Get shell access to your MySQL container using the below command.

$ docker exec -it mysql bash

Login to your MySQL server using the password provided during instance creation.

bash-4.2# mysql -u root -p

Create a new user with phpmyadmin host IP address. In my case phpmyadmin host ip address is ‘172.21.0.3‘ as shown in the above step.

6. Access MySQL with PHPMyAdmin

Finally, Connect your docker host system on port 8080 to access phpmyadmin web user interface.

Use MySQL credentials created in the above step to log in to PHPMyAdmin.



Reactions

Post a Comment

0 Comments

close