How to access docker volume data from host machine?

Hello,

I have important config files needed for the nms tool inside container directory - /var/lib/enms. Also when the container run other data related to the container save it in the same /var/lib/enms directory. I wanted to take backup of this data in local host machine. so run the below command.

docker run -v /root/nms:/var/lib/enms mohanstack/nms:1.0

Above docker command completely wiped off the container directory /var/lib/enms. so container throws error and quits

But when If I did below steps, the already existing data in container /var/lib/enms not erasing and newly added data are copied to created volume and container works fine.

docker volume create nmsvol

docker run -v nmsvol:/var/lib/enms mohanstack/nms:1.0

But the problem is I dont find a way to access the volume data nmsvol. Is there way that I access the data of the nmsvol from the local system,

Thanks,
,Mohan

1 Like

Your first attempt used a bind.
Your second attempt used a named volume. Only with named volumes existing data is copied from the container target folder into the volume.

You can configure a named volume that binds a host folder into the container:

docker volume create --driver local -o o=bind -o type=none -o device="/root/nms" nmsvol
2 Likes

Here is a simple command to start a temporary Debian container and copy all the data from nmsvol to a tar file in the current directory:

docker container run --rm -v "nmsvol:/source" -v "$(pwd):/backup" -w /source debian tar czf /backup/nmsvol.tar.gz .
1 Like

The question I believe you are asking is: where does the “docker volume create” command place the “nmsvol” volume? I don’t know if that changes with different installs of Docker on different hosts, but I use PhotonOS and there Docker stores volumes in the /var/lib/docker/volumes directory. Maybe take a look there.

2 Likes

This worked, thanks.

1 Like

Thank you very much. After hours of coding and recoding, searching the internet and a stackoverflow question
none of which worked,
your answer fixed it.

Q1. Where can I reference parameters (especially -o) of docker volume create Docker CLI ? There is not much in docker.org.doc(docker volume create | Docker Documentation)

Q2.Can I set docker volume to store in some other dir? If not how to deal when /var/lib/docker/volumes space is out?

Well it pretty much depends on the driver and type. The local driver is able to mount whatever you can mount on the host. Most parameters of the mount command work here as well, but some of them might require a slightly different syntax.

device: hostpath
nmsvol: name of the volume

The declaration of the volume will be still present in /var/lib/docker/volumes/${volume}/opts.json, but the data will be whatever path you picked as device.

Thanks for your detailed answer, and it’s helpful. :smiley:

I just access them on /var/lib/docker/volumes/[volumename]/_data

You can do that, but you need to be root which makes it a bit more complicated. I would also avoid working in the docker data root directory directly, but again, if you are careful, you can do that.