https://www.docker.com/sites/default/files/oyster-registry-3.png
If you have a bunch of Docker instances (physicsal or vm) running, each time one of them pulls an image that is not present on the host, it will fetch it from the internet (DockerHub). By caching these images, you can keep the traffic within your local network and avoid egress bandwidth usage. In this article we will see how to setup docker registry as a pull through cache.
Just like any other caching works, the first time any docker client tries to pull an image it will fetch the image from Dockerhub and also store a copy on the filesystem. On any subsequent request, you get served from the local registry rather than DockerHub. It is smart enough to detect any changes to the image upstream and update the image before serving it to the client.
Let’s try this with an example. I have written a small compose file that you can run locally to test this. It requires running one container and updating the docker daemon settings.
Step 1: Run docker registry
$ git clone https://github.com/techmaniack/docker-local-cache.git$ cd docker-local-cache$ docker-compose up -d
Verify that the container is up and listening on port 5000
$ curl -I localhost:5000/ [23:40:30]HTTP/1.1 200 OKCache-Control: no-cacheDate: Mon, 29 May 2017 18:10:33 GMTContent-Type: text/plain; charset=utf-8
[The images will be cached in docker-local-cache/.data/
directory.]
Step 2: Update the docker daemon configuration
On OSX you can open docker preferences and under Daemon > Registry Mirrors add ‘http://localhost:5000’ and restart docker service.
That’s it! You have successfully installed and configured docker registry as a pull through cache.
Let’s quickly see it in action.
$ time docker pull mongoUsing default tag: latestlatest: Pulling from library/mongo...Status: Downloaded newer image for mongo:latestdocker pull mongo 0.17s user 0.07s system 0% cpu 1:11.08 total$ docker rmi mongoUntagged: mongo:latestUntagged: mongo@sha256:c4bc4644b967a4b58022a79cf5c9afcd25ed08180c958a74df57b7753cfc8649...Deleted: sha256:5d924e3b1efdcda770bc33e6d7563f5ca2411dcc6b7368c8a30356bed474ae5a$ time docker pull mongoUsing default tag: latestlatest: Pulling from library/mongo...Status: Downloaded newer image for mongo:latestdocker pull mongo 0.13s user 0.05s system 0% cpu 19.296 total
As you can see the first docker pull took 71 seconds while the second pull is completed in 19 seconds. #somuchwin