Migrating Docker to a different volume, keeping your data intact

Photo by Mohammad Rahmani / Unsplash

Moving the Docker package from one volume to another on Synology

Docker Dec 11, 2022

There's a lot of info out there about moving docker from one volume to another. Almost all of it unfortunately is for systems which differ from DSM, and although still Linux, do have some significant changes.

The below will run through how to move the actual package across volumes, and some of the work you may need to do afterward. It's worth noting that this will initially remove the docker package and all your containers, but not to fear, they're easily recreated.


Prerequisites

  1. SSH access to your NAS (click the link if you don't know how to do that)
  2. A way to recreate your containers, such as Portainer, the docker-compose.yml files for your containers stored somewhere secure, or the docker run commands you previously used
  3. Sudo/root access (normally by using the same password as any user with admin permissions)
  4. Access to DSM
💡
If you don't have either the compose or run files saved, you can use a container called docker-autocompose to create compose files from all your running containers. You can find the github repo for the project here

Backup backup backup

BEFORE we uninstall docker, backup your docker ecosystem now. We shouldn't need the backups, this is just in case. Separate to any mapped folders you may have, the following directoires also need to be backed up (assuming you installed docker in /volume1) :

  1. /volume1/@docker/volumes
  2. /volume1/@docker/network
  3. /volume1/@appstore/Docker
  • We back up volumes as it has all of the data which our containers created which wasn't mapped to the local drive
  • We back up network as it stores the database of all our docker networks
  • We back up the appstore Docker folder just in case. I don't expect we'll need to use it

Also make sure as previously stated that you know how to recreate your containers.


Uninstalling docker

Now we're ready to begin the process.

  • Log in to DSM and stop, then uninstall, your docker package (it's up to you if you want to stop your containers first)
  • In the pop up, DO NOT tick the box next to 'Delete the items listed above...'. We still want the docker shared folder (at least for now)

Reinstalling Docker

You did it before, so I'm assuming you can do it again, however this time:

  • In the pop up modal just after you click Install, make sure you click the Volume drop down box and select the volume you want to install it in. For my purposes I'm going to assume moving forward it is volume2

The docker target is automatically stored in /var/packages/Docker/target, but this isn't a file you can change. Just reinstalling the docker package doesn't change this file either, so we need to do a few things (again assuming your new docker package is on volume2:

  • SSH into your machine with an admin-level user
  • Type sudo -i to get root access and then type in your password when prompted
  • Check that docker is using /volume2 by typing docker info. The output should include a line Docker Root Dir: /volume2/@docker
  • To repoint your docker target to volume2, copy paste the following
ln -s /volume2/@appstore/Docker /var/packages/Docker/target
You won't get an output from this command

Recreating your docker networks

This should be as simple as copying across your network directory we backed up earlier:

rsync -avh /volume1/@docker/network /volume2/@docker/
I prefer this rsync command as it keeps all source directory permissions, and shows a verbose output

Now if you type a docker network inspect command, you should get an ouput which includes your previous networks.


Recreating your containers

You should now be able to recreate your containers in exactly the same way as before, without having to move any of your container config folders.

But what about the volumes created by the containers?

  • Spin up the container which has a created volume
  • When the container is running, copy the relevant directory in /volume1/@docker/volumes to overwrite the one that was created in /volume2/@docker/volumes
💡
For instance, if I have an authentik_volume created by the container, I will copy (with permissions) the same folder across from volume1 using the following command:
rsync -avh /volume1/@docker/volumes/authentik_volume /volume2/@docker/volumes/

Hint: to copy across more than one folder, you can use { } brackets in the following format:

rsync -avh /volume1/@docker/volumes/{folder1,folder2,folder3} /volume2/@docker/volumes/

What about all my container config data still on volume1?

It's now up to you if you want to move none, some or all of your config and data folders across to volume2. Just remember that if you do, you will need to modify any of your docker-compose or .env files to mirror those changes, otherwise the next time you try and update the image you'll run into problems.

Similarly, make sure you use a command which keeps all source folder permissions (like the rsync -av commands above) so that you don't run into any issues.


Relacted Articles

Create an M.2 NVMe SSD volume on your older Synology NAS
Disappointed that the newer x23 Synology units have M.2 volume support and your older model doesn’t? Here’s a way to game the system
Preshared Keys and more - SSH on Linux and Synology
An easy guide which explains how to access and use your SSH sessions to their fullest, including clients and logging in with preshared keys
Portainer - Easy Container Management for Docker
A step-by-step docker walkthrough to installing and configuring Portainer, your one-stop container-management resource

PTS

With very limited knowledge, PTS fell down the selfhosted rabbit hole after buying his first NAS in October 2020. You can find him on the Synology discord server (click the icon below)

Have some feedback or something to add? Comments are welcome!

Please note comments should be respectful, and may be moderated