26

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?

3 Answers 3

20

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.

15

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)

1
  • Don't think it's a good idea deleting docker managed volumes. Use a mount instead to mount a host directory into docker. Commented Jul 22, 2019 at 22:43
7

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

5
  • Thanks, this saved me a step. I was trying to add a volume. just mount the dir Commented Jul 31, 2019 at 4:50
  • 2
    this creates a bind mount, docker suggest to use volumes, and have several advantages docs.docker.com/storage/volumes Commented Mar 13, 2020 at 8:45
  • @JoeCabezas yeah volumes are a better choice for production given they're docker managed and isolated from other operating system tasks. But for this question in a specific directory bind mounts are the answer. It does enhance the answer distinguishing the two. Commented Mar 13, 2020 at 20:07
  • thanks for the insight, you are right, in order to answer the question a bind volume is enough. Commented Mar 14, 2020 at 1:08
  • I specifically wanted to use a bind mount but was having issues with PostgreSQL until I found a good answer. Just sharing because I ended up here when looking for a solution. Even though the question is for Windows, I had the same error on Linux. Commented Nov 23, 2021 at 14:44

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.