Mounting a host directory into a docker container when you are using docker-machine

Datetime:2016-08-23 03:16:07         Topic: Docker  DataBase          Share        Original >>
Here to See The Original Article!!!

I used docker before on my Fedora 23 laptop having some images for doing Development tests.

Now I have to use Windows 8.1 on my laptop. The docker toolbox is here a great help, to get a

docker engine on the windows host.

Docker Toolbox creates a Linux host in a VirtualBox VM.

To access the docker host, you have to set the environment in the shell:

docker-machine.exe env <Machine-Name> --shell powershell

This shows hints,on how to set the environment in a PowerShell  to directly talk to the docker daemon.

Everything else on interacting with the container works like talking to a remote docker host.

The Docker Hub is a docker registry providing prebuilt images.

From there I used the MySQL-Image, which is providing a full MySQL DB. The data of the database

is stored in the container if you just start the image:

docker run -d --name mysql_local mysql

docker inspect mysql_local

shows you the configuration of the container. Somewhere in the output, you find, that the image

defines a volume, where you can redirect the content of the database to a local folder.

            "Image": "mysql",             "Volumes": {                 "/var/lib/mysql": {}

On Fedora, I just used the '-v' option to mount a local directory of the laptop and I had the persistent

store, even on rebuilding the container, when using e.g. docker-compose .

With docker-machine from the Docker Toolbox , the data would be stored in the VM of Virtualbox .

When recreating the VM, the data would be lost.

There is one directory, the VM from the Docker Toolbox mounts automatically into the VM Linux:

C:\Users is mounted to /c/Users

Using e.g.:

docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=db -e MYSQL_USER=spring -e MSQL_USER_PASSWORD=spring -e MYSQL_DATABASE=spring -P -v /c/Users/eer/docker_volumes/mysql_db:/var/lib/mysql mysql

allows you to mount a local Windows directory into the container and store the database.

You can also mount other directories, but  this needs manual steps in configuring the VirtualBox VM to share the directory and configure the VM Box Linux to mount it.

About List