Docker – mysql – permanent databases

If you want your databases in docker to remain permanent then this post is for you.

With docker you can save a lot of time what you would otherwise spend at setting up your working environment when you reinstall your OS,  when you change project or when you need to set up the same environment for one of your colleagues.

The problem takes its origins from our volatile world. Libraries get changed, packages get updated. If you set up your project once on your computer successfully, this doesn’t mean that you will  reach success following the same path as you did then.

Docker comes to help. The single issue is that it’s volatile too. You either need to do commit messages on every change of your container (what I actually did at start of my path with docker) or you  can separate volatile data from docker.

 

You can find the plethora of tutorials which say you how you to set up your code for docker.   But not for database.

It my case I needed mysql.  It was tricky for me to do it at first and took me a while. But this is actually dead easy to do.

The steps are:

  • 1. Install mysql in docker
  • 2. do command sudo service mysql stop in container
  • 3. inside container launch sudo cp -rp /var/lib/mysql/   ./mysqlfolder   (. is any path what is available from outside on your host machine)
  • 4. then you can exit container
  • 5. sudo chown  mysql:mysql  mysqlfolder   – we need to change permissions to folder so that mysql can accept it
  • 6. now you can mount folder to docker adding one more -v option  and your databases are going to never disappear anymore

 

For example I use such command for running one of my projects during development cycle:

docker run –rm  -w=’/var/www’  -v /home/lauer/www/bs:/var/www -v  /home/lauer/www/bs/protected/.bashrc:/root/.bashrc  -v /home/lauer/.ssh:/root/.ssh  -v  /home/lauer/mysqlfolderall:/var/lib/mysql  -p 80:80   -t -i apachenmysqllauer /bin/bash

 

 



Leave a Reply

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