Skip to content

Instantly share code, notes, and snippets.

@ShahinSorkh
Last active March 26, 2024 11:08
Show Gist options
  • Save ShahinSorkh/6b7639ee65f302c5aa81d68d8116e615 to your computer and use it in GitHub Desktop.
Save ShahinSorkh/6b7639ee65f302c5aa81d68d8116e615 to your computer and use it in GitHub Desktop.
Install latest docker-ce on debian and ubuntu

Install docker bash script

Installation script for docker-ce and docker-compose on debian, ubuntu and linux mint.

Note: Since docker.com has set sanctions on IPs from Iran, I need to use a proxy.

Official way

curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

Usage

curl -fsSL https://gist.github.com/ShahinSorkh/6b7639ee65f302c5aa81d68d8116e615/raw/a62221809286877c00c612853a9f00b261dbac07/install-docker.bash >install-docker.bash
# Check the contents for security concerns:
bash install-docker.bash --dry-run
sudo bash install-docker.bash

Or if you can trust:

curl -fsSL https://gist.githubusercontent.com/ShahinSorkh/6b7639ee65f302c5aa81d68d8116e615/raw/install-docker.bash | bash -s - --dry-run
curl -fsSL https://gist.githubusercontent.com/ShahinSorkh/6b7639ee65f302c5aa81d68d8116e615/raw/install-docker.bash | sudo bash -s -

Options

  • --dry-run - If you want to see all the script would do without execution and/or test other options.
  • --proxy - If you need to use some proxy to access download.docker.com.
  • --no-proxy - If you don't need any proxy.
  • --no-compose - If you don't need docker-compose to be installed.

Docker mirror

Configs for docker-compose to serve a mirror of registry-1.docker.io on port 5001. This mirror can be used as a proxy and a local cache. And can be used as a middle proxy for a chained network.

Usage

mkdir -p /opt/dockermirror && cd /opt/dockermirror
curl -fsSL https://gist.github.com/ShahinSorkh/6b7639ee65f302c5aa81d68d8116e615/raw/b84c9b39e4542f631d21d132f0396808fa3e6e9d/docker-mirror.yml >docker-compose.yml

# Change configs to fit your needs
docker-compose up -d
version: "3.7"
services:
registry:
image: registry:2
restart: unless-stopped
ports:
- 5001:5000
dns:
- 1.1.1.1
- 1.0.0.1
#- 178.22.122.100
#- 185.51.200.2
environment:
#- REGISTRY_PROXY_REMOTEURL="https://registry.local:5000"
- REGISTRY_PROXY_REMOTEURL="https://registry-1.docker.io"
#- HTTP_PROXY=http://fodev.org:8118
#- HTTPS_PROXY=http://fodev.org:8118
#- http_proxy=http://fodev.org:8118
#- https_proxy=http://fodev.org:8118
#- NO_PROXY="localhost,127.0.0.1"
#- no_proxy="localhost,127.0.0.1"
volumes:
- ./data:/var/lib/registry
#!/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
# }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment