How to transfer Docker volumes between your VPS and local PC

The most seamless way to use a VPS is naturally to develop and run apps on the server, keeping it running 24/7. However, working this way could be viewed as wasteful. If your server does not need to be running all the time, you can save money by developing in Docker, syncing its volume to your local device, then restoring it to a new server when needed.

The easiest way to achieve this is to make use of Docker's backup and restore functionality for volumes, download that backup to our local device, then scp/rsync it to a fresh server when we need it.

Backing up a Docker volume

Creating a volume backup with Docker may be easier than you expect. We'll install docker and create a new dummy container so that you can follow along without any risk if you wish:

sudo apt install docker.io
docker run -v /ourcontainer --name container ubuntu /bin/bash

Note that we have created a container called "container" in the /ourcontainer volume. We can now mount a local host directory, /backup, and create a backup there. We'll call it container-backup.tar:

docker run --rm --volumes-from container -v $(pwd):/backup ubuntu tar cvf /backup/container-backup.tar /ourcontainer

If you type ls, you'll see your container-backup.tar sitting in your home directory.

Transferring the Docker backup to your local PC

You have several options for transferring the backup now that you have made it -- for example, using SFTP. We'll be focusing on scp, however, as its universal and versatile.

Transferring a backup from your remote host to a local host via scp

Let's pretend we are transferring the backup from our Linux VPS server to our local Linux PC. The easiest way to achieve this is by opening terminal on your local PC and running the following command:

scp vpsusername@vps.ip.address:container-backup.tar your/backup/directory

Let's give an example:

scp root@168.100.8.181:container-backup.tar ~/backups

If you are not using SSH keys, you'll be asked to authenticate with a password, after which the transfer will take place:

container-backup.tar                                                 100%   10KB   1.0MB/s   00:00

Using SCP on a Windows PC

Windows 10 and Windows 11 now have open SSH built in, enabling easy scp transfer. However, you must enable this feature and install the OpenSSH server in an administrator PowerShell instance:

Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

Restart your PC, then run:

Start-Service sshd

If you don't want to start the sshd service every time, you can run it at startup:

Set-Service -Name sshd -StartupType 'Automatic'

Now that you've done this you can run your scp command, for example:

scp root@168.100.8.181:container-backup.tar C:\Users\ryanm\Desktop\Docs

Restoring your Docker backup

Once you have a backup, you can restore it to the same container (if your server is still running), or to a newly created container. We'll naturally be doing the latter for our use case, so let's install Docker and create a container on our new server called container-two:

sudo apt install docker.io
docker run -v /ourcontainer --name container-two ubuntu /bin/bash

To restore the backup, all we need to do is send it via scp, rsync, or ftp and then restore it. For example, you could run the following command on your local PC to transfer the file to your server via scp:

scp C:\Users\YourUsername\Desktop\container-backup.tar root@your.server.ip:~\

Running ls on your server should reveal it in your home/root directory.
Now, to unzip and restore the backup, we can run the following command:

docker run --rm --volumes-from container-two -v $(pwd):/backup ubuntu bash -c "cd /ourcontainer && tar xvf /container-backup.tar --strip 1"

That's it. The nice thing about this technique is that it offers a high degree of flexibility without needing VPS snapshots. You can work on a VPS, sync the volume to your PC and continue working, then transfer that back to the server, etc. When combined with BitLaunch's hourly billing, this allows for a flexible and low-cost development experience. Of course, you can use these commands to automate the backup and restore process to make it even more seamless.