Migrating Docker Images
I’ve recently become enthralled with containerization. For the uninitiated, the very basic idea behind containerization is to take everything you might need to run an application or other piece of software and package it all into a nice little bundle, then run that bundle inside the software equivalent of a shipping container. Then you can run that container one whatever system you’d like without ever worrying about missing dependencies or incompatible environments. Obviously, it’s a bit more complicated than that, but I think that description will do for now. There are a few different tools for accomplishing this process, but the most popular one by far is Docker.
So rather than developing some software application on my laptop, running it there for testing, and then having to do a bunch of backend setup and configuration steps to try to make sure that when I move the software to another computer to test it out or run it in production, I can just create a “Docker image”, which acts in a similar way to a software program on a CD—it’s the starting “snapshot” of the software. To run the software on the image, you create a container from the image and then run the container.
As I work on certain personal projects, I occasionally have the need to transfer those projects from one host computer to another, whether it’s for testing purposes or because I’m offloading something from my laptop to my home server or whatever else. Obviously this step is a pain in the butt, but once it’s done, the process of transferring the software itself is pretty straightforward—transfer the source code files and then run the software. However, it’s not quite so simple with Docker. Typically, images are stored in a
repository from which they can then be downloaded to individual host machines. There is a big central repository called the Docker Hub where there are lots of publically available images that anyone can download for free.
But what if you don’t want your image downloaded by others just yet since it’s still in development? Well, personal accounts are allowed 3 private images that can only be accessed by the owner, but that’s pretty limited, and the allowable size is quite limited as well.
To get around this, one could actually run a personal self-hosted repository, although having tried to do so once before, I wouldn’t necessarily recommend it, as it gets uber-complicated pretty quickly, especially if you want it to be secure (hint: you absolutely do).
So what’s a poor developer to do?
Turns out there’s a super easy way to transfer Docker images between hosts. First, run this command on the host where the image lives:
sudo docker save -o <path/to/output/file.tar> <image-name:tag>
Then, you simply transfer the
.tar file between computers by whatever usual means (scp or rsync are my favorites) and then run this command on the new host machine:
sudo docker load -i <path/to/input/file.tar>
You can also import the Docker image via the GUI if you’re using Docker Desktop.