2016-09-29 19:21:06 +00:00
---
description: Setting-up a local mirror for Docker Hub images
2016-11-10 19:54:25 +00:00
keywords: registry, on-prem, images, tags, repository, distribution, mirror, Hub, recipe, advanced
2016-11-04 22:38:40 +00:00
title: Registry as a pull through cache
2016-09-29 19:21:06 +00:00
---
2016-09-28 21:46:28 +00:00
## Use-case
2018-01-26 01:37:23 +00:00
If you have multiple instances of Docker running in your environment, such as
multiple physical or virtual machines all running Docker, each daemon goes out
to the internet and fetches an image it doesn't have locally, from the Docker
repository. You can run a local registry mirror and point all your daemons
there, to avoid this extra internet traffic.
2016-09-28 21:46:28 +00:00
2021-04-20 10:39:12 +00:00
> **Note**
>
2022-01-12 17:38:26 +00:00
> Docker Official Images are an intellectual property of Docker.
2021-04-20 10:39:12 +00:00
2016-09-28 21:46:28 +00:00
### Alternatives
2016-11-04 22:38:40 +00:00
Alternatively, if the set of images you are using is well delimited, you can
simply pull them manually and push them to a simple, local, private registry.
2016-09-28 21:46:28 +00:00
2016-11-04 22:38:40 +00:00
Furthermore, if your images are all built in-house, not using the Hub at all and
relying entirely on your local registry is the simplest scenario.
2016-09-28 21:46:28 +00:00
### Gotcha
2016-11-04 22:38:40 +00:00
It's currently not possible to mirror another private registry. Only the central
Hub can be mirrored.
2016-09-28 21:46:28 +00:00
2023-05-31 18:20:52 +00:00
The URL of a pull-through registry mirror must be the root of a domain.
No path components other than an optional trailing slash (`/`) are allowed.
The following table shows examples of allowed and disallowed mirror URLs.
| URL | Allowed |
| -------------------------------------- | ------- |
| `https://mirror.company.example` | Yes |
| `https://mirror.company.example/` | Yes |
| `https://mirror.company.example/foo` | No |
| `https://mirror.company.example#bar` | No |
| `https://mirror.company.example?baz=1` | No |
2021-04-20 10:39:12 +00:00
> **Note**
>
2021-06-04 17:45:50 +00:00
> Mirrors of Docker Hub are still subject to Docker's [fair usage policy](https://www.docker.com/pricing/resource-consumption-updates){: target="blank" rel="noopener" class=“”}.
2021-04-20 10:39:12 +00:00
2016-09-28 21:46:28 +00:00
### Solution
2018-01-26 01:37:23 +00:00
The Registry can be configured as a pull through cache. In this mode a Registry
2016-11-04 22:38:40 +00:00
responds to all normal docker pull requests but stores all content locally.
2016-09-28 21:46:28 +00:00
## How does it work?
2016-11-04 22:38:40 +00:00
The first time you request an image from your local registry mirror, it pulls
the image from the public Docker registry and stores it locally before handing
it back to you. On subsequent requests, the local registry mirror is able to
serve the image from its own storage.
2016-09-28 21:46:28 +00:00
### What if the content changes on the Hub?
2018-01-26 01:37:23 +00:00
When a pull is attempted with a tag, the Registry checks the remote to
ensure if it has the latest version of the requested content. Otherwise, it
fetches and caches the latest content.
2016-09-28 21:46:28 +00:00
### What about my disk?
2016-11-04 22:38:40 +00:00
In environments with high churn rates, stale data can build up in the cache.
2018-01-26 01:37:23 +00:00
When running as a pull through cache the Registry periodically removes old
content to save disk space. Subsequent requests for removed content causes a
2016-11-04 22:38:40 +00:00
remote fetch and local re-caching.
2016-09-28 21:46:28 +00:00
2016-11-04 22:38:40 +00:00
To ensure best performance and guarantee correctness the Registry cache should
be configured to use the `filesystem` driver for storage.
2016-09-28 21:46:28 +00:00
2017-05-15 17:44:40 +00:00
## Run a Registry as a pull-through cache
2016-09-28 21:46:28 +00:00
2016-11-04 22:38:40 +00:00
The easiest way to run a registry as a pull through cache is to run the official
Registry image.
2018-02-14 08:23:34 +00:00
At least, you need to specify `proxy.remoteurl` within `/etc/docker/registry/config.yml`
as described in the following subsection.
2016-09-28 21:46:28 +00:00
2018-01-26 01:37:23 +00:00
Multiple registry caches can be deployed over the same back-end. A single
registry cache ensures that concurrent requests do not pull duplicate data,
but this property does not hold true for a registry cache cluster.
2016-09-28 21:46:28 +00:00
2021-09-02 21:00:07 +00:00
> **Note**
>
2023-10-12 07:32:37 +00:00
> Service accounts included in the Team plan are limited to 5,000 pulls per day.
> See [Service Accounts](https://docs.docker.com/docker-hub/service-accounts/) for more details.
2021-09-02 21:00:07 +00:00
2017-05-15 17:44:40 +00:00
### Configure the cache
2016-09-28 21:46:28 +00:00
2016-11-04 22:38:40 +00:00
To configure a Registry to run as a pull through cache, the addition of a
`proxy` section is required to the config file.
2016-09-28 21:46:28 +00:00
2018-01-26 01:37:23 +00:00
To access private images on the Docker Hub, a username and password can
2016-11-04 22:38:40 +00:00
be supplied.
2016-09-28 21:46:28 +00:00
2017-05-15 17:44:40 +00:00
```yaml
proxy:
remoteurl: https://registry-1.docker.io
username: [username]
password: [password]
2020-08-28 00:07:35 +00:00
ttl: 168h
2017-05-15 17:44:40 +00:00
```
2016-09-28 21:46:28 +00:00
2017-05-15 17:44:40 +00:00
> **Warning**: If you specify a username and password, it's very important to
2018-01-26 01:37:23 +00:00
> understand that private resources that this user has access to Docker Hub is
> made available on your mirror. **You must secure your mirror** by
2017-05-15 17:44:40 +00:00
> implementing authentication if you expect these resources to stay private!
2016-09-28 21:46:28 +00:00
2018-01-26 01:37:23 +00:00
> **Warning**: For the scheduler to clean up old entries, `delete` must
2017-05-15 17:44:40 +00:00
> be enabled in the registry configuration. See
2023-10-12 07:32:37 +00:00
> [Registry Configuration](/about/configuration) for more details.
2016-10-14 21:24:14 +00:00
2017-05-15 17:44:40 +00:00
### Configure the Docker daemon
2016-09-28 21:46:28 +00:00
2017-05-15 17:44:40 +00:00
Either pass the `--registry-mirror` option when starting `dockerd` manually,
2023-10-12 07:32:37 +00:00
or edit [`/etc/docker/daemon.json` ](https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-configuration-file )
2017-10-27 19:56:04 +00:00
and add the `registry-mirrors` key and value, to make the change persistent.
2016-09-28 21:46:28 +00:00
2017-05-15 17:44:40 +00:00
```json
{
2023-05-31 18:20:52 +00:00
"registry-mirrors": ["https://mirror.company.example"]
2017-05-15 17:44:40 +00:00
}
```
2016-09-28 21:46:28 +00:00
2023-05-31 18:20:52 +00:00
> **Note**
>
> The mirror URL must be the root of the domain.
2017-10-27 19:56:04 +00:00
Save the file and reload Docker for the change to take effect.
2017-06-22 20:08:43 +00:00
2017-07-13 20:21:08 +00:00
> Some log messages that appear to be errors are actually informational messages.
>
> Check the `level` field to determine whether
2017-07-13 19:07:43 +00:00
> the message is warning you about an error or is giving you information.
> For example, this log message is informational:
>
> ```conf
2017-10-27 19:56:04 +00:00
> time="2017-06-02T15:47:37Z" level=info msg="error statting local store, serving from upstream: unknown blob" go.version=go1.7.4
2017-07-13 19:07:43 +00:00
> ```
>
> It's telling you that the file doesn't exist yet in the local cache and is
2021-04-20 10:39:12 +00:00
> being pulled from upstream.