Docker

Updated:

Docker is a great tool for managing web servers in easily deployable, configurable, and disposable containers. If your guest OS is Linux, then this is a great solution. It is most ideal for Linux hosts as that seems to be the only way to get the most out of its features these days.

The most common way to run it on non-Linux hosts is through a VM provider like VirtualBox. This is especially the case when using the Docker Toolbox version, which predates the current Docker for Mac and Docker for Windows. An unfortunate disadvantage to the more recent Docker for Windows is that the Hyper-V extension must be enabled in order to use it. As of the time I used this, enabling this extension also forces the disabling of the virtualization setting in your BIOS that is required to use VirtualBox. As a result, I was not able to both use Docker for Windows and still use VirtualBox for other projects. This may or may not be addressed over time, but it’s worth checking into. I have not yet used Docker for Mac, as I prefer obtaining docker and docker-machine from Homebrew for easy and reliable updates for many open source programs on OS X, much akin to yum or apt-get.

For installing on Linux, Docker recommends either obtaining it from the OS’s package manager or from their site. Instead of working with docker-machine and VirtualBox, you will simply start and enable the docker service.

MySQL

When using a database container like the official mariadb one, you may find yourself wanting to backup the data directory on the host. This is quite easy with a Linux host following their example run command:

$ docker run --name some-mariadb -v /my/own/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mariadb:tag

This will bind mount /my/own/datadir on the host to to the database data folder in the container. You will find that it currently doesn’t work with a Windows or OS X host with Docker for Windows/Mac or Toolbox. What you can do, however, is save the data directory to a volume by way of the VOLUME command in a Dockerfile.

VOLUME ["/var/lib/mysql"]

Now when you build your image, you can use the docker inspect command to find out where in the VirtualBox VM it saved the data directory:

docker inspect --format='{{range .Mounts}}docker-machine path: {{.Source}} Container path: {{.Destination}}{{end}}' database-container-name

Just as an example of how to format that info. In my case, I got the following output:

docker-machine path: /mnt/sda1/var/lib/docker/volumes/f8a7f7908149b244c278a5e21f26942973ee865558e64d5ebb3e5671ee332c2f/_data Container path: /var/lib/mysql

Now I can go into the docker machine and copy that data over. I went to the settings for the VM and saw that there was already a shared folder linked to my C:\Users directory, so I made a folder for it there:

$ docker-machine ssh
# sudo cp -R /mnt/sda1/var/lib/docker/volumes/f8a7f7908149b244c278a5e21f26942973ee865558e64d5ebb3e5671ee332c2f/_data /c/Users/data

Now the data can be backed up anywhere else. You might even consider using rsync or some other backup method to a remote destination in the VM so that case-insensitive file names are preserved.