Blog

How to: Run MySQL and PHPMyAdmin through Docker

Prerequisite:
1. Basic Understanding of Docker
2. Install Docker

Let’s see if in your local machine or on any linux server, you want to run MySQL and PHPMyAdmin to create and access relation MySQL database, how can we achieve this.

Let’s Pull MySQL Docker image first, to check which versions are available, follow this link: DockerHub Available MySQL Tags

docker pull mysql:5.7

Let’s Run this image and get MySQL up and running

docker run --name mysql --restart always -v /my/own/datadir:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-password -d mysql:5.6

Here,
– Container name is mysql
– restart always policy is set up, so every time system reboots or docker restarts, the container will restart automatically.
– instead of /my/own/datadir, replace directory path where you want to persist data of MySQL database, failing in doing so, whenever container restarts, data will be lost.
– the exposed port is 3306, it is recommended that you use different port than 3306 on host machine to achieve more security
– change password you want to set for root user instead of my-secret-password. you can pass MySQL root password as an environment variable. To check more available environment variables, follow this link: Environment Variables
– -d will run this container is daemon(background) mode

Now, let’s run PHPMyAdmin tool to easily access this MySQL database.

Let’s Pull PHPMyAdmin Docker image, to check which versions are available, follow this link: DockerHub Available PHPMyAdmin Tags (latest is recommended)

docker pull phpmyadmin/phpmyadmin:latest

Let’s Run this image and connect it with recently lunched MySQL Container

docker run --name phpmyadmin --restart always -d --link mysql:db -p 8080:80 phpmyadmin/phpmyadmin:latest

Here,
– The container name is phpmyadmin
– link command will link above launched MySQL container with this container, so instead of mysql define your container name
– The port 80 is exposed on 8080, you can change it to any other available port

References :
[1] https://hub.docker.com/_/mysql
[2] https://hub.docker.com/r/phpmyadmin/phpmyadmin
[3] https://identicalcloud.com/blog/what-is-docker-basic-concepts/
[4] https://identicalcloud.com/blog/how-to-install-docker-and-docker-compose/

Leave a Comment