Few days ago, I changed my personal server, with all my data, to another one. On this new one, I choose to use Docker to deploy all my old stuff, including this web site. When investigating throught Docker, I thought it was nice to explain the migration from my old server to my new one !
When I installed my previous server, years ago, I did not have any idea and knowlegde about Docker. So, when I had to install my previous server, I went throught internet and check about installing mysql, apache, and deploying wordpress. Since I work with Docker, I have a whole new way to do that ! I will not explain how to install Docker, cause there is another post dealing with that part !
Retrieving and deploying mysql
Retrieving mysql image to Docker
Knowing the way to retrieve images from external repository, just execute the pull command on mysql package (found it using https://hub.docker.com/) :
docker pull mysql
Waiting from Docker to download and extract the image, then you are ready to play with mysql !
Adding user to store mysql data
I choose to create a user per application I will deploy using Docker. Let’s create a mysql user, which will store all data from mysql instance, and create associated directory :
useradd mysql su - mysql mkdir data
Deploying mysql using Docker
Ok, we have the mysql image on our local repository, we have the directory to store data, all we need know is to deploy mysql using Docker ! To do that, just execute the “docker run” and specify the mysql image :
docker run --name mysql-instance \ -v /home/mysql/data:/var/lib/mysql \ -e MYSQL_ROOT_PASSWORD=<my root password> \ --memory=512m \ -d mysql:latest
Command line explanations :
- –name : container name,
- -v / –volume : bind mount a volume. In this cas, Docker will mount automatically /home/mysql/data server directory to /var/lib/mysql container directory,
- -m / –memory : specifying container memory limit
- -e : from hub.docker.com documentation : “This variable is mandatory and specifies the password that will be set for the MySQL
root
superuser account. In the above example, it was set tomy-secret-pw
.“, - -d / –detach : “Run container in background and print container ID“.
Verify that mysql is running
Docker implements a command to know all processes actually running on Docker instance, by using the “docker ps” command. Let’s see what is returned by this specific command :
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 902871f958c1 mysql:latest "docker-entrypoint.s 2 days ago Up 34 hours 3306/tcp mysql-instance
Mysql is running, we will be able to query it using container ip or the link function !
Retrieving and deploying wordpress
Retrieving wordpress image to Docker
Same way as mysql image, I used the pull command specifying wordpress :
docker pull wordpress
Deploying wordpress using Docker
WordPress image is retrieved, let’s deploy it.
docker run --name blog --link mysql-instance:mysql -p 9083:80 -d wordpress
Command line explanations :
- –link : from the official documentation (https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/#communication-across-links), “Links allow containers to discover each other and securely transfer information about one container to another container. When you set up a link, you create a conduit between a source container and a recipient container. The recipient can then access select data about the source.“. With this parameter, my wordpress container will be able to consume data from my mysql container ! The mysql instance, in the wordpress container, will be knowned as “mysql”,
- -p / –publish : this argument specify which port I will query to query wordpress on port 80. I choose the port 9083 cause I have several applications using port 80.
I just check my applications status using the “docker ps” command :
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6c70b48aef18 wordpress "docker-entrypoint.s About an hour ago Up About an hour 0.0.0.0:9083->80/tcp blog 902871f958c1 mysql:latest "docker-entrypoint.s 2 days ago Up 34 hours 3306/tcp mysql-instance
Both of them are running ! To finish the migration, I just have to take care about mysql datas from my old instance to my new one.
Migrating data from mysql
Before initiating the migration, I updated wordpress to the last version, same version deployed using Docker, so I was sure that the mysql tables will be identical. To export datas from my old mysql instance, I just used the mysqldump command :
mysqldump --single-transaction -h localhost -u root -p wordpress > wordpress-dbbackup_20180203.bak
I have my backup, all I have to do is to inject it to my new instance :
mysql -p -h localhost mysql-instance < wordpress-dbbackup_20180203.bak
All my datas are up-to-date, accessing my web site on my new server adress giving me all my post and wordpress apparence, all is OK !