Docker

Set up a MySQL Server and phpMyAdmin with Docker

In this article, I am going to show you how to use Docker Compose to create a MySQL container and access it using phpMyAdmin 5, the web-based MySQL admin interface. I will also show you how to access the MySQL database server running in a Docker container from DataGrip IDE. So, let’s get started.

Requirements:

In order to follow this article, you must have Docker installed on your computer. LinuxHint has a lot of articles that you can follow to install Docker on your desired Linux distribution if you don’t have it installed already. So, be sure to check LinuxHint.com in case you’re having trouble installing Docker.

Installing Docker Compose:

You can download Docker Compose binary file very easily with the following command:

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/
docker-compose-$(uname -s)-$(uname -m)"
-o /usr/local/bin/docker-compose

NOTE: curl may not be installed on your Linux distribution. If that’s the case, you can install curl with the following command:

Ubuntu/Debian/Linux Mint:

$ sudo apt install curl -y

CentOS/RHEL/Fedora:

$ sudo dnf install curl -y

Once docker-compose binary file is downloaded, run the following command:

$ sudo chmod +x /usr/local/bin/docker-compose

Now, check whether docker-compose command is working as follows:

$ docker-compose version

It should print the version information as shown in the screenshot below.

Setting Up Docker Compose for the Project:

Now, create a project directory (let’s say ~/docker/mysqldev) as follows:

$ mkdir -p ~/docker/mysqldev

Now, navigate to the project directory ~/docker/mysqldev as follows:

$ cd ~/docker/mysqldev

Now, create a docker-compose.yaml file in the project directory ~/docker/mysqldev and type in the following lines in the docker-compose.yaml file.

version: "3.7"
services:
  mysql-server:
    image: mysql:8.0.19
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: secret
    volumes:
      - mysql-data:/var/lib/mysql
    ports:
      - "3306:3306"
   
  phpmyadmin:
    image: phpmyadmin/phpmyadmin:5.0.1
    restart: always
    environment:
      PMA_HOST: mysql-server
      PMA_USER: root
      PMA_PASSWORD: secret
    ports:
      - "8080:80"
volumes:
  mysql-data:

The docker-compose.yaml file should look as follows.

Here, I have created 2 services mysql-server and phpmyadmin.

mysql-server service will run the mysql:8.0.19 image (from DockerHub) in a Docker container.

phpmyadmin service will run the phpmyadmin/phpmyadmin:5.0.1 image (from DockerHub) in another Docker container.

In mysql-server service, the MYSQL_ROOT_PASSWORD environment variable is used to set the root password of MySQL.

In phpmyadmin service, the PMA_HOST, PMA_USER, PMA_PASSWORD environment variables are used to set the MySQL hostname, username and password respectively that phpMyAdmin will use to connect to the MySQL database server running as mysql-server service.

In mysql-server service, all the contents of the /var/lib/mysql directory will be saved permanently in the mysql-data volume.

In the mysql-server service, the container port 3306 (right) is mapped to the Docker host port 3306 (left).

In the phpmyadmin service, the container port 80 (right) is mapped to the Docker host port 8080 (left).

Starting MySQL server and phpMyAdmin Services:

Now, to start the mysql-server and phpmyadmin services, run the following command:

$ docker-compose up -d

The services should start in the background.

To see how the ports are mapped, run the following command:

$ docker-compose ps

As you can see, for the mysql-server service, the Docker host port 3306 is mapped to the container TCP port 3306.

For the phpmyadmin service, the Docker host port 8080 is mapped to the container TCP port 80.

Accessing phpMyAdmin 5 or MySQL server from Other Computers:

If you want to access phpMyAdmin 5 or MySQL database server from other computers on your network, you must know the IP address of your Docker host.

To find the IP address of your Docker host, run the following command:

$ ip

In my case, the IP address of my Docker host 192.168.20.160. It will be different for you. So, make sure to replace it with yours from now on.

Accessing phpMyAdmin 5 from Web Browser:

To access phpMyAdmin 5, open your web browser and visit http://localhost:8080 from your Docker host or http://192.168.20.160:8080 from other computers on your network.

phpMyAdmin 5 should be loaded in your web browser.

You will see the following warning. Click on Find out why.

Now, click on Create.

A phpmyadmin database should be created and the warning should be gone.

Now, you can use phpMyAdmin to manage your MySQL databases and tables.

Accessing MySQL from DataGrip:

You can also access your MySQL database server from DataGrip IDE or any other SQL IDEs.

In case of DataGrip, click on + from the Databases section and go to Data Source > MySQL.

Now, type in 192.168.20.160 as Host, 3306 as Port, root as User, secret as Password and then click on Test Connection.

If everything is well, you should see the MySQL database server information as shown in the screenshot below.

Now, click on OK.

Now, you should be able to manage your MySQL databases and tables from DataGrip.

Stopping MySQL Server and phpMyAdmin Services:

Now, to stop the mysql-server and phpmyadmin services, run the following command:

$ docker-compose down

The mysql-server and phpmyadmin services should be stopped.

Cleaning Up MySQL Server Data:

If you want to remove all the MySQL database data and settings, you must remove the mysql-data volume.

You can find the actual name of the volume with the following command:

$ docker volume ls

As you can see, the volume to remove is mysqldev_mysql-data.

You can remove the volume mysqldev_mysql-data with the following command:

$ docker volume rm mysqldev_mysql-data

References:

[1] https://hub.docker.com/_/mysql
[2] https://hub.docker.com/r/phpmyadmin/phpmyadmin/

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.