13

I need to create a persistent volume for Docker. The volume must be named extra-addons and located in /mnt/.

I run this command:

sudo docker volume create /mnt/extra-addons

I got this error message:

Error response from daemon: create /mnt/extra-addons: "/mnt/extra-addons" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path

Note that when I simply run: sudo docker volume create extra-addons, I do not face this problem but when I inspect the volume in question using sudo docker inspect extra-addons, I see it is located in a place I do not want:

[
    {
        "CreatedAt": "2018-04-21T14:40:25+03:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/extra-addons/_data",
        "Name": "extra-addons",
        "Options": {},
        "Scope": "local"
    }
]

I mean I rather want to see the volume like this: /mnt/extra-addons

Any idea?

10

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.

5

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)

  • Don't think it's a good idea deleting docker managed volumes. Use a mount instead to mount a host directory into docker. – Lex Jul 22 at 22:43
2

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.

enter image description here

  • Thanks, this saved me a step. I was trying to add a volume. just mount the dir – Chaim Eliyah Jul 31 at 4:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.