Skip to content

Instantly share code, notes, and snippets.

@WoozyMasta
Last active March 6, 2024 08:11
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save WoozyMasta/ee80d7ad7cfb6dd787daff036a1078d2 to your computer and use it in GitHub Desktop.
Save WoozyMasta/ee80d7ad7cfb6dd787daff036a1078d2 to your computer and use it in GitHub Desktop.
Create multiplie container registry cache proxies using docker distribution in registry mirror mode
#!/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.'
@brentmjohnson
Copy link

Thanks @WoozyMasta! This helped automate redeployments of our pull-through cache mirrors. I also adapted to work with alpine / posix shell if anyone is interested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment