Migrating Docker Volumes from Rootless to Root Environment

I recently faced the challenge of migrating a Docker volume from a rootless environment to a root environment. Due to the rootless environment, the file permissions differed significantly, which complicated the migration process. I overcame this issue by utilizing an intermediate container to pack the data, ensuring a smooth transition. Here's how I managed it:

Creating a Backup

To create a backup of your Docker volume, follow these steps:

  1. Set the Environment Variables: Start by defining the necessary environment variables for the volume name, backup file name, and save path.

    export VOLUME_NAME=volume_name # you can also provide a path
    export FILE_NAME=backup
    export SAVE_PATH=$(pwd)
    
    • VOLUME_NAME: The name of the Docker volume you want to back up. You can also specify a path if needed.
    • FILE_NAME: The desired name for your backup file.
    • SAVE_PATH: The directory where you want to save your backup file. Using $(pwd) sets this to the current working directory.
  2. Run the Backup Command: Use the Docker run command to create a compressed tarball of your volume data.

    docker run --rm --volume $VOLUME_NAME:/data -v $SAVE_PATH:/backup ubuntu tar -zcvf /backup/$FILE_NAME.tar /data
    
    • --rm: Automatically removes the container once the command completes.
    • --volume $VOLUME_NAME:/data: Mounts your specified Docker volume to the /data directory inside the container.
    • -v $SAVE_PATH:/backup: Mounts your local save path to the /backup directory inside the container.
    • ubuntu tar -zcvf /backup/$FILE_NAME.tar /data: Uses the tar command to create a compressed archive of the /data directory, saving it as backup.tar in the /backup directory.

Restoring the Backup

To restore a backup to your Docker volume, follow these steps:

  1. Set the Environment Variables: Define the necessary environment variables for the volume name and backup file name.

    export VOLUME_NAME=volume_name
    export FILE_NAME=backup
    
  2. Run the Restore Command: Use the Docker run command to extract the contents of your backup file back into the Docker volume.

    docker run --rm -v $VOLUME_NAME:/data -v $(pwd):/backup ubuntu tar xvf /backup/$FILE_NAME.tar -C /data --strip 1
    
    • --rm: Automatically removes the container once the command completes.
    • -v $VOLUME_NAME:/data: Mounts your specified Docker volume to the /data directory inside the container.
    • -v $(pwd):/backup: Mounts your local save path (where the backup file is located) to the /backup directory inside the container.
    • ubuntu tar xvf /backup/$FILE_NAME.tar -C /data --strip 1: Uses the tar command to extract the contents of the backup.tar file into the /data directory, removing the leading directory level (--strip 1).

By using these steps, I was able to easily create and restore backups of my Docker volumes. This approach ensured that my data remained intact and could be quickly restored in the new environment, regardless of the differing permissions.