Docker Over SSH

Forbes Lindesay
ITNEXT

--

I recently had a challenge for one of my side projects. I needed to transfer a docker container that I’d built on one machine, to another machine that I could access via SSH. I didn’t want to push my container to a public docker registry, or go through the trouble of setting up my own private registry.

Using Built-in Tools

It didn’t take long to find an answer on stack overflow (https://stackoverflow.com/a/26226261/272958)

docker save <image> | bzip2 | \
ssh user@host 'bunzip2 | docker load'

Lets break this down:

  1. docker save <image> takes all the image data and serializes it, along with its tag, to a stream of binary data.
  2. docker load takes a stream of binary data and deserializes it into an image with a tag.
  3. bzip2 compresses the stream and bunzip2 decompresses the stream.
  4. ssh user@host 'some command' ssh’s into a remote host and runs the specified command.

It turns out that docker load is able to automatically decompress bzip’d content, so you can simplify the command to:

docker save <image> | bzip2 | \
ssh user@host 'docker load'

You can remove the bzip2 but docker images are often big, and the compression from bzip2 saves a massive amount…

--

--

JavaScript enthusiast and maintainer of many open source projects.