Sub topic 15
Running a Local Registry for Testing
This chapter assumes you can build and tag images, you know the registry/repository:tag naming format, and you have a working Docker installation. You will run a registry on your own machine, push an image to it, and pull it back.
Why run one locally
A local registry lets you practice docker push and docker pull without an account or network access. It also suits CI jobs and offline tests. Docker publishes the open-source registry as the registry:2 image, so you start it like any other container.
Start the registry
docker run -d -p 5000:5000 --name registry registry:2
docker tag myapp:1.0 localhost:5000/myapp:1.0
docker push localhost:5000/myapp:1.0The first command runs the registry in the background and publishes port 5000. The second gives the image a name that starts with localhost:5000. The host part of an image name tells Docker where to push. The third command uploads the layers and the manifest.
Why plain HTTP works here
Registries normally require HTTPS. Docker makes an exception for localhost, so the commands above work without TLS certificates. If you reach the registry through another hostname or IP address, Docker refuses the plain HTTP connection and reports an error about an HTTP response to an HTTPS client.
Verify the round trip
Remove the local copy, then pull it back from the registry:
docker rmi localhost:5000/myapp:1.0
docker pull localhost:5000/myapp:1.0Docker downloads the image again, which shows the registry stored it. You can also list repositories through the registry HTTP API:
curl http://localhost:5000/v2/_catalogThe response looks like {"repositories":["myapp"]}.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `connection refused` | The registry container is not running | Run `docker ps` and start it |
| `port is already allocated` | Another process uses port 5000 | Publish a different host port, such as `-p 5001:5000`, and use `localhost:5001` in image names |
| HTTP response to HTTPS client | The registry host is not `localhost` | Use `localhost`, or configure TLS or an insecure-registry setting |
| Images vanish after removing the container | Data lived inside the container | Mount a volume at `/var/lib/registry` |
To keep data across container restarts and removals, add a volume:
docker run -d -p 5000:5000 --name registry \
-v registry-data:/var/lib/registry registry:2Exercise
You started a registry with -p 5001:5000. What image name do you use to push myapp:1.0 to it?
Show answer
localhost:5001/myapp:1.0. The port in the image name is the host port you published, not the container's internal port 5000.
Exercise
You remove the registry container and start a new one with the same command. Your pushed images are gone. What change prevents this?
Show answer
Mount a named volume or a host directory at /var/lib/registry, for example -v registry-data:/var/lib/registry, so the stored layers and manifests outlive the container.