Running MySQL inside docker

On a Cpanel Server with an old version of PHP, one of the applications requires MySQL 5.6 to work. Since the PHP version is no longer supported, updating MySQL using WHM will upgrade PHP to one of the supported versions, which may break existing sites. So I decided to install MySQL inside docker.

First, create a folder for MySQL docker to store data.

mkdir /home/mysql-docker-data

Now run

docker run --name serverok-mysql --restart=unless-stopped -v /home/mysql-docker-data:/var/lib/mysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=KqAtPd3BpyKjQ -d mysql:5.6

For MairaDB 10.1, run

docker run --name serverok-mysql --restart=unless-stopped -v /home/mysql-docker-data:/var/lib/mysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=KqAtPd3BpyKjQ -d mariadb:10.5

Here I used “KqAtPd3BpyKjQ” as the password, replace it with your password.

-p 3307:3306 -> tell docker to map port 3306 inside the docker container to port 3307 on host machine.

This will pull the MySQL docker image and start it.

Once it finished downloading, you can check if it is running with the command

root@rapidswitch [~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
24c86d5ce381        mysql:5.6           "docker-entrypoint..."   8 minutes ago       Up 8 minutes        0.0.0.0:3307->3306/tcp   serverok-mysql
root@rapidswitch [~]# 

To connect to MySQL use

mysql -h 127.0.0.1 --port 3307 -u root -pKqAtPd3BpyKjQ

Auto Start on Boot

To start the container on boot, you can run

docker update --restart=unless-stopped serverok-mysql

Or use /etc/rc.local, this may not work with newer versions of Ubuntu/Debian

docker start serverok-mysql
Need help with Linux Server or WordPress? We can help!

Leave a Reply

Your email address will not be published. Required fields are marked *