Skip to content

Instantly share code, notes, and snippets.

@lorello
Created June 21, 2023 10:13
Show Gist options
  • Select an option

  • Save lorello/689623a6c4bbf90efd81f7cef521f661 to your computer and use it in GitHub Desktop.

Select an option

Save lorello/689623a6c4bbf90efd81f7cef521f661 to your computer and use it in GitHub Desktop.
Setup Proxy per Docker Daemon
#!/usr/bin/env bash
[[ -n $DEBUG ]] && set -x
if [[ $UID -gt 0 ]]; then
echo "Script must run as root"
exit 1
fi
proxy_address=${PROXY_SERVER:-http://127.0.0.1:3128}
# pre-cache the image of the proxy
docker pull ghcr.io/rpardini/docker-registry-proxy:0.6.2
if [[ $? -gt 0 ]]; then
echo "Cannot pull proxy image"
exit 1
fi
if [[ ! -d /etc/systemd/system ]]; then
echo "Cannot find systemd config directory, is this an Ubuntu server with systemd?"
exit 1
fi
if wget -q --spider $proxy_address; then
echo "Proxy @ $proxy_address is available, starting configuration"
else
echo "Proxy @ $proxy_address seems NOT available, ending here."
exit 1
fi
if ! grep -q "docker_registry_proxy.crt" /etc/ca-certificates.conf; then
echo "Get the CA certificate from the proxy and make it a trusted root."
curl --silent ${proxy_address}/ca.crt > /usr/share/ca-certificates/docker_registry_proxy.crt
if [[ ! -s /usr/share/ca-certificates/docker_registry_proxy.crt ]]; then
echo "ERROR: downloaded an empty file from: ${proxy_address}/ca.crt"
echo "cannot continue"
exit 2
fi
echo "docker_registry_proxy.crt" >> /etc/ca-certificates.conf
update-ca-certificates --fresh
systemctl daemon-reload
systemctl restart docker.service
else
echo "The CA certificate of the trusted proxy is already trusted"
fi
if [[ ! -f /etc/systemd/system/docker.service.d/http-proxy.conf ]]; then
echo "Add environment vars pointing Docker to use the proxy: created file /etc/systemd/system/docker.service.d/http-proxy.conf"
[[ ! -d /etc/systemd/system/docker.service.d ]] && mkdir /etc/systemd/system/docker.service.d
cat << EOD > /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=$proxy_address/"
Environment="HTTPS_PROXY=$proxy_address/"
EOD
# Reload systemd
systemctl daemon-reload
# Restart dockerd
systemctl restart docker.service
else
echo "HTTP Proxy is already configured"
fi
# vim: autoindent tabstop=2 shiftwidth=2 expandtab softtabstop=2 filetype=bash fileencoding=utf-8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment