Transferring containers

One question I had myself already, but was also asked on twitter after my very first Docker post about cloning a Docker named data volume on one computer was:

"How can you migrate the data volume from one host to another?"

First of all, let me explain why I created the script from the previous post: If I look at all the documentation from Docker regarding moving data around with the data volumes, I see they use the --volumes-from option a lot. For my ideas/approaches, this functionality does not suffice as I would need to know where the data volumes are mounted in the original container. Therefore I came up with the script of the previous post, which only requires the name of the volume

Now coming back to the question, I had to add connectivity from one host to another host (e.g. to allow a data-volume from a production environment to be transferred to a development environment).

Fortunately (or unfortunately for my Docker journey.. :) ), the Last couple of days it has been the most amazing weather in Holland (If you would believe it, The Netherlands were the warmest spot in the whole of Europe...:) ), so could not spend too much time on Docker :)

The question regarding the transfer of named data volumes from one host to another was still going through my mind and today I just had to try to find a bash one-liner for this and I succeeded :)

The one-liner I use is:

docker run --rm -v <SOURCE_DATA_VOLUME_NAME>:/from alpine ash -c "cd /from ; tar -cf - . " | ssh <TARGET_HOST> 'docker run --rm -i -v <TARGET_DATA_VOLUME_NAME>:/to alpine ash -c "cd /to ; tar -xpvf - " '

The main ideas behind this one-liner are:

  • Run ash in alpine container and map the named data volume SOURCE_DATA_VOLUME_NAME to the directory /from in the container
  • Execute the command tar to tar the whole contents of the /from directory to the standard output
  • ssh to TARGET_HOST where we start ash in a alpine linux docker container with:
    • standard input redirected using -i and use the
    • TARGET_DATA_VOLUME_NAME data volume mapped to the /to directory
    • Using the tar command to untar the stream sent over ssh into the /to directory

Note that this one-liner is not doing any sanity checking, but it shows the basic idea how you can achieve transferring a docker named data volume from one host to another by only using its name.

Will add a new script soon to my Docker convenience scripts repository soon.

Do you have any other ideas how to transfer docker named data volumes from one host to another? I am very interested to hear your approach!!