0

When you use a volume in docker-compose, docker will create a volume with the volume name appended to the directory name: [directory containing yml]_[volume name]

/var/containers/my-important-server-v0.23/docker-compose.yml

volumes:
    server-db:
    # Volume name: my-important-server-v0.23_server-db

Now when you move and rename the directory containing docker-compose.yml for reasons, it will create a new volume with the new directory prepended to the volume name.

In order to prevent this, and allow multiple compose files to use the same volume, we should have created a volume manually:

docker volume create server-db

volumes:
    server-db:
        external: true

How can we transfer the files from my-important-server-v0.23_server-db to server-db?


This is what I tried and seemed to make sense, but it doesn't work as expected.

docker volume create server-db
OLD=$(docker volume inspect my-important-server-v0.23_server-db | jq -r .[0].Mountpoint)
NEW=$(docker volume inspect server-db | jq -r .[0].Mountpoint)
sudo rsync -va $OLD/ $NEW/

Now here is the problem. The directory containing MariaDB files have different sizes. Apparently you can't simply copy the files like that.

Redsandro
  • 11,060
  • 13
  • 76
  • 106
  • I think your problem is answered in this post. https://stackoverflow.com/questions/54051130/share-volumes-between-separate-docker-compose-files/54052250 – Ali Momeni Oct 12 '20 at 13:30
  • 1
    You can also set the environment variable `COMPOSE_PROJECT_NAME` which will override the `[directory containing yml]` part of the generated names. It'd be more reliable to launch a container mounting both directories to do the copy. If there are sparse files involved it's natural and not harmful for the new directory to be larger than the old one. – David Maze Oct 12 '20 at 13:33
  • fwiw imho you should [use the appropriate tools to backup the database volumes](https://mariadb.com/kb/en/full-backup-and-restore-with-mariabackup/). – masseyb Oct 13 '20 at 09:34
  • @DavidMaze you were right. I did an experimental copy and run and inspect the database. The contents match, even if the size on disk varies. – Redsandro Oct 13 '20 at 22:05
  • @masseyb you are absolutely right. Always have a proper backup if the data is of anything resembling importance. On a lower level though, a copy of the data(files) containing the data(base) should yield the same data(base), and sometimes it's fun to take a shortcut/gamble when you're kind of sure after simulating the logic a couple of times in your head. – Redsandro Oct 13 '20 at 22:14

0 Answers0