Skip to content

Instantly share code, notes, and snippets.

@am3n
Last active April 15, 2022 19:04
Show Gist options
  • Save am3n/235272ed9f8967a07b55be93209f4991 to your computer and use it in GitHub Desktop.
Save am3n/235272ed9f8967a07b55be93209f4991 to your computer and use it in GitHub Desktop.
Set docker http-proxy

put the docker-set-http-proxy.sh in a directory

run chmod +x docker-set-http-proxy.sh to access execute permission

run ./docker-set-http-proxy.sh YOUR_HTTP_PROXY_HOST YOUR_HTTP_PROXY_PORT

#!/bin/bash
# Also in case you want to add a socks proxy, you might want to add this instead: ALL_PROXY=socks5://<host>:<port>
HOST="$1"
PORT="$2"
if test -z "$HOST"
then
echo "First arg is the proxy host name & is NULL"; exit;
fi
if test -z "$PORT"
then
echo "Second arg is the proxy port number & is NULL"; exit;
fi
mkdir -p /etc/systemd/system/docker.service.d/
cd /etc/systemd/system/docker.service.d/
touch http-proxy.conf
echo "[Service]" > http-proxy.conf
echo "Environment=\"HTTP_PROXY=http://$HOST:$PORT\"" >> http-proxy.conf
echo "Environment=\"HTTPS_PROXY=http://$HOST:$PORT\"" >> http-proxy.conf
echo "Environment=\"NO_PROXY=localhost,127.0.0.0/8,docker-registry.somecorporation.com\"" >> http-proxy.conf
sudo systemctl daemon-reload
printf "\nDocker environments:\n"
echo " "$(sudo systemctl show --property Environment docker)
echo " "
while true; do
read -p "Restart docker to use proxies? (y: restart now) (n: later) " yn
case $yn in
[Yy]* ) sudo systemctl restart docker; break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment