How to Run MySQL and phpMyAdmin Using Docker
In this article, we will learn how to deploy MySQL and phpMyAdmin using Docker.
1. Introduction
A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries, and settings.
2. Install MySQL in Docker
In this section, we will discuss how to download, run, and connect to the MySQL
server using Docker.
2.1 Download MySQL Docker image
In this section, we will see how to download the MySQL docker image. Downloading the MySQL image is not strictly necessary – however, performing this before you create your Docker container ensures your local image is up to date. To download the MySQL Community Edition image, run this command:
docker pull mysql/mysql-server
This will download the latest version of the image. You can also download a specific version by using the below command:
docker pull mysql/mysql-server:tag
Where ‘tag’ is the version of the image you want to download.
You will see similar output as below:
~$ docker pull mysql/mysql-server Using default tag: latest latest: Pulling from mysql/mysql-server 03e20c09154c: Pull complete be7ba00cd800: Pull complete 218845b603a2: Pull complete 0807146aa37b: Pull complete Digest: sha256:5dfceab2e53e7dac983168c5f5d611d9d8e85c3bee7297c24c931d77fbeeaac3 Status: Downloaded newer image for mysql/mysql-server:latest docker.io/mysql/mysql-server:latest
2.2 Starting
In this section, we will see how to run the MySQL docker image. To run the MySQL run the below command:
~$ docker run -d mysql/mysql-server
The -d
option will run the image in detach mode. You will see the similar output as below:
~$ docker run -d mysql/mysql-server ce9a316046f06aaa7ca19de6414bbf98cdfc98b1039faf548dc90bddc3e8a733
You can give an optional --name
parameter to give the name of the running image. If no name is provided a random name is generated. If the Docker image of the specified name and tag has not been downloaded by an earlier docker pull
or docker run
command, the image is now downloaded. After the download completes, initialization for the container begins, and the container appears in the list of running containers when you run the docker ps
command.
~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ce9a316046f0 mysql/mysql-server "/entrypoint.sh mysq…" 10 minutes ago Up 10 minutes (healthy) 3306/tcp, 33060-33061/tcp focused_ganguly ~$
The output of the container can be monitored by the below command:
docker logs
You don’t need to give the full name – even the first few characters will do the job if it is able to recognise the container. Once the initialisation is finished, you can grab the password from the logs – [Entrypoint] GENERATED ROOT PASSWORD: ylV3LYdQUGAtoJ@hivOsHAG#ON8
2.3 Connecting to server
Once the server is ready, you can run the mysql
client within the MySQL Server container you just started and connect it to the MySQL Server. Use the docker exec -it
command to start a mysql client inside the Docker container you have started. You can get the root password from the logs.
~$ docker exec -it ce9a316 mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 59 Server version: 8.0.22 Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
Because the MYSQL_ONETIME_PASSWORD
option is true by default, after you have connected a mysql
client to the server, you must reset the server root password by issuing this statement:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'mysql-password'; Query OK, 0 rows affected (0.07 sec)
Substitute mysql-password
with the password of your choice. Once the password is reset, the server is ready for use.
3. Install phpMyAdmin in Docker
In this section, we will see how to install the phpMyAdmin
in Docker. phpMyAdmin
connects using your MySQL server credentials
3.1 Image variants
We have three types of images to choose from:
apache
includes a full Apache webserver with PHP and includes everything needed to work out of the box. This is the default when only a version number is requested.- fpm only starts a PHP FPM container. Use this variant if you already have a separate webserver. This includes more tools and is, therefore, a larger image than the
fpm-alpine
variation. fpm-alpine
has a very small footprint. It is based on Alpine Linux and only starts a PHP FPM process. Use this variant if you already have a separate webserver. If you need more tools that are not available on Alpine Linux, use the fpm image instead.
3.2 Starting the server
Make sure the MySQL
is running before starting the phpMyAdmin
container. We need to link the phpMyAdmin
image to the MySQL
db container. To run the phpMyAdmin
container run the below command:
docker run --name phpmyadmin -d --link ce9a316046f0:db -p 8080:80 phpmyadmin
ce9a316046f0
is the container id of the MySQL
container. If you are running this for the first time the image will be downloaded from the docker hub. You will see similar output as below:
docker run --name phpmyadmin -d --link ce9a316046f0:db -p 8080:80 phpmyadmin 9ff20fe03253bb314343a77ffb9e98e44d7683a8a9af894409714759c562df2e
When you use the run
command you don’t need to explicitly download the image using the pull
command. Now if you do docker ps
you should be able to find both the MySQL and phpMyAdmin running:
~$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9ff20fe03253 phpmyadmin "/docker-entrypoint.…" 7 seconds ago Up 5 seconds 0.0.0.0:8080->80/tcp phpmyadmin ce9a316046f0 mysql/mysql-server "/entrypoint.sh mysq…" 7 days ago Up 7 days (healthy) 3306/tcp, 33060-33061/tcp focused_ganguly
If the MySQL is not running on the same machine where you are running the phpMyAdmin you can provide the host and port in the PMA_HOST
and PMA_PORT
environment variable.
4. Summary
In this article, we started by seeing what is a container. Then we moved on to the instruction to download and install the MySQL and phpMyAdmin using Docker. We also looked at different environment variables available in case we don’t want to use the default configuration.