Nowadays, there are more and more people starting to use docker to containerize their applications, and MySQL is the most popular one.It is as simple as one command line to create and start your own database:
docker run -d –P tutum/mysql
However if you decide to replace MySQL with MariaDB in the future, data migration will be painful to perform. Tutum MySQL/MariaDB images help solve this problem.
Here is a simple example:
1. Create a volume container that holds database data
docker run -d -v /var/lib/mysql –name db_vol -p 22:22 tutum/ubuntu:trusty
Note: you can change the name of the volume container, which will be used in the next step. The volume path (/var/lib/mysql) should not be changed.
2. Create a MySQL container and write data to the volume container:
docker run -d –volumes-from db_vol -p 3306:3306 tutum/mysql:5.5
Note: –volumes-from should use the name of the volume container.
Here’s the log view of the MySQL container:
Since there is no data inside the volume, Tutum MySQL image will help you initialize the database and create an admin user with a random password. If you want to use a preset password, add ‘-e MYSQL_PASS=<yourpass>’ in the run command above.
Let’s create a new database name ‘hello_tutum’:
3. Now stop the container and all the data is preserved in our volume container.
docker stop 7aa5dcaf8e41
Note: change the container id to the one you got in step 2.
4. Create a MariaDB container with the same volume container:
docker run -d –volumes-from db_vol -p 3306:3306 tutum/mariadb:5.5
Here is the log of MariaDB:
Since we have created a database in the volume container, MariaDB will use the volume directly.
Let’s check what’s in the database using the same credentials we used in the MySQL container:
Here you can see that the version of database server changed to MariaDB, while the data was preserved.