DevOps & Containers

Container Registries Explained: Store, Version and Secure Your Images

By NileVPS Team · Published · 4 min read

اقرأ المقال بالعربي

Every time you run docker pull nginx, you are talking to a container registry. A registry is the storage and distribution layer for container images: you build an image once, push it to a registry, and then any server, CI runner or Kubernetes node can pull the exact same artifact. Understanding how registries work helps you ship faster, keep deployments reproducible, and avoid leaking private code. This guide covers the core concepts and walks through running your own private registry on a VPS.

What a registry actually stores

A container image is not a single file. It is a manifest (a JSON document) that points to a config blob and a list of layers, each a compressed tarball identified by its SHA-256 digest. The registry stores these blobs and serves them over an HTTP API defined by the OCI Distribution Specification.

Because layers are content-addressed, they are deduplicated. If ten images share the same debian:bookworm-slim base, the registry keeps that base layer only once, and a client that already has it locally skips the download.

Repositories, tags and digests

An image reference looks like this:

registry.example.com/team/api:1.4.2
  • registry.example.com is the registry host (omitted means Docker Hub).
  • team/api is the repository.
  • 1.4.2 is a tag, a human-friendly, *mutable* pointer.

Tags can be moved. Someone can push a new image as 1.4.2 and every future pull gets different content. A digest is immutable:

registry.example.com/team/api@sha256:3f1c...

For production deployments, pinning by digest (or using unique tags such as a Git commit SHA) guarantees you run exactly what you tested. Avoid deploying latest.

Public, managed or self-hosted?

OptionGood forThings to watch
Public registry (e.g. Docker Hub)Open-source images, base imagesPull rate limits, public visibility
Managed private registry from a cloud providerTeams already on that cloudCost per GB and egress, vendor coupling
Self-hosted registry on your VPSFull control, predictable cost, data localityYou handle TLS, backups, cleanup

Self-hosting makes sense when you build images in your own CI, deploy to your own servers, and want images to stay close to them.

Running a private registry on a VPS

The reference implementation is the open-source registry image (CNCF Distribution). The steps below assume Docker is installed and you have a DNS name pointing at the server. If you are new to Docker, start with Docker explained for beginners.

1. Create an htpasswd file

sudo mkdir -p /opt/registry/{auth,data}
docker run --rm --entrypoint htpasswd httpd:2 -Bbn deploy 'a-long-random-password' \
  | sudo tee /opt/registry/auth/htpasswd

The registry only accepts bcrypt (-B) entries.

2. Start the registry bound to localhost

docker run -d --name registry --restart unless-stopped \
  -p 127.0.0.1:5000:5000 \
  -v /opt/registry/data:/var/lib/registry \
  -v /opt/registry/auth:/auth \
  -e REGISTRY_AUTH=htpasswd \
  -e REGISTRY_AUTH_HTPASSWD_REALM="Registry" \
  -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd \
  -e REGISTRY_STORAGE_DELETE_ENABLED=true \
  registry:2

Binding to 127.0.0.1 keeps the plain-HTTP port off the internet.

3. Put TLS in front with Nginx

server {
    listen 443 ssl;
    server_name registry.example.com;
    ssl_certificate     /etc/letsencrypt/live/registry.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/registry.example.com/privkey.pem;

    client_max_body_size 0;          # layers can be large
    chunked_transfer_encoding on;

    location /v2/ {
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_read_timeout 900;
    }
}

Obtain the certificate with Certbot, reload Nginx, and allow only port 443 (and SSH) in your firewall.

4. Push and pull

docker login registry.example.com
docker tag api:build registry.example.com/team/api:$(git rev-parse --short HEAD)
docker push registry.example.com/team/api:$(git rev-parse --short HEAD)

Docker stores credentials in ~/.docker/config.json. On CI runners, prefer a credential helper or short-lived secrets over a plaintext file.

Keeping storage under control

Deleting a tag does not free disk space; it only removes a reference. Run garbage collection to remove unreferenced blobs:

docker exec registry bin/registry garbage-collect --delete-untagged /etc/docker/registry/config.yml

Run it during a quiet window, because pushes during collection can be corrupted. Many teams stop the registry or set it read-only first. A simple retention policy, such as keeping the last 20 tags per repository plus anything currently deployed, keeps growth predictable.

Security checklist

  • Always serve the registry over TLS; never configure clients with insecure-registries in production.
  • Use separate accounts for CI (push) and servers (pull) where your auth setup allows it.
  • Scan images for known vulnerabilities in CI with an open-source scanner such as Trivy.
  • Consider signing images (for example with Cosign) and verifying signatures before deploy.
  • Back up /opt/registry/data or take regular server snapshots; it is the only copy of images you built.
  • Do not bake secrets into image layers; anyone who can pull can extract them.

A small VPS with enough disk is usually plenty for a team registry. Check /pricing for plans with the storage you need.

FAQ

Do I need a registry if I only have one server?

Not strictly. You can build directly on the server. A registry becomes valuable once you have CI building images, more than one server, or you want to roll back to an exact earlier image quickly.

Why does my push fail with "413 Request Entity Too Large"?

Your reverse proxy is limiting the request body size. In Nginx, set client_max_body_size 0; in the registry server block and reload.

Is a tag or a digest better for deployments?

A digest is immutable, so it is the safest reference. Unique tags such as a commit SHA are a practical compromise that stay readable while still identifying one specific build.

Related guides