Docker: create a persistent volume in a specific directory
I found the solution:
I had to install local-persist plugin.
I had to mount the volume to create to the mount point as follows:
sudo docker volume create -d local-persist -o mountpoint=/mnt/ --name=extra-addons
Check if I got what I expected:
sudo docker volume inspect extra-addons
Result:
[
{
"CreatedAt": "0001-01-01T00:00:00Z",
"Driver": "local-persist",
"Labels": {},
"Mountpoint": "/mnt/",
"Name": "extra-addons",
"Options": {
"mountpoint": "/mnt/"
},
"Scope": "local"
}
]
That was what I am looking for.
If you don't want to install any plugins to your docker, I would recommend to create a symbolic link for your volume:
$ docker volume create <myVolume>
$ docker volume inspect <myVolume>
[
{
"CreatedAt": "0001-01-01T00:00:00Z",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/<myVolume>/_data",
"Name": "<myVolume>",
"Options": {},
"Scope": "local"
}
]
$ mkdir /mnt/<myVolume>
# if you already have data in your volume, you should copy it to `/mnt/<myVolume>` now
$ sudo rm -rf /var/lib/docker/volumes/<myVolume>/_data
$ sudo ln -s /mnt/<myVolume> /var/lib/docker/volumes/<myVolume>/_data
Now feel free to use your volume as usual (with all your data beeing in /mnt
as you wanted)
I don't think using the local-persist
driver is the way to go. It hasn't been updated in a while. You can mount a local (host) directory into a docker container using docker mount.
Running the following creates a new container with a mounted directory mapped to my desktop.
mkdir extra-addons
docker run -it -v /Users/me/Desktop/extra-addons:/mnt/extra-addons busybox /bin/sh
ls
You can now see a mnt
folder in root of your container.
bin dev etc home mnt proc root sys tmp usr var
Creating a new file in the container
touch /mnt/extra-addons/test.txt
Creates a test.txt file on my host machine. At the specified path. This is now a two way read/write shared folder. Multiple containers can mount the same folder. And it'll persist once you shut your container/s down.