Docker Networking Example
As you already read our previous tutorial Docker Networking. This tutorial, we will create a small docker network with 2 docker containers as following.
> 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 the different container.
1. Create Network
First of all, create a new docker network. Use below command to create 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 showing in below command.
$ docker run --name mysql -e MYSQL_ROOT_PASSWORD=secret -d mysql/mysql-server
After creating 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 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 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 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 above step.
1 2 3 4 5 6 7 8 | mysql> GRANT ALL on *.* to 'dbuser'@'172.21.0.3' identified by 'secret'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye |
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 above step to login to PHPMyAdmin.