How to deploy MySQL and phpMyAdmin with Docker

Create the MySQL docker container

$ docker pull mysql/mysql-server:5.7
$ docker run -d --name=mysqldb -e MYSQL_ROOT_HOST=% -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=perfdb -v /var/lib/osd/mounts/mysql_data:/var/lib/mysql -p 3306:3306 mysql/mysql-server:5.7
$ docker logs mysqldb
$ docker ps -a
CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS                    PORTS                                                  NAMES
ff380903d3a0   mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   34 seconds ago   Up 34 seconds (healthy)   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysqldb

Note:

  • The option -e passes a value to the container environment variable.
  • The variable MYSQL_ROOT_HOST=% creates a root user with permission to login from any IP address.
  • The variable MYSQL_ROOT_PASSWORD creates the root password of MySQL. If not provided, a random password will be generated.

Create MySQL database and restore from a db backup(if exists)

$ docker exec -it mysqldb bash

bash-4.2# mysql -V
mysql  Ver 14.14 Distrib 5.7.37, for Linux (x86_64) using  EditLine wrapper

bash-4.2# mysql -u root -p

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| perfdb             |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
+---------------+-------------------------------------------+-----------------------+-----------+
| user          | authentication_string                     | plugin                | host      |
+---------------+-------------------------------------------+-----------------------+-----------+
| root          | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 | mysql_native_password | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| healthchecker | *36C82179AFA394C4B9655005DD2E482D30A4BDF7 | mysql_native_password | localhost |
| root          | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 | mysql_native_password | %         |
+---------------+-------------------------------------------+-----------------------+-----------+
5 rows in set (0.01 sec)

bash-4.2# mysql -u root -p perfdb < /var/lib/mysql/perfdb_dump_20220412_121339.sql
bash-4.4# ls -ltr /var/lib/mysql

mysql> use mydb;
mysql> show tables
mysql> SELECT table_schema "DB Name", ROUND(SUM(data_length + index_length) / 1024 / 1024, 1) "DB Size in MB"  FROM information_schema.tables  GROUP BY table_schema;

Create the phpMyAdmin docker container

$ docker pull phpmyadmin/phpmyadmin:latest
$ docker run --name phpadmin -d --link mysqldb:db -p 8080:80 phpmyadmin/phpmyadmin

Note:

  • The option –link provides access to another container running in the host. In our case, the container mysqldb created in previous step is linked and the resource accessed is the MySQL db.
  • The option -p provides mapping between the host port(8080) and the container port(80, which is used by the apache server for the phpMyAdmin web application in the container).

Check the docker images and containers

$ docker images
REPOSITORY                    TAG       IMAGE ID       CREATED        SIZE
phpmyadmin/phpmyadmin         latest    5682e7556577   2 months ago   524MB
mysql/mysql-server            5.7       b3eaae317eb2   2 months ago   390MB

$ docker ps -a
CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS                   PORTS                                                  NAMES
a19c6406ffff   phpmyadmin/phpmyadmin    "/docker-entrypoint.…"   38 seconds ago   Up 38 seconds            0.0.0.0:8080->80/tcp, :::8080->80/tcp                  phpadmin
ff380903d3a0   mysql/mysql-server:5.7   "/entrypoint.sh mysq…"   5 minutes ago    Up 5 minutes (healthy)   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysqldb

Access the phpMyAdmin from browser

Image

Reference