How to Run MySQL and phpMyAdmin Using Docker
By: Mahbub Zaman
Tags: Programming, Software Engineering, Data Science, Docker, MySQL

Setup

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.

Method 1

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.

Voila! you should see the web page like the one below on your browser.

Admin panel

As you can see, there is a warning message at the bottom. Let’s fix that now.

First, click on the Find out why link

Now click on Create link

You are all set to manage your database!

Method 2

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.

This method is helpful when you want to connect multiple Docker containers.