Published on

Spinning up MySQL Server & PhpMyAdmin container with Docker

Setting up MySQL and PhpMyAdmin doesn't have to be a hassle—especially for developers. With Docker, you can launch both in just a few commands, creating a powerful database environment right on your machine. In this guide, I'll show you how to quickly get everything running, making your development process smoother and faster. Let's get started!

Prerequisites

Install Docker Desktop if you haven't already. It makes it easier to interact with Docker using GUI or CLI.

Create containers

Open terminal, run the command to create a Docker network.

docker network create mynetwork1

You can rename mynetwork with your own name, but make sure all the commands following uses the same name.

note

The --network flag is used to create and manage container networks. When you create containers on the same custom Docker network, they can communicate with each other using container names as hostnames.

Next, create a MySQL container.

docker run -d --name mysql-container --network mynetwork1 -e MYSQL_ALLOW_EMPTY_PASSWORD=true -p 3306:3306 mysql:latest

You'll need to specify one of the following as an environment variable (the -e flag):

  • MYSQL_ROOT_PASSWORD
  • MYSQL_ALLOW_EMPTY_PASSWORD (Uses in example above to allow login to MySQL without password)
  • MYSQL_RANDOM_ROOT_PASSWORD

Read more about MySQL environment here.

Then, create a PhpMyAdmin container instance.

docker run -d --name phpmyadmin-container --network mynetwork1 -e PMA_HOST=mysql-container -e PMA_PORT=3306 -p 8080:80 phpmyadmin

In the PMA_HOST variable above, we set the name of the MySQL container we have created above.

Now, both of the containers should've been running. You can check their status by running docker ps.

CONTAINER ID   IMAGE          COMMAND                  CREATED        STATUS              PORTS                               NAMES
11db916fa868   phpmyadmin     "/docker-entrypoint.…"   10 hours ago   Up About a minute   0.0.0.0:8080->80/tcp                phpmyadmin-container
1ed0cec416c5   mysql:latest   "docker-entrypoint.s…"   10 hours ago   Up 22 seconds       0.0.0.0:3306->3306/tcp, 33060/tcp   mysql-container

Now, let's try to log into PhpMyAdmin. Launch https://localhost:8080 on your web browser. If you didn't specify the username in the step above, the default one will be root.

phpMyAdmin Dashboard

Import existing database to MySQL

You can now interact with the database including importing an existing .sql file using PhpMyAdmin interface. But sometimes, if you upload a large file, an error will occur like below.

PhpMyAdmin Import error

To fix this kind of error, you may need to inspect the PhpMyAdmin container and modify some php configs etc. But what I found easier is to use mysql CLI from the mysql container. Here's how you can do it using Docker Desktop GUI.

Open the mysql-container container.

Docker Desktop open container

Navigate to the Files tab.

Docker Files tab

It will show the container's filesystem files. Find the home folder, right-click on it and choose Import.

Docker files import

Choose the .sql file you want to import. After, choosing, the file will appear under the home folder.

Docker Imported Files

Now, navigate to the Exec tab and run the following command:

mysql -u root -p smartquran < home/smartquran.sql

The -p flag is the table name, you can create one using PhpMyAdmin console.

Docker Exec mysql

Done! You can check your data from PhpMyAdmin console to verify that the import was success.

References