caching with docker-registry

Docker based systems are great, but if you live in New Zealand (and in rural New Zealand, like I do), repeated docker pulls to the Internet can be a little painful. Fortunately you can configure docker-registry to locally cache.

First install docker-registry and configure it as a proxy.

# apt-get install docker-registry
# vi /etc/docker/registry/config.yml
# cat /etc/docker/registry/config.yml 
version: 0.1
log:
fields:
service: registry
storage:
cache:
blobdescriptor: inmemory
filesystem:
rootdirectory: /var/lib/docker-registry
delete:
enabled: true
http:
addr: :5000
headers:
X-Content-Type-Options: [nosniff]
health:
storagedriver:
enabled: true
interval: 10s
threshold: 3
proxy:
remoteurl: https://registry-1.docker.io
# service docker-registry restart

Now configure docker to use the proxy.

# vi /etc/docker/daemon.json 
# cat /etc/docker/daemon.json 
{
"storage-driver": "overlay2",
"registry-mirrors": ["http://localhost:5000"]
}
# service docker restart

Now when you do a docker pull, the cache will be used if the latest image is available locally.

# docker pull faucet/gauge
Using default tag: latest
latest: Pulling from faucet/gauge
Digest: sha256:26e2c56800df26deccc6a1d8377c697d7ca2a38cf2d8fc52739bb6b634028298
Status: Image is up to date for faucet/gauge:latest
docker.io/faucet/gauge:latest
# grep docker-registry /var/log/syslog|grep gauge
Nov 5 22:29:21 finf-gw docker-registry[29624]: time="2019-11-05T22:29:21Z" level=info msg="Challenge established with upstream : {https registry-1.docker.io /v2/ %!s(bool=false) } &{{{%!s(int32=0) %!s(uint32=0)} %!s(uint32=0) %!s(uint32=0) %!s(int32=0) %!s(int32=0)} map[https://registry-1.docker.io:443/v2/:[{bearer map[realm:https://auth.docker.io/token service:registry.docker.io]}]]}" go.version=go1.8.1 http.request.host="localhost:5000" http.request.id=fb8d44dd-193a-4a18-8625-35dc3ea015eb http.request.method=GET http.request.remoteaddr="[::1]:49450" http.request.uri=/v2/faucet/gauge/manifests/latest http.request.useragent="docker/19.03.4 go/go1.12.10 git-commit/9013bf583a kernel/5.3.0-19-generic os/linux arch/amd64 UpstreamClient(Docker-Client/19.03.4 (linux))" instance.id=9bdb53c3-1d48-4015-9708-8f1ddc816651 vars.name=faucet/gauge vars.reference=latest version=v2.6.2+debian
Nov 5 22:29:23 finf-gw docker-registry[29624]: time="2019-11-05T22:29:23Z" level=info msg="Adding new scheduler entry for faucet/gauge@sha256:26e2c56800df26deccc6a1d8377c697d7ca2a38cf2d8fc52739bb6b634028298 with ttl=167h59m59.999997987s" go.version=go1.8.1 instance.id=9bdb53c3-1d48-4015-9708-8f1ddc816651 version=v2.6.2+debian
Nov 5 22:29:23 finf-gw docker-registry[29624]: time="2019-11-05T22:29:23Z" level=info msg="response completed" go.version=go1.8.1 http.request.host="localhost:5000" http.request.id=fb8d44dd-193a-4a18-8625-35dc3ea015eb http.request.method=GET http.request.remoteaddr="[::1]:49450" http.request.uri=/v2/faucet/gauge/manifests/latest http.request.useragent="docker/19.03.4 go/go1.12.10 git-commit/9013bf583a kernel/5.3.0-19-generic os/linux arch/amd64 UpstreamClient(Docker-Client/19.03.4 (linux))" http.response.contenttype=application/vnd.docker.distribution.manifest.v2+json http.response.duration=3.545613795s http.response.status=200 http.response.written=1582 instance.id=9bdb53c3-1d48-4015-9708-8f1ddc816651 version=v2.6.2+debian
Nov 5 22:29:23 finf-gw docker-registry[29624]: ::1 - - [05/Nov/2019:22:29:20 +0000] "GET /v2/faucet/gauge/manifests/latest HTTP/1.1" 200 1582 "" "docker/19.03.4 go/go1.12.10 git-commit/9013bf583a kernel/5.3.0-19-generic os/linux arch/amd64 UpstreamClient(Docker-Client/19.03.4 \(linux\))"
Nov 5 22:31:51 finf-gw docker-registry[29624]: time="2019-11-05T22:31:51Z" level=info msg="response completed" go.version=go1.8.1 http.request.host="localhost:5000" http.request.id=013a90e2-11e3-44d8-8875-03aca0c54be5 http.request.method=GET http.request.remoteaddr="[::1]:49702" http.request.uri=/v2/faucet/gauge/manifests/latest http.request.useragent="docker/19.03.4 go/go1.12.10 git-commit/9013bf583a kernel/5.3.0-19-generic os/linux arch/amd64 UpstreamClient(Docker-Client/19.03.4 (linux))" http.response.contenttype=application/vnd.docker.distribution.manifest.v2+json http.response.duration=2.237893853s http.response.status=200 http.response.written=1582 instance.id=9bdb53c3-1d48-4015-9708-8f1ddc816651 version=v2.6.2+debian
Nov 5 22:31:51 finf-gw docker-registry[29624]: ::1 - - [05/Nov/2019:22:31:49 +0000] "GET /v2/faucet/gauge/manifests/latest HTTP/1.1" 200 1582 "" "docker/19.03.4 go/go1.12.10 git-commit/9013bf583a kernel/5.3.0-19-generic os/linux arch/amd64 UpstreamClient(Docker-Client/19.03.4 \(linux\))"