How to Run a Private Docker Registry on Your VPS

How to Run a Private Docker Registry on Your VPS

A private docker registry vps setup keeps your images on hardware you control. Push an image from your laptop, pull it on any server, and never wonder who else can see it. Docker ships an official registry image, and a basic setup takes about half an hour: one Compose file, one Nginx site, and a password file. This guide walks through each step on Ubuntu, from the first container to regular cleanup.

Why Run a Private Docker Registry Instead of Docker Hub

Docker Hub works fine for public images, but private images sit behind a paywall once you pass the free tier, and every push and pull travels through someone else’s servers. A private registry on your own VPS removes that dependency. Your images stay on your disk, your pushes are limited only by your own bandwidth, and you can run the registry on the same box that already hosts your apps.

There are trade-offs. You own the backups, the disk space, and the uptime. If the VPS goes down, your registry goes down with it. For a small team or a personal fleet of servers, that is usually a fair deal, especially if you already back up your Docker volumes on a schedule.

Private registry on your VPS Docker Hub
Private images Unlimited, no extra charge Limited on the free plan
Storage Your VPS disk Hosted, metered by plan
Speed As fast as your server Depends on Docker Hub
Maintenance Yours: backups, updates, cleanup None
Cost Part of your existing VPS bill Free tier, then per-seat pricing (check official pricing)

What You Need Before You Start

You need a VPS running Ubuntu 22.04 or 24.04, Docker and Docker Compose installed, a domain or subdomain pointed at the server (for example registry.example.com), and ports 80 and 443 open. If Docker is not installed yet, follow the install steps in our guide to install Docker Compose on Ubuntu 24.04 first. You will also need Certbot for the TLS certificate, which you can install with apt install certbot python3-certbot-nginx.

Set Up the Registry With Docker Compose

The official image is registry:2. Create a directory for the project, then add a Compose file. The registry stores images under /var/lib/registry inside the container, so map that path to a volume on the host. The environment variable REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY tells the registry where to write.

mkdir -p ~/registry/data
cd ~/registry
services:
  registry:
    image: registry:2
    container_name: registry
    restart: unless-stopped
    environment:
      - REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY=/var/lib/registry
    volumes:
      - ./data:/var/lib/registry
    ports:
      - "127.0.0.1:5000:5000"

Binding the port to 127.0.0.1 matters. The registry itself has no TLS and no login screen at this stage, so it should never listen on a public interface. Nginx will sit in front of it and handle both. If you are new to Compose files, our Docker Compose explainer covers the syntax used here.

Start it and check that it answers locally:

docker compose up -d
curl http://127.0.0.1:5000/v2/

A reply of {} means the registry is running. That empty JSON object is the expected response from an empty registry.

Put Nginx in Front With TLS

Docker clients refuse to push to a registry over plain HTTP unless you mark it insecure, and insecure registries are a bad habit. The clean fix is a reverse proxy with a real certificate. This nginx reverse proxy docker registry setup keeps the registry itself off the public internet while serving it over TLS. Create an Nginx site for your subdomain:

server {
    listen 80;
    server_name registry.example.com;

    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        client_max_body_size 0;
    }
}

The client_max_body_size 0 line is the one most guides leave out, and it is the one that will bite you. Nginx defaults to a 1 MB request body limit, and Docker image layers are far larger than that. Without this line, pushes of any real image fail partway through with a confusing error. Setting it to 0 removes the limit for this site only.

Enable the site, then get a certificate:

ln -s /etc/nginx/sites-available/registry /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx
certbot --nginx -d registry.example.com

Certbot will add the TLS settings and a redirect from HTTP to HTTPS. Test the registry over HTTPS now:

curl https://registry.example.com/v2/

You should see {} again, this time over TLS.

Add Authentication With htpasswd

So far anyone who finds your subdomain can pull and push images. Docker registry authentication is usually basic auth backed by an htpasswd file. Generate one with the htpasswd command (from the apache2-utils package) or with a small container if you prefer not to install it:

apt install apache2-utils
htpasswd -Bbn username password > ~/registry/auth/htpasswd

The -B flag uses bcrypt, which the registry expects. Then mount the file into the container and point the registry at it with two environment variables:

services:
  registry:
    image: registry:2
    container_name: registry
    restart: unless-stopped
    environment:
      - REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY=/var/lib/registry
      - REGISTRY_AUTH=htpasswd
      - REGISTRY_AUTH_HTPASSWD_REALM=Docker Registry
      - REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd
    volumes:
      - ./data:/var/lib/registry
      - ./auth:/auth
    ports:
      - "127.0.0.1:5000:5000"

Recreate the container and confirm that anonymous requests are now rejected:

docker compose up -d
curl https://registry.example.com/v2/

This time you should get a 401 Unauthorized response. That is the correct result: the registry is asking for credentials.

Push and Pull Your First Image

Log in from any machine that needs access:

docker login registry.example.com

Tag an existing local image with the registry address, then push it. The tag format is registry-address/image-name:tag:

docker tag nginx:latest registry.example.com/nginx:latest
docker push registry.example.com/nginx:latest

On another server, the same login followed by docker pull registry.example.com/nginx:latest fetches the image. This is the whole point of a docker push private registry workflow: build once, push once, pull anywhere. It also makes moving workloads easier, since the same images are available on the new box without rebuilding them; our guide on moving Docker containers to a new VPS pairs well with this setup.

If a push fails with a TLS error, check that the certificate covers the exact subdomain you used, and that the clock on the client machine is correct. If a push fails partway through, re-check client_max_body_size in your Nginx config.

Garbage Collection and Maintenance

Deleting an image tag does not free disk space. The registry keeps the underlying layers until you run garbage collection, and it keeps untagged layers (from overwritten tags) as well. That is why a registry that nobody cleans slowly eats the disk.

Run registry garbage collection with the container stopped, or at least in read-only mode, because the collector deletes blobs while the registry might still reference them. The safe sequence:

docker compose stop registry
docker run --rm -v ~/registry/data:/var/lib/registry registry:2 garbage-collect --delete-untagged /etc/docker/registry/config.yml
docker compose start registry

The --delete-untagged flag removes layers that no tag points to. Run this monthly, or whenever disk usage climbs. Check usage with du -sh ~/registry/data before and after to see what it freed.

Two other habits keep a private docker registry vps setup healthy. First, back up the data directory on a schedule, the same way you would back up Docker volumes automatically. A registry without a backup is one disk failure away from losing every image. Second, update the image now and then with docker compose pull && docker compose up -d and check the registry release notes for breaking changes before you do.

FAQ

Can I run a private registry without a domain name?

Yes, but it is awkward. Without TLS, Docker clients need the registry added as an insecure registry in /etc/docker/daemon.json on every machine that pushes or pulls. A free certificate from Certbot with a subdomain is less work in the long run and avoids that configuration on every client.

How much disk space does a private registry need?

It depends on how many images and tags you keep. A handful of small images fits in a few gigabytes, while images with large base layers grow fast. Watch the data directory with du -sh and run garbage collection regularly, since deleted tags do not free space on their own.

Is htpasswd authentication secure enough?

For a small team or personal use behind HTTPS, yes. Bcrypt-hashed passwords over TLS are a reasonable setup. For larger teams, look at token-based authentication or an external auth service, since htpasswd has no per-user permissions and every user with a login can push to any repository.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.