How to run MySQL server in a Docker container and connect with phpMyAdmin

Prasad Lakshan
UCSC ISACA Student Group
5 min readJan 13, 2023

Hi, I’m going to walk you through the process of running mysql server inside a docker container and connecting to the database from the terminal and phpMyAdmin.

Docker is a powerful tool that allows programmers to quickly set up and manage their development environments. One of the most popular uses of Docker is running a local development environment for a database and in this post, I will show you how to run MySQL in docker with phpMyAdmin for easy management.

Prerequisites

Before starting to work on this, you have to properly install the docker and docker-compose on your machine. You can download and install it from the official website of docker, https://docs.docker.com/get-docker. Once you complete the installation, run the command docker --version in your terminal to verify that the installation was successful.

Step 01 — Run MySQL Server in Docker Container

The first thing you need to do is, pulling the mysql docker image from the docker hub. This image includes everything you need to run a mysql server in a docker container. You can do this by running the below command on your terminal. For more information refer to this link.

docker pull mysql

It’ll take some time to download the docker image and once it’s complete, you can run the command, docker images in your terminal and view the existing docker images on your machine. Let's use the mysql docker image to create a new container. For that, use the following command.

docker run -e MYSQL_ROOT_PASSWORD=pass --name mysql-container -d mysql

By running this command you can create a new mysql container from the docker image. In the command,

  • docker run — is used to start a new container from an image
  • -e — is a flag used to set the environment variable for the container. In this, we are settingMYSQL_ROOT_PASSWORD the variable to “pass” which will be used as the root user password when connecting to the mysql server.
  • --name — flag is used to give a specific name to the container, in this case, I have used “mysql-container” as the name of the container.
  • -d — flag is used to run the container in detached mode, which means the container will run in the background and you will able to use the terminal when the container is running.
  • Finally mysql is the image name you are using to create a container

By running docker container ls command in the terminal you can see the names of the currently running docker images. and if you want to stop any docker image you can use the docker stop <container_name> command. And also to remove the docker image use the docker prune <container_name> command. The <container_name> should be replaced with your container name.

Step 02 — Connect to MySQL Server Container via Bash

Now that we have a running mysql server on our machine, we can connect to that server using the terminal. You can get a list of the currently running containers with docker container ls. For that, we need to have the IP addresses of the running containers. We can use the following command to get them.

docker inspect --format '{{ .NetworkSettings.IPAddress }}' mysql-container

You have to remember the IP address to connect with the mysql server. For that, we will create a new container from the mysql image and open a bash shell inside the container. Then we can use the shell to interact with the mysql database running inside the container. For that run the docker run -it mysql /bin/bash command in your terminal. This will open a bash shell. Next, you can connect to the mysql server with bash shell by using the following command

mysql -h 172.17.0.2 -u root -p

By running this command you can connect with the mysql server. For that, you need to use the password you have earlier assigned to the MYSQL_ROOT_PASSWORD.

  • -h — flag is used to specify the host IP address in this you have to use the previously received IP address by inspecting the container.

Then you can use mysql in your terminal.

Step 03 — Connect MySQL Server via phpMyAdmin

First, you need to pull the phpMyAdmin docker image from the docker hub. By running this image you can run phpMyAdmin(a PHP-based application) inside a container created from the image. The container is an isolated environment that includes everything needed to run the application, such as the PHP runtime, web server, and phpMyAdmin files.

docker pull phpmyadmin

after it’s completed, Use the command,docker images to list the existing docker images on your machine to ensure that.

Now, let's proceed to running the phpMyAdmin and connecting to the mysql server we have created previously. For that, use the following command.

docker run --name myphpadmin -d --link mysql-container:db -p 8080:80 phpmyadmin

By using this command you will able to connect the phpMyAdmin with mysql server container. In this command,

  • --link an option creates a network connection between two containers, When two containers are linked, they can communicate with each other using their hostname. Here, the mysql-container is being linked to the myphpadmin container, and the alias “db” is used. This means that the myphpadmin container can refer to them mysql-container as “db” when connecting to the database.
  • -p 8080:80 is an option to map the host machine’s port 8080 to the container’s port 80, making the application accessible on port 8080 on the host machine.

Once the container is up and running, you can access the phpMyAdmin interface by navigating to “http://localhost:8080/” url from your web browser. You can login to the phpMyAdmin by providing the username as “root” and the password as the password you have assigned to the MYSQL_ROOT_PASSWORD. Here, I have used “pass”.

Finally, now you can use the MySQL server with phpMyadmin, which runs inside a docker container!

--

--

Prasad Lakshan
UCSC ISACA Student Group

Tech enthusiast, passionate about exploring opportunities to learn, teach, help, and take experiences. 🌐 https://howtocodes.com