I have question regarding volumes and ownership.
As an example I'm using this image: privatebin, but this is the same for any case.
First I'm creating volume:
$ docker volume create privatebin-data
From docker inspect, I can see where data is located:
$ docker inspect privatebin-data
[
{
"CreatedAt": "2018-12-04T21:42:46+01:00",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/privatebin-data/_data",
"Name": "privatebin-data",
"Options": {},
"Scope": "local"
}
]
Following instructions from docker hub, I'm starting image:
$ docker run -d --restart="always" --read-only -p 8080:80 -v privatebin-data:/srv/data privatebin/nginx-fpm-alpine:1.1.1
Then I visit http://localhost:8080 and everything is working as expected.
Content of volume now:
$ ls -l /var/lib/docker/volumes/privatebin-data/_data
total 16
drwx------ 3 82 82 4096 Dec 4 21:49 73
-rw-r----- 1 82 82 46 Dec 4 21:49 purge_limiter.php
-rw-r----- 1 82 82 529 Dec 4 21:49 salt.php
-rw-r----- 1 82 82 131 Dec 4 21:49 traffic_limiter.php
I want to backup directory by archiving it:
tar -C /var/lib/docker/volumes/privatebin-data -czf privatebin-data-backup.tar.gz _data
My question is: Can I safely assume that if I restart image, for example on other server, user and group owner will still be 82? Is this proper way of backuping and restoring docker volumes?
The UID/GID come from inside your image, privatebin/nginx-fpm-alpine. So as long as you create users in the same way/order in there, and nothing changes in your base image, then those ID's will be the same regardless of where you run the image.
My preferred way to backup and restore volumes is to use a utility container, just in case the backend of docker changes, or you decide to move your named volume to another location or external data store. The commands to do that look like:
docker run --rm \
-v privatebin-data:/source:ro \
busybox tar -czC /source . >privatebin-data-backup.tar.gz
and
docker run --rm -i \
-v privatebin-data:/target \
busybox tar -xzC /target <privatebin-data-backup.tar.gz