How to Run MySQL and phpMyAdmin Using Docker | by Mahbub Zaman | Towards Data Science

Managing a MySQL server using the command-line interface can be difficult for many people. It becomes a lot easier when we can work with a graphical interface. The database administration tool phpMyAdmin solves that problem. We can use this tool over a web interface. It supports MySQL and MariaDB with a wide variety of operations, such as import data, export data, execution of SQL statements, and many more.
We will use Docker for our setup because running and managing phpMyAdmin on a different machine can be nerve-wracking for environment management. Docker solves this problem by using its containerization technology, which uses Docker images. Previously, I’ve discussed how to run MySQL using Docker. You can read the following post, where I’ve covered some basic concepts and commands of MySQL and Docker.
towardsdatascience.com
From this post, you’ll learn how to connect phpMyAdmin to a MySQL server using Docker. This method will save you time and effort since you don’t have to install or configure phpMyAdmin and MySQL.
There are two ways we can connect phpMyAdmin with MySQL using Docker. In the first method, we will use a single Docker compose file. For the second one, I’ll show you how to connect to an already running MySQL Docker container. First, you will need to install Docker. I’ll use macOS for both methods.
In this method, we will use a Docker compose file. We need to put docker-compose.yml inside a folder. The folder name used in this setup is phpMyAdmin. Let’s break down the individual ingredients of the docker-compose.yml file.
version: '3.1'services:  db:    image: mysql    restart: always    environment:      MYSQL_ROOT_PASSWORD: root      MYSQL_DATABASE: test_db    ports:      - "3308:3306"  phpmyadmin:    image: phpmyadmin/phpmyadmin:latest    restart: always    environment:      PMA_HOST: db      PMA_USER: root      PMA_PASSWORD: root    ports:      - "8080:80"
First, we are using a version tag to define the Compose file format, which is 3.1. There are other file formats — 1, 2, 2.x, and 3.x. Get more information on Compose file formats from Docker’s documentation here.
We follow our version tag by the services hash. Inside this, we have to define the services we want to use for our application. For our application, we have two services, db, and phpmyadmin.
To make our setup process quick and easy, we are using the pre-built official image of MySQL and phpMyAdmin for the image tag.
When we use always for the restart tag, the container always restarts. It can save time. For example, you don’t have to start the container every time you reboot your machine manually. It restarts the container when either the Docker daemon restarts or the container itself is manually restarted.
We have defined the environment variables under the environment tag, which we will use for database and phpMyAdmin authentication.
Finally, the ports tag is used to define both host and container ports. For the db service, it maps the port 3308 on the host to port 3306 on the MySQL container. For the phpmyadmin service, it maps the port 8080 on the host to port 80 on the phpMyAdmin container.
Now run the following command from the same directory where the docker-compose.yml file is located. The command docker compose up starts and runs your entire app. If you encounter the following error, that means you’re already running a Docker container on port 3308. To fix the problem, you just have to change to a different port, or you can stop the other container.
Bind for 0.0.0.0:3308 failed: port is already allocated
Now choose any web browser and go to the following address.
http://localhost:8080
Voila! you should see the web page like the one below on your browser.
As you can see, there is a warning message at the bottom. Let’s fix that now.
You are all set to manage your database!
In this method, you will learn how to connect a phpMyAdmin docker container to a MySQL container that is already running. It is helpful when you don’t have a phpMyAdmin service in your docker-compose file.
First, we need to list all the current Docker networks using the following command.
docker network ls
Now you should see phpmyadmin_default from the list. Our goal here is to find the application network that we have created using the docker-compose file in method one. Since we didn’t specify a network name in the docker-compose file for our application, Docker will give the network name based on the name of the directory with _default at the end. In this case, phpmyadmin_default. If you’re interested in Docker networks, check here.
Well done, you have successfully identified the network! Finally, we can run a stand-alone phpMyAdmin Docker container, which is connected to our desired network.
docker run --name stand-alone-phpmyadmin --network phpmyadmin_default -p 8081:80 phpmyadmin/phpmyadmin:latest
The docker run command is used to run a container from an image. Here we are using phpmyadmin/phpmyadmin:latest image.
The --name flag (optional) is used to give the container a specific name. If you don’t want to provide one, Docker will randomly assign a name.
The —-network flag is used to connect to a Docker network.
The -p flag is already discussed in this post.
Now choose any web browser and go to the following address. Use root as a username and password to log in.
http://localhost:8081/index.php
This method is helpful when you want to connect multiple Docker containers.
Database management can be a daunting task when we use a command-line interface. The web-based tool phpMyAdmin takes care of that problem, and Docker makes the entire process smoother. I use this setup to experiment with MySQL queries, which saves a lot of time. By using Docker, I don’t have to worry about MySQL and phpMyAdmin setup. I hope this will help you to get started with MySQL, phpMyAdmin, and Docker.