How to Deploy MySQL + PHPMyAdmin Environment Container using Docker

Hello People!

Nothing better than start figuring out what is the difference between a Solution build in the old style and now with Containers using Docker.

It’s really a HUGE difference starting at the point that to Deploy it takes us simple 5 minutes or Less (with the O.S already installed of course).

Ok, no matter you’re using CentOS or Red Hat – OR – another distribution. The article today will take care about creating a standard environment for Database administration using MySQL and PHPMyAdmin using Docker.

We will create 2 containers, One to MySQL Database and Another to PHPMyAdmin.

So, Joao, can we create one unique Container with both solution? Yes! we can, but today the idea is to show how containers can talk each other using same network infrastructure and how things works at the Docker Environment.

I’m really sure that after that you gonna start understanding better how the entire things works.

Basically we will:

1 – Create the Network for both MySQL and PHPMyAdmin Containers
2 – Create the Filesystem that will make the modifications on Container (MySQL) Persistent
3 – Create the MYSQL Container
4 – Create the PHPMyAdmin Container
5 – Run everything together at the Navigator

Can we go? GOOD!!

1st Step: Create Network

# docker network create mysqlnet

2nd Step: Create the Filesystem for MySQL Database that we will map to the Container so it could be possible data being Persistent.

# mkdir -p /opt/mysql

3th Step: Create the MySQL Container with root password set to “new4you”, using the network created (mysqlnet), with the container name set to mysqlsrv, exposing port 3306 and with the latest mysql image from Docker registry.

# docker run -d \
--name mysqlsrv \
--network mysqlnet \
-e MYSQL_ROOT_PASSWORD="new4you" \
-v /opt/mysql:/var/lib/mysql \
-p 3306:3306 \
mysql

4th Step: Create the PHPMyAdmin Container with the name phpmyadminsrv, using the container (database host) mysqlsrv, in the network mysqlnet (so it could be possible the containers having the same network providing communication between them), mapping the port 80 to 8080 with the latest PHPMyAdmin image available.

# docker run -d \
    --name phpmyadminsrv \
    --network mysqlnet \
    -e PMA_HOST=mysqlsrv \
    -p 8080:80 \
    phpmyadmin/phpmyadmin

Ok, done these steps, just go to the navigator and type the IP address of the Docker Host that we create those containers pointed to the port 8080, something like this:

http://youhostip:8080

You should receive a page something like this:

That’s it!

Let’s improve?

Let’s make those containers auto start after machine boot:

We will now create 2 systemd files (create 2 services), one for mysqlsrv container and another to phpmyadminsrv.

First we will start the mysqlsrv container after docker service is up and running and, after, we start the phpmyadminsrv after mysqlsrv service is running. EASY.

# vi /etc/systemd/system/mysqlsrv.service

Add the following content:

[Unit]
Description=MySQL container
Requires=docker.service
After=docker.service

[Service]
Restart=always
ExecStart=/usr/bin/docker start mysqlsrv
ExecStop=/usr/bin/docker stop mysqlsrv

[Install]
WantedBy=default.target

Let’s make it enabled after the system start.

# systemctl enable mysqlsrv.service

Now, the PHPMyAdmin service:

# vi /etc/systemd/system/phpmyadminsrv.service

Add the following content:

[Unit]
Description=PHPMyAdmin container
Requires=mysqlsrv.service
After=mysqlsrv.service

[Service]
Restart=always
ExecStart=/usr/bin/docker start phpmyadminsrv
ExecStop=/usr/bin/docker stop phpmyadminsrv

[Install]
WantedBy=default.target

So, for the last, we’ll make the PHPMyAdmin service (container) start after mysqlsrv container (service) is up and running:

# systemctl enable phpmyadminsrv.service

Reboot the machine ans check if both are online and test at the navigator again.

Beautiful isn’t it?

Any question or concerns and even improvements, just write down the comments and I’ll be more than happy to answer or adapt the post.

See you guys!

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.