# How to build a private Docker registry with the website and use it


This is a note about how to build a private Docker registry.

Here is my docker compose file, I will use my docker compose file as an example to explain it.

```
version: '3.7'

services:
  registry:
    image: registry:2
    container_name: docker-registry
    labels:
      - "PROJECT=registry"
    ports:
      - 5000:5000
    networks:
      - docker-registry-network
    environment:
      - REGISTRY_STORAGE_DELETE_ENABLED=true
    volumes:
      - "/mnt/shopee/backup/registry/images:/var/lib/registry"
      - "/mnt/shopee/backup/registry/config/config.yml:/etc/docker/registry/config.yml"

registry-ui:
    image: konradkleine/docker-registry-frontend:v2
    container_name: registry-ui
    labels:
      - "PROJECT=registry"
    ports:
      - 3000:80
    networks:
      - docker-registry-network
    environment:
      - ENV_DOCKER_REGISTRY_PORT=5000
      - ENV_DOCKER_REGISTRY_HOST=registry
    depends_on:
      - registry

networks:
  docker-registry-network:
    name: docker-registry-network
    labels:
      - "PROJECT=registry"
```


### First Step：Prepare environment

* Install docker & docker compose

* Create a folder named “backup” to store image data and configuration file

### 2nd Step：Add registry to docker compose file

The environment variable `REGISTRY_STORAGE_DELETE_ENABLED=true` is telling registry open to support deleting the image. Too many deprecated images will cause storage problems. It won’t be a good thing. So I open the support.

The volumes mount two positions.

First `/backup/registry/images:/var/lib/registry` is the position where image store.

Second`/mnt/shopee/backup/registry/config/config.yml:/etc/docker/registry/config.yml `is the configuration file. It’s very useful when you need to move the server and don’t want to set up registry again. Then you just need to copy this file.

These two files are the most important things what I want to back up. So I mount these to my local.

```
registry:
    image: registry:2
    container_name: docker-registry
    labels:
      - "PROJECT=registry"
    ports:
      - 5000:5000
    networks:
      - docker-registry-network
    environment:
      - REGISTRY_STORAGE_DELETE_ENABLED=true
    volumes:
      - "/mnt/shopee/backup/registry/images:/var/lib/registry"
      - "/mnt/shopee/backup/registry/config/config.yml:/etc/docker/registry/config.yml"
```


### 3rd Step：Add UI Website for registry

Docker’s registry image doesn’t provide the website. I chose konradkleine’s image to my website. You could choose anyone’s image, it’s fine.

The environment `ENV_DOCKER_REGISTRY_PORT` & `ENV_DOCKER_REGISTRY_HOST` tell the website where to connect to the registry. Because my registry & website are on the same network. I could connect it with the service name.

```
registry-ui:
    image: konradkleine/docker-registry-frontend:v2
    container_name: registry-ui
    labels:
      - "PROJECT=registry"
    ports:
      - 3000:80
    networks:
      - docker-registry-network
    environment:
      - ENV_DOCKER_REGISTRY_PORT=5000
      - ENV_DOCKER_REGISTRY_HOST=registry
    depends_on:
      - registry
```


### 4rd Step：Start

Run the command to start website & registry

```
$ docker-compose -f docker-compose.registry.yml up -d
```


Then you could access website localhost:3000 to see your registry

## Learn how to use it

Push & Pull the image

```
# You could pull image from docker hub
$ docker pull redis:latest

# localhost:5000 is the registry link
# demo:redis -- just like docker hub name:tag
$ docker tags redis localhost:5000/demo:redis

# Push image to private registry
# Then you could see it from website
$ docker push localhost:5000/demo:redis

# Pull image from private registry
$ docker pull localhost:5000/demo:redis
```


Delete image

```
# Delete image need to get image's digest
$ curl -v localhost:5000/v2/demo/manifests/redis -H 'Accept: application/vnd.docker.distribution.manifest.v2+json'
# Then you will find a field name "Docker-Content-Digest:"
# Then use the digest value to the delete api

$ curl -X DELETE -v localhost:5000/v2/demo/manifests/sha256:8b5e24dd14cff03e0db8f372e6fd5a9a0f29af771122ef8e94917317db8c39f9
```


