Reuse docker images and save bandwidth

Lately, Docker has been creating a lot of fuss and I decided to give it a try. The website was easy to follow and the hello world example easy to setup.

The Problem

It was great to have a container setup for me automatically with a ubuntu base image running. All this in just few seconds. Well, assuming you have a blazingly fast Internet connection to download the image in few seconds. In any case, it makes sense for anyone to be able to reuse already downloaded files on one machine for the other machines. This is what I wanted to do, it didn't make sense for me to download the image on all of my machines.

The Solution

Initially I thought “well, its just a matter of copying the image file to the other machine and you're done”. Well, I soon realized that it might not be that straightforward as it is not just one image but different “layers” of images managed by AuFS.

Luckily, Docker provides a simple solution to do this –

user@machine1:~$ sudo docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              12.04               8dbd9e392a96        9 months ago        128 MB
ubuntu              latest              8dbd9e392a96        9 months ago        128 MB
ubuntu              precise             8dbd9e392a96        9 months ago        128 MB
ubuntu              12.10               b750fe79269d        9 months ago        175.3 MB
ubuntu              quantal             b750fe79269d        9 months ago        175.3 MB

user@machine1:~$ sudo docker save ubuntu > ubuntu.tar

Now copy ubuntu.tar from machine1 to machine2 and follow these from machine2:

user@machine2:~$ cat ubuntu.tar | sudo docker load

user@machine2:~$ sudo docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              12.04               8dbd9e392a96        9 months ago        128 MB
ubuntu              latest              8dbd9e392a96        9 months ago        128 MB
ubuntu              precise             8dbd9e392a96        9 months ago        128 MB
ubuntu              12.10               b750fe79269d        9 months ago        175.3 MB
ubuntu              quantal             b750fe79269d        9 months ago        175.3 MB

Or, if you want a compressed tar,

user@machine1:~$ sudo docker save ubuntu | bzip2 > ubuntu.tar.bz2

Copy ubuntu.tar.bz2 from machine1 to machine2

user@machine2:~$ bunzip2 ubuntu.tar.bz2 –stdout | sudo docker load

Or, if your machines are connected by a network and you can use ssh,

user@machine1:~$ sudo docker save ubuntu | bzip2 | ssh  user@machine2 “cat > ubuntu.tar.bz2”

user@machine2:~$ bunzip2 ubuntu.tar.bz2 –stdout | sudo docker load

Hope that helps!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s