Sub topic 15
Running a local registry for testing
This chapter assumes you can build an image, tag it, and run docker push and docker pull against a registry, as covered in the previous chapters. It also assumes you can run containers with port mappings and volumes. By the end you will run a registry on your own machine, push to it, inspect it over HTTP, and avoid the usual pitfalls.
Why run a registry locally
A registry is a server that stores image manifests and layers and serves them over an HTTP API. Docker Hub and cloud registries are implementations of that API. The open-source Distribution project provides a small implementation, published as the registry:2 image. Running it locally gives you:
- A place to practice tagging, pushing, and pulling without an account or network access.
- A way to test CI scripts, image names, and retention behavior without touching a shared registry.
- A cache or staging area on a development machine or a private network.
A local registry is a testing tool. The default setup has no authentication, no TLS, and no backups, so do not expose it to untrusted networks.
Start the registry
docker run -d \
-p 5000:5000 \
--restart unless-stopped \
-v registry-data:/var/lib/registry \
--name registry \
registry:2Each option does a specific job:
-p 5000:5000publishes the registry's API port on your host.-v registry-data:/var/lib/registrystores image data in a named volume. Without it, the data lives in the container's writable layer and disappears when you remove the container.--restart unless-stoppedbrings the registry back after a Docker restart.
Check that the API answers:
curl http://localhost:5000/v2/The response is an empty JSON object, {}. A 200 status on /v2/ is how clients detect a registry that speaks the v2 API.
On macOS, port 5000 may already be used by the AirPlay Receiver service. If the container fails to start with "address already in use", publish a different host port, for example -p 5001:5000, and use localhost:5001 in the image names below.
Push and pull
The registry host is part of the image name. To send an image to your local registry, tag it with the localhost:5000/ prefix, then push:
docker tag myapp:1.0 localhost:5000/myapp:1.0
docker push localhost:5000/myapp:1.0The tag command does not copy anything. It adds a second name to the same image ID. The push uploads the layers and the manifest to the registry.
To prove the round trip works, remove every local name for the image and pull it back:
docker rmi localhost:5000/myapp:1.0 myapp:1.0
docker pull localhost:5000/myapp:1.0You must remove both names. If myapp:1.0 still exists, the layers are already on disk and the pull has nothing to download, so it proves little.
The order matters because a manifest refers to layers by digest. The registry would be holding a broken reference if the manifest arrived first.
Why localhost works without TLS
Docker normally requires HTTPS when talking to a registry. It makes an exception for addresses in the loopback range, so localhost:5000 works over plain HTTP.
Any other hostname fails. If you push to 192.168.1.20:5000 or registry.lan:5000 without TLS, you get:
http: server gave HTTP response to HTTPS clientThere are two ways to fix this:
- Put TLS in front of the registry with a certificate the client trusts. This is the correct choice for anything shared.
- Tell the Docker daemon to accept plain HTTP for that host. Add it to
/etc/docker/daemon.jsonon the client machine, then restart the daemon:
{
"insecure-registries": ["registry.lan:5000"]
}The setting is per client machine. Every machine that pushes or pulls needs it, which is one more reason to use TLS once more than one person is involved.
Inspect what is stored
The registry API lets you look at the contents directly with curl:
curl http://localhost:5000/v2/_catalog
curl http://localhost:5000/v2/myapp/tags/listThe first call lists repository names, for example {"repositories":["myapp"]}. The second lists the tags in one repository, for example {"name":"myapp","tags":["1.0"]}.
To read the digest the registry assigned to a tag, request the manifest headers:
curl -sI \
-H "Accept: application/vnd.oci.image.manifest.v1+json" \
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
-H "Accept: application/vnd.oci.image.index.v1+json" \
http://localhost:5000/v2/myapp/manifests/1.0Look for the Docker-Content-Digest header. The Accept headers tell the registry which manifest formats you can handle. If you omit them, the registry may return an older format with a different digest than the one docker push printed.
You can then pull by that digest, as in the digest-pinning chapter:
docker pull localhost:5000/myapp@sha256:<digest>Replace <digest> with the value from the header.
Persistence and cleanup
Data persistence depends on how you started the registry:
| Setup | What survives |
|---|---|
| Named volume at `/var/lib/registry` | Container removal and recreation. The data is gone only if you remove the volume. |
| No volume | Container restarts only. Removing the container deletes all images. |
| Bind mount, for example `-v $PWD/data:/var/lib/registry` | Everything, and you can see the files on disk. |
To stop and discard the registry but keep its data:
docker stop registry
docker rm registryTo delete the data as well, remove the volume:
docker volume rm registry-dataBy default the registry does not allow deleting images through the API. A DELETE request on a manifest returns 405 Method Not Allowed. To enable it, start the container with -e REGISTRY_STORAGE_DELETE_ENABLED=true. Even then, deleting a manifest does not free disk space. The layer files remain until you run the registry's garbage collector, for example docker exec registry registry garbage-collect /etc/docker/registry/config.yml. Run it while no pushes are in progress, since a layer uploaded before its manifest arrives looks unreferenced.
Common errors
connection refusedon push. The container is not running or the port mapping is wrong. Checkdocker psand the-poption.http: server gave HTTP response to HTTPS client. You used a non-loopback hostname without TLS. See the section above.- Pushing goes to Docker Hub instead. You forgot the registry prefix.
myapp:1.0meansdocker.io/library/myapp:1.0, so the tag must start withlocalhost:5000/. - Images vanish after recreating the container. The registry ran without a volume.
- A tag exists but the digest differs from a colleague's. Two builds of the same Dockerfile can produce different images. Compare digests, not tags.
Exercise
A teammate runs docker push 10.0.0.8:5000/myapp:1.0 against your registry container and gets "http: server gave HTTP response to HTTPS client". The same push to localhost:5000/myapp:1.0 on your own machine works. Why, and what are two ways to fix it?
Show answer
Docker allows plain HTTP only for loopback addresses such as localhost. 10.0.0.8 is not loopback, so the client insists on HTTPS, but the registry speaks HTTP. Fix it by putting TLS on the registry with a certificate the client trusts (the better choice), or by adding "10.0.0.8:5000" to "insecure-registries" in the teammate's daemon.json and restarting their Docker daemon.
Exercise
You run docker rmi localhost:5000/myapp:1.0 and then docker pull localhost:5000/myapp:1.0. The pull finishes instantly without downloading layers. Why does this not prove the registry holds your image, and how do you make the test meaningful?
Show answer
The local name myapp:1.0 still points to the same image, so the layers are still on disk and docker rmi only removed one tag. Remove every local tag for the image (docker rmi localhost:5000/myapp:1.0 myapp:1.0) before pulling, so the layers must come from the registry.