|
#!/usr/bin/env bash |
|
# vim: fdm=marker fdl=0 ts=4 sw=4 sts=4 ai si sta et |
|
|
|
#|##########################################################################|# |
|
#| |# |
|
#| BY Shahin Sorkh <[email protected]> |# |
|
#| https://gist.github.com/ShahinSorkh/6b7639ee65f302c5aa81d68d8116e615 |# |
|
#| https://www.opensource.org/licenses/MIT <19 May 2019> |# |
|
#| |# |
|
#|##########################################################################|# |
|
|
|
# USAGE: sudo bash ./install-docker.bash |
|
|
|
set -e |
|
|
|
for opt in $@; do |
|
case $opt in |
|
--dry-run) |
|
shift |
|
DRY='y' ;; |
|
--proxy) |
|
shift |
|
PROXY="$1" ;; |
|
--no-proxy) |
|
shift |
|
PROXY= ;; |
|
--no-compose) |
|
shift |
|
NO_COMPOSE='y' ;; |
|
*) shift ;; |
|
esac |
|
done |
|
|
|
error () { |
|
echo $'\e[31m''[ERR] '"$1" >&2 |
|
} |
|
|
|
sh_c () { |
|
echo $'\e[31m''>>>'$'\e[00m' "$@" |
|
if [ -z "$DRY" ]; then |
|
sh -c "$@" |
|
fi |
|
} |
|
|
|
check_gpg_fingerprint () { |
|
if [ -z "$DRY" ]; then |
|
FINGER_PRINT=($(apt-key fingerprint 0EBFCD88 | egrep -o '(\s[0-9A-F]{4}){5}')) |
|
FINGER_PRINT="${FINGER_PRINT[@]}" |
|
echo $([ "${FINGER_PRINT}" == "9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88" ] && echo y || echo n) |
|
fi |
|
} |
|
|
|
get_os_code_name () { |
|
DISTRO="$(awk -F= '/^ID=/ {print $2}' /etc/os-release)" |
|
case "$DISTRO" in |
|
debian) |
|
VERSION="$(awk -F= '/^VERSION_CODENAME=/ {print $2}' /etc/os-release)" |
|
if [ -z "$VERSION" ]; then |
|
VERSION="$(awk -F= '/^VERSION_ID=/ {print $2}' /etc/os-release | sed 's/"//g')" |
|
case "$VERSION" in |
|
8) VERSION='jessie' ;; |
|
9) VERSION='stretch' ;; |
|
10) VERSION='buster' ;; |
|
*) error 'Could not detect debian codename'; exit 101 ;; |
|
esac |
|
fi ;; |
|
linuxmint) |
|
DISTRO='ubuntu' |
|
VERSION="$(awk -F= '/^UBUNTU_CODENAME=/ {print $2}' /etc/os-release | sed 's/"//g')" ;; |
|
ubuntu) VERSION="$(awk -F= '/^VERSION_CODENAME=/ {print $2}' /etc/os-release | sed 's/"//g')" ;; |
|
*) error 'Not supported distro'; exit 102 ;; |
|
esac |
|
echo $DISTRO $VERSION |
|
} |
|
|
|
HTTP_PROXY="${PROXY-http://fodev.org:8118}" |
|
|
|
# Set apt proxy {{{ |
|
if [ -n "$HTTP_PROXY" ]; then |
|
CURL_PROXY="-x $HTTP_PROXY" |
|
|
|
echo $'\e[33m'"============================================================================="$'\e[00m' |
|
echo '>>>>' $'\e[32m''Set apt proxy'$'\e[00m' |
|
sh_c "echo \"Acquire::http::Proxy::download.docker.com \\\"$HTTP_PROXY\\\";\" >/etc/apt/apt.conf.d/00dockerproxy" |
|
sh_c "echo \"Acquire::https::Proxy::download.docker.com \\\"$HTTP_PROXY\\\";\" >>/etc/apt/apt.conf.d/00dockerproxy" |
|
else |
|
CONF="$(grep 'download.docker.com' -slir /etc/apt/apt.conf.d/ || echo '')" |
|
if [ -n "$CONF" ]; then |
|
echo $'\e[33m'"============================================================================="$'\e[00m' |
|
echo '>>>>' $'\e[32m''Remove apt proxy'$'\e[00m' |
|
sh_c "rm -f \"$CONF\"" |
|
fi |
|
fi |
|
# }}} |
|
|
|
# Clean up existing docker {{{ |
|
echo $'\e[33m'"============================================================================="$'\e[00m' |
|
echo '>>>>' $'\e[32m''Clean up existing docker if any'$'\e[00m' |
|
sh_c 'apt remove -qqy docker docker.io docker-engine containerd runc docker-compose || exit 0' |
|
if [ -z "$NO_COMPOSE" ]; then |
|
sh_c 'rm -rf /usr/bin/docker-compose /usr/local/bin/docker-compose' |
|
fi |
|
# }}} |
|
|
|
# Install prerequiesties {{{ |
|
echo $'\e[33m'"============================================================================="$'\e[00m' |
|
echo '>>>>' $'\e[32m''Install prerequiesties'$'\e[00m' |
|
sh_c 'apt update && apt install --no-install-recommends -y \ |
|
apt-transport-https ca-certificates curl gnupg-agent gpg' |
|
if [ "$?" != "0" ]; then |
|
error 'Could not install prerequisties'; exit $? |
|
fi |
|
# }}} |
|
|
|
# Import docker.com gpg key {{{ |
|
echo $'\e[33m'"============================================================================="$'\e[00m' |
|
echo '>>>>' $'\e[32m''Import docker.com gpg key'$'\e[00m' |
|
sh_c "curl $CURL_PROXY -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -" |
|
if [ "$( check_gpg_fingerprint )" == 'n' ]; then |
|
error 'GPG fingerprint does not match'; exit 100 |
|
fi |
|
# }}} |
|
|
|
# Add docker repository {{{ |
|
echo $'\e[33m'"============================================================================="$'\e[00m' |
|
echo '>>>>' $'\e[32m''Add docker repository'$'\e[00m' |
|
if ( grep 'download.docker.com/linux' /etc/apt/sources.list /etc/apt/sources.list.d -rc 2>&1 1>/dev/null ); then |
|
echo 'Repository already exists' |
|
else |
|
OS_CODE_NAME="$(get_os_code_name)" |
|
if [ -z "$OS_CODE_NAME" ]; then |
|
exit 103 |
|
fi |
|
REPO="deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/$OS_CODE_NAME stable" |
|
sh_c "echo $REPO >/etc/apt/sources.list.d/docker-ce.list" |
|
fi |
|
# }}} |
|
|
|
# Install docker-ce {{{ |
|
echo $'\e[33m'"============================================================================="$'\e[00m' |
|
echo '>>>>' $'\e[32m''Install docker-ce'$'\e[00m' |
|
sh_c 'apt update && apt install --no-install-recommends -y \ |
|
docker-ce docker-ce-cli containerd.io' |
|
if [ "$?" != "0" ]; then |
|
error 'Could not install docker-ce'; exit $? |
|
fi |
|
# }}} |
|
|
|
# Enable and edit docker service {{{ |
|
echo $'\e[33m'"============================================================================="$'\e[00m' |
|
echo '>>>>' $'\e[32m''Enable and start docker service'$'\e[00m' |
|
DOCKER_SERVICE=/lib/systemd/system/docker.service |
|
if [ -n "$HTTP_PROXY" ] && ! ( grep -c 'HTTP_PROXY' $DOCKER_SERVICE 2>&1 >/dev/null ); then |
|
ENV1="/\\[[Ss]ervice\\]/ a Environment=\"HTTP_PROXY=$HTTP_PROXY\"" |
|
ENV2="/\\[[Ss]ervice\\]/ a Environment=\"HTTPS_PROXY=$HTTP_PROXY\"" |
|
sh_c "sed -i \"$DOCKER_SERVICE\" \ |
|
-e '/\\[[Ss]ervice\\]/ a # the proxy used for outgoing connections' \ |
|
-e '/\\[[Ss]ervice\\]/ a # to exclude certain domains, uncomment and edit line below:' \ |
|
-e '/\\[[Ss]ervice\\]/ a #Environment=\"NO_PROXY=.reg1.local,.reg2.local\"' \ |
|
-e \"$ENV1\" -e \"$ENV2\"" |
|
sh_c "sed -i '/HTTPS_PROXY/G' \"$DOCKER_SERVICE\"" |
|
fi |
|
sh_c 'systemctl daemon-reload' |
|
sh_c 'systemctl enable --now docker.service' |
|
sh_c 'systemctl restart docker.service' |
|
# }}} |
|
|
|
# Install docker-compose {{{ |
|
if [ -z "$NO_COMPOSE" ]; then |
|
echo $'\e[33m'"============================================================================="$'\e[00m' |
|
echo '>>>>' $'\e[32m''Install docker-compose'$'\e[00m' |
|
sh_c 'curl -fL -o /usr/local/bin/docker-compose \ |
|
"https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)"' |
|
if [ "$?" != "0" ]; then |
|
error 'Could not download docker-compose'; exit $? |
|
fi |
|
sh_c 'chmod +x /usr/local/bin/docker-compose' |
|
fi |
|
# }}} |
|
|
|
# Verify installation {{{ |
|
echo $'\e[33m'"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"$'\e[00m' |
|
sh_c 'docker --version' |
|
echo $'\e[33m'"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"$'\e[00m' |
|
if [ -z "$NO_COMPOSE" ]; then |
|
sh_c 'docker-compose --version' |
|
echo $'\e[33m'"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"$'\e[00m' |
|
fi |
|
# }}} |