Pull-Through Cache

Introduction

Docker Hub, Quay, and other registry providers have pull limits, and costs associated with using them. Running large builds (or many small builds, frequently) may incur costs, rate limiting, or both. This guide will help you set up your own "pull-through" cache to reduce network traffic, and bypass the limitations imposed by registry providers.

What Is A Pull Through Cache?

A pull through cache is a registry mirror that contains no images. When your client checks the registry for an image, the registry will either:
  • Give an existing response from its cache; thereby avoiding egress (or a pull) from your registry,
  • Or pull the image and its metadata from the registry on your behalf; caching it for later use.

Running A Pull-Through Cache

To run a cache, you'll need the ability to deploy a persistent service, somewhere. This could be a dedicated instance with Docker installed, or a container in your Kubernetes cluster.
There are multiple ways to setup a registry -- Docker, for example, has a guide for using the registry as a pull through cache, as well as documentation for the available options, and other details under the registry image.
Documenting all the possible ways to setup a pull through cache is beyond the scope of this document; however, it does include a quick getting-started section for those who wish to run an insecure pull through cache.

Configuration & Tips

Set Up Mirror Authentication

Pull-through caches run unsecured by default. Add an htpasswd file for basic authentication, at a minimum:
1
auth:
2
htpasswd:
3
realm: basic-realm
4
path: /auth/htpasswd
Copied!

Set Up Mirror TLS

Adding TLS is also highly recommended. you can bring your own certificates, or use the built-in LetsEncrypt support:
1
http:
2
tls:
3
letsencrypt:
4
cachefile: /certs/cachefile
6
hosts: [my.cool.mirror.horse]
Copied!
The currently shipping library/registry image does not support the DNS-01 challenge yet, and some of the LetsEncrypt challenge support is getting out of date. If you need this, there is a tracking issue; We have had success by building the binary ourselves and replacing it in the image that Docker ships.

Use An Insecure Mirror

By default, Earthly expects your mirror to be using TLS. While this is not recommended, you can use an unsecured mirror by specifying the following config in the buildkit_additional_config setting:
1
global:
2
buildkit_additional_config: |
3
[registry."<upstream>"]
4
mirrors = ["<mirror>"]
5
​
6
[registry."<mirror>"]
7
http = true
8
insecure = true
Copied!
Where <mirror> is the host/port of your mirror, and <upstream> is the address of the registry you are intending to mirror.

Insecure Docker Hub Cache Example

This section contains a quick-start guide for running an insecure pull through cache using docker's registry container.
This guide assumes you are running on a trusted network with one computer acting as a server, and the other as your development workstation.
In these examples, we will assume the server has an IP set to 192.168.0.80.

Setting up the pull through cache

First connect to the server where you will be running the cache (e.g. ssh 192.168.0.80), and create a file under ~/.docker-registry-config.yml containing:
1
version: 0.1
2
log:
3
fields:
4
service: registry
5
storage:
6
cache:
7
blobdescriptor: inmemory
8
filesystem:
9
rootdirectory: /var/lib/registry
10
http:
11
addr: :5000
12
headers:
13
X-Content-Type-Options: [nosniff]
14
health:
15
storagedriver:
16
enabled: true
17
interval: 10s
18
threshold: 3
19
proxy:
20
remoteurl: https://registry-1.docker.io
21
username: [username]
22
password: [password]
Copied!
Note that you'll need to replace [username] and [password] with your dockerhub credentials.
Next, start the registry container with:
1
docker run --rm --network host -d --name docker-registry -v $HOME/.docker-registry-config.yml:/root/config.yml registry.hub.docker.com/library/registry:2 registry serve /root/config.yml
Copied!
You can then verify the registry is running by tailing logs with:
1
docker logs --follow docker-registry
Copied!
You may want to leave a second terminal window open to display the logs while you work on the following sections; this will make it more obvious when the cache is being used.
The rest of the guide focus on configuring your workstation to use this cache.

Configuring Docker to Use the Cache

To configure docker to use this cache as a mirror, edit the /etc/docker/daemon.json file, and add:
1
{
2
"registry-mirrors" : ["http://192.168.0.80:5000"]
3
}
Copied!
Then restart docker:
1
sudo service docker restart
Copied!
Next you should be able to pull an image (e.g. docker pull alpine:3.15), which should use the cache.

Verifying the Cache is Actually Working (Optional)

If you want to verify the cache is working, you can block access to dockerhub on your workstation by adding
1
0.0.0.0 index.docker.io auth.docker.io registry-1.docker.io dseasb33srnrn.cloudfront.net production.cloudflare.docker.com
Copied!
to your /etc/hosts file.
If /etc/docker/daemon.json was not correctly configured, you should see an error such as:
1
Error response from daemon: Get "https://registry-1.docker.io/v2/": dial tcp 0.0.0.0:443: connect: connection refused
Copied!
If the cache is correctly configured, the pull command should work, and you should see logs on your server under the docker-registry container:
1
192.168.0.126 - - [22/Mar/2022:19:10:39 +0000] "HEAD /v2/library/alpine/manifests/3.15 HTTP/1.1" 200 1638 "" "docker/20.10.12 go/go1.16.12 git-commit/459d0df kernel/5.13.0-35-generic os/linux arch/amd64 UpstreamClient(Docker-Client/20.10.12 \\(linux\\))"
Copied!

Configuring Earthly to Use the Cache

To configure earthly to use the cache, you must edit ~/.earthly/config.yml to include:
1
global:
2
buildkit_additional_config: |
3
[registry."docker.io"]
4
mirrors = ["192.168.0.80:5000"]
5
[registry."192.168.0.80:5000"]
6
http = true
7
insecure = true
Copied!
The next time earthly is run, it will detect the configuration change and will restart the earthly-buildkitd container to reflect these settings.
You can force these settings to be applied, and verify the mirror appears in the buildkit config by running:
1
earthly bootstrap && docker exec earthly-buildkitd cat /etc/buildkitd.toml
Copied!