Create a Private Docker Registry and Push/Pull Docker Images

Create a Private Docker Registry and Push/Pull Docker Images

In this guide, you will:

  1. Install Docker on an Ubuntu server
  2. Set up a private Docker registry on the Ubuntu server
  3. Create a Docker image on Windows and push it to the private registry
  4. Pull the image on the Ubuntu server and run it

1. Install Docker on Ubuntu

Install Docker using apt:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Update and install dependencies
$ sudo apt update
$ sudo apt install ca-certificates curl

# Add Docker's official GPG key
$ sudo install -m 0755 -d /etc/apt/keyrings
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo tee /etc/apt/keyrings/docker.asc > /dev/null
$ sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add Docker's repository
$ echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo \"${UBUNTU_CODENAME:-$VERSION_CODENAME}\") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
$ sudo apt update

# Install Docker Engine
$ sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

2. Install Docker Compose

Docker Compose is a tool for managing multi-container applications.

Install it via apt:

1
$ sudo apt install docker-compose

3. Create a Private Docker Registry

Generate TLS Certificates

1
2
3
4
5
6
7
8
9
$ sudo apt install apache2-utils mkcert

# Create certificate directory
$ sudo mkdir -p /var/registry/certs
$ cd /var/registry/certs

# Generate TLS certificates
$ sudo mkcert --install
$ sudo mkcert localhost

Create Basic Authentication

1
2
3
$ sudo mkdir -p /var/registry/auth
$ cd /var/registry/auth
$ sudo htpasswd -Bbn <username> <password> | sudo tee ./htpasswd > /dev/null

Create Docker Compose File

Save the following content as docker-compose.yml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
version: "3"
services:
registry:
image: registry:2
restart: always
ports:
- 5000:5000
environment:
REGISTRY_AUTH: htpasswd
REGISTRY_AUTH_HTPASSWD_PATH: /auth/htpasswd
REGISTRY_AUTH_HTPASSWD_REALM: Registry Realm
REGISTRY_HTTP_TLS_CERTIFICATE: /certs/localhost.pem
REGISTRY_HTTP_TLS_KEY: /certs/localhost-key.pem
volumes:
- /var/registry/data:/var/lib/registry
- /var/registry/certs:/certs
- /var/registry/auth:/auth

Start the Registry

1
sudo docker-compose up -d

4. Push Docker Images

1. Create Docker Image

You can follow this guide: Create an ASP.NET Core Docker Image

2. Log in to the Private Registry

1
2
3
4
$ docker login <IP>:5000 --username <USERNAME>
Password: <enter your password>

Login Succeeded

3. Tag the Image

You can use the following command to list all images:

1
$ docker images

And tag image which you want to push:

1
$ docker tag <local-image>:<tag> <IP>:5000/<your-image-name>:<tag>

4. Push the Image

1
$ docker push <IP>:5000/<your-image-name>:<tag>

5. Pull Docker Images

Check Available Repositories

1
$ curl -ik --user <username>:<password> https://localhost:5000/v2/_catalog

Pull the Image

1
2
3
4
5
6
$ docker login <IP>:5000 --username <USERNAME>
Password: <enter your password>

Login Succeeded

docker pull <IP>:5000/<your-image-name>:<tag>

6. Common Issues

x509: cannot validate certificate for [IP] because it doesn't contain any IP SANs

Solution (Ubuntu):

1
$ sudo vi /etc/docker/daemon.json

Add:

1
2
3
{
"insecure-registries": ["<IP>:5000"]
}

Then restart Docker:

1
$ sudo systemctl restart docker

x509 certificate signed by unknown authority (Windows Docker Desktop)

Go to: Docker Desktop > Settings > Docker Engine
Add the following block:

1
2
3
"insecure-registries": [
"https://<IP>:5000"
]

Then click Apply & Restart.


References