Export your Docker images and run them remotely

I am assuming you already have a docker image that you now want to move to another host. In this example I have two web server containers I want to move called web1 and web2.

list docker images
docker image ls

Now we can to export image web1 to a file. This is simple using docker
save -o web1.image
However, if we pipe the output to bzip2 we can reduce the size of this image significantly.
In the image below you can see I have gone from 125mb to 42mb in size.
docker save web1 | bzip2 > web1.image.bz2
not a bad reduction especially if you are moving larger images over smaller pipes.

docker save
docker save <image> -o <exported-image>

Now you can scp the file to the remote host and then load the image into your remote docker instance. Docker supports compressed files so you don’t have to extract the bz image file.
docker load < web1.image.bz2
Next simply run your image. In this example I giving the container a name “web1”, running it in daemon mode on port 8081 which connects to port 80 on the actual container. The last web1 argument is the name of the image file.
docker run --name web1 -d -p 8081:80 web1

docker load image
A quick screenshot of what the process might look like.

Now that you see the steps we can condense these into one command which hopefully looks less like magic now and more like a neat shortcut.
docker save web2 | bzip2 | ssh user@remotehost 'bunzip2 | docker load; docker run -d -p 8082:80 web2

Leave a Reply

Your email address will not be published. Required fields are marked *