Docker Swarm Offline
I work on learning new technologies during my commute. Even though I can tether from my phone, pulling 100MB Docker images is not so efficient when the train is traveling at 60 miles per hour.
Hence, I tend to work offline. I have discovered when working with Docker Swarm offline, it’s a little bit of a challenge.
In this article, I will go over one of these challenges: Docker image distribution.
Requirements
If you would like to follow along, please:
- Install Virtualbox
- Install vagrant
- Download this Vagrantfile to your local folder and modify this line:
to be:
- From the local folder where the Vagrantfile is, run:
$ vagrant up
to create virtual computers: manager and worker1.
Ensure Clean Slate
Let’s start with no images on any of the nodes connected to the swarm,
run $ docker rmi <image name>
to get rid of any images. You may need
the -f
option.
On manager node, use $ docker images
and $ docker ps
to check
there are no images or containers running:
Similarly on worker1 node:
Just to double check, use $ docker service list
to ensure no
services running on the swarm:
Load images
For this article, we are only going to work with the nginx image. On
the manager node, grab images using command: docker image pull
- the
nginx image itself is 109MB. No big deal when moving at 60mph, right?!
That’s all we will need from hub.docker.com for this article. I will work offline for the rest of the article. If you are following along and want to see the same results, find a way to detach from the Internet. Either by turning off the network from the host computer.
Running offline
With any locally stored image, (i.e. $ docker images
,) we can create
a swarm service from an image on the server when it is offline, but
each time, a warning message will appear:
image nginx:latest could not be accessed on a registry to record its digest. Each node will access nginx:latest independently, possibly leading to different nodes running different versions of the image.
For example:
This is fine, the service starts. It’s a single instance.
Let’s see what happens when we want to scale the image, since we have more than one node in our swarm, let’s use that other node:
Ok, so the service scaled and there are two instances. Which nodes are
they running on? Use docker ps
to find out:
On manager:
On worker1:
Interesting. BOTH instances of the nginx container is running on the
manager node, none are on worker1. Hmm… Let’s see what docker
service ps <service ID>
says:
On manager:
So, there were attempts to scale the service on the worker1 node, but the error message says:
“No such image: nginx:latest”
Which makes sense, there are no images on worker1.
This is a problem of working with Docker Swarm offline. For any service, all the nodes must have the image locally or else it would try to reach out to the repository, by default: https://hub.docker.com.
Without a network connection, the nodes cannot get the image and would require manually loading the image from file, using the techniques from my How to Work with Docker Images article.
With a small swarm, it’s easy to manually load images. When there are more, then it becomes tedious, and error prone.
Better Solution?
As always, I am on the look out for a better solution and have found one that works well, hint: it’s setting up a local Docker Registry.
I will go over a solution in Docker Swarm and how to interact with it.
Stay tuned!