From 9ecf2e931dfc7a9440bffd250a9c32f4d4dc6408 Mon Sep 17 00:00:00 2001 From: victor Date: Mon, 10 Jun 2024 18:42:44 +0200 Subject: [PATCH] First version with user/password auth --- .gitignore | 1 + README.md | 66 ++++++++++++++++++++++++++++++++++++++++++++- docker-compose.yaml | 25 +++++++++++++++++ registry.sh | 45 +++++++++++++++++++++++++++++++ scripts/gen_user.sh | 37 +++++++++++++++++++++++++ 5 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 docker-compose.yaml create mode 100755 registry.sh create mode 100755 scripts/gen_user.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f8892b1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +auth/htpasswd \ No newline at end of file diff --git a/README.md b/README.md index 4a7098d..fcccb33 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,67 @@ # dockerRegistry -A container image repository using docker. \ No newline at end of file +A container image repository using docker. + +## How to use + +```shell +./registry.sh +usage: ./registry.sh options + +Quickly manage a simple docker registry. + +OPTIONS: + c Create user config + s Start registry + d Stop registry + h Show help +``` + +### Create user/password +```shell +./registry.sh c +Set User name and press enter:testuser +Set Password and press enter:password + +Name: testuser +Password: password. Is this correct? (y/n)y + +User testuser added to auth/passwd +``` + +### Start registry +You must create a user/password first. + +```shell +./registry.sh s +[+] Running 1/1 + ✔ Container dockerRegistry Started +``` + + +### Stop registry + +```shell +./registry.sh d +[+] Stopping 1/1 + ✔ Container dockerRegistry Stopped +``` + +### Connect to the registry +Example assuming that the IP of the machine where the container is running is 192.168.10.50 + +```shell +docker login -u testuser -p password 192.168.10.25:5010 +``` + +### Upload an image to the registry + +```shell +docker tag hello-word 192.168.10.25:5010/hello-world:latest +docker push 192.168.10.25:5010/hello-world:latest +``` + +### Pulling an image from the registry +```shell +docker pull 192.168.10.25:5010/hello-world:latest +``` diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..3fda46d --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,25 @@ +version: "3" +services: + registry: + restart: on-failure + image: registry:2 + container_name: dockerRegistry + ports: + - 5010:5000 + environment: + - REGISTRY_AUTH=htpasswd + - REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" + - REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd + # - REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY=/data + # - REGISTRY_AUTH=token + # - REGISTRY_AUTH_TOKEN_REALM=https://localhost:5011/auth + # - REGISTRY_AUTH_TOKEN_SERVICE=Authentication + # - REGISTRY_AUTH_TOKEN_ISSUER=Example Issuer + # - REGISTRY_AUTH_TOKEN_ROOTCERTBUNDLE=/mnt/local/certs/RootCA.crt + # - REGISTRY_HTTP_TLS_CERTIFICATE=/mnt/local/certs/RootCA.crt + # - REGISTRY_HTTP_TLS_KEY=/mnt/local/certs/RootCA.key + volumes: + - ./auth:/auth + # - ".data/auth/registry/data:/mnt/registry/data" + # - "./registry-auth/certs:/mnt/local/certs" + diff --git a/registry.sh b/registry.sh new file mode 100755 index 0000000..c658f63 --- /dev/null +++ b/registry.sh @@ -0,0 +1,45 @@ +#!/bin/bash +OPTION=$1 +# Functions +usage() +{ +cat << EOF +usage: $0 options + +Quickly manage a simple docker registry. + +OPTIONS: + c Create user config + s Start registry + d Stop registry + h Show help +EOF +} + +genUser() +{ + "$PWD"/scripts/gen_user.sh +} +startContainer() +{ + docker compose up -d +} +stopContainer() +{ + docker compose stop +} + +case "$OPTION" +in + h) + usage + exit 0 + ;; + c) genUser;; + s) startContainer;; + d) stopContainer;; + *) + usage + exit 1 + ;; +esac diff --git a/scripts/gen_user.sh b/scripts/gen_user.sh new file mode 100755 index 0000000..effbd3a --- /dev/null +++ b/scripts/gen_user.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +aksUser() +{ +read -p "Set User name and press enter:" USER_NAME +} + +askPassword() +{ +read -p "Set Password and press enter:" USER_PASSWORD +} + +askConfirm() +{ +echo "Name: $USER_NAME" +read -p "Password: $USER_PASSWORD. Is this correct? (y/n)" CONFIRM +} + +setPassword() +{ +docker run --entrypoint htpasswd httpd:2 -Bbn $USER_NAME $USER_PASSWORD > auth/htpasswd +} +aksUser; +askPassword; +clear; +askConfirm; +while true; do + case $CONFIRM in + [Yy]* ) clear;setPassword; echo "User ${USER_NAME} added to auth/passwd"; break;; + [Nn]* ) askPassword;askConfirm;; + * ) echo "Please, asnwer with y/n."; askPassword;askConfirm;; + esac +done + + + +