Skip to content

Instantly share code, notes, and snippets.

@pascencio
Last active May 17, 2018 20:45
Show Gist options
  • Save pascencio/c0b391832a733a9b0fedd1343cadeaab to your computer and use it in GitHub Desktop.
Save pascencio/c0b391832a733a9b0fedd1343cadeaab to your computer and use it in GitHub Desktop.
Docker Proxy Environment for Systemd
#!/bin/sh
cmd=${1}
docker_proxy_file="/etc/systemd/system/docker.service.d/http-proxy.conf"
# Installations instructions:
# sudo curl https://gist.githubusercontent.com/pascencio/c0b391832a733a9b0fedd1343cadeaab/raw/c38e2f43956668f82c0345aaaa927dfcea3c759e/docker-proxy-env.sh -o /usr/bin/docker-proxy-env && sudo chmod +x /usr/bin/docker-proxy-env
reload_docker(){
systemctl daemon-reload
systemctl restart docker
}
is_root(){
ID=$(id -u)
if [ ${ID} != 0 ] ;
then
echo "You must execute this command with root"
exit 1
fi
}
set_proxy_on_file(){
if [ ! -d /etc/systemd/system/docker.service.d ] ;
then
mkdir -p /etc/systemd/system/docker.service.d
touch ${docker_proxy_file}
fi
docker_proxy=${1}
docker_no_proxy=${2}
echo "[Service]" > ${docker_proxy_file}
echo "Environment=\"HTTP_PROXY=${docker_proxy}\" \"HTTPS_PROXY=${docker_proxy}\" \"http_proxy=${docker_proxy}\" \"https_proxy=${docker_proxy}\" \"no_proxy=${docker_no_proxy}\" \"NO_PROXY=${docker_no_proxy}\"" >> ${docker_proxy_file}
echo "" >> ${docker_proxy_file}
}
set_proxy(){
is_root
read -p "Proxy scheme (http): " scheme
read -p "Proxy username: " username
read -p "Proxy password: " password
read -p "Proxy host: " host
if [ "${host}X" = "X" ];
then
echo "You must set a host"
exit 1
fi
read -p "Proxy port: " port
if [ "${port}X" = "X" ];
then
echo "You must set a port"
exit 1
fi
read -p "No Proxy hosts: " no_proxy
login="${username}:${password}"
if [ login != ":" ];
then
login="${login}@"
else
login=""
fi
if [ "${scheme}http" != "http" ];
then
docker_proxy="${scheme}://${host}:${port}/"
else
docker_proxy="http://${host}:${port}/"
fi
set_proxy_on_file ${docker_proxy} ${no_proxy}
reload_docker
}
unset_proxy(){
is_root
set_proxy_on_file ""
reload_docker
}
print_help()
{
echo "Invalid argument. Usage: docker-proxy-env <argument>"
echo "Arguments:"
echo " set: Load proxy settings"
echo " unset: Clear proxy settings"
}
case $cmd in
set)
set_proxy
;;
unset)
unset_proxy
;;
*)
print_help
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment