Forked from WoozyMasta/create-container-registry-proxies.sh
Created
January 27, 2025 18:55
-
-
Save dmi3mis/c8c0888fd08d6db70e8142a1a0221f01 to your computer and use it in GitHub Desktop.
Create multiplie container registry cache proxies using docker distribution in registry mirror mode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eu | |
# Listen address for all docker.io/registry instances | |
listen_address=0.0.0.0 | |
# Listen port for the first container | |
# all subsequent ports for containers will be incremented by one | |
listen_port_first=5000 | |
insecure=true | |
# Array with a list of proxied container registries | |
registries=( | |
"docker.io=registry-1.docker.io" | |
"quay.io" | |
"gcr.io" | |
"k8s.gcr.io" | |
"ghcr.io" | |
"mcr.microsoft.com" | |
"registry.gitlab.com" | |
) | |
work_dir="$(dirname "$(readlink -f "$0")")" | |
data_dir="$work_dir/containers-registry-proxy" | |
# Get container engine binary | |
if command -v podman &>/dev/null; then | |
cre=podman | |
elif command -v docker &>/dev/null; then | |
cre=docker | |
else | |
>&2 printf '\n%s\n' 'Podman or Docker not installed!' | |
exit 1 | |
fi | |
>&2 printf '\n%s\n\n' \ | |
'Add this lines to /etc/containers/registries.conf config:' | |
printf '%s\n' 'unqualified-search-registries = [' | |
printf ' "%s",\n' "${registries[@]}" | sed 's/=.*",/",/' | |
printf '%s\n\n' ']' | |
# Start Redis | |
mkdir -p "$data_dir/redis-data" | |
$cre run --rm --detach --quiet --name registry-cache-redis \ | |
--publish 6379:6379 \ | |
--volume "$data_dir/redis-data:/data" \ | |
docker.io/redis:6 redis-server --appendonly yes >/dev/null | |
# Start Distribution's | |
for i in ${registries[@]}; do | |
: "${port:=$listen_port_first}" | |
registry="${i/=*/}" | |
registry_url="${i/*=/}" | |
mkdir -p "$data_dir/$registry" | |
$cre run --rm --detach --quiet --name "registry-cache-$registry" \ | |
--publish $port:5000 \ | |
--env REGISTRY_HTTP_ADDR=0.0.0.0:5000 \ | |
--env REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY=/cache \ | |
--env REGISTRY_STORAGE_CACHE_BLOBDESCRIPTOR=redis \ | |
--env REGISTRY_PROXY_REMOTEURL=https://$registry_url \ | |
--env REGISTRY_REDIS_ADDR=$(hostname -I | cut -d' ' -f1):6379 \ | |
--env REGISTRY_LOG_LEVEL=debug \ | |
--volume "$data_dir/$registry":/cache \ | |
docker.io/registry:2 >/dev/null | |
cat <<EOF | |
[[registry]] | |
prefix = "$registry" | |
location = "$registry" | |
[[registry.mirror]] | |
prefix = "$registry" | |
location = "$listen_address:$port" | |
insecure = $insecure | |
EOF | |
port=$((port+1)) | |
done | |
>&2 printf '\n%s\n' 'Done.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment