|
|
@ -1,8 +1,35 @@ |
|
|
|
#!/bin/bash |
|
|
|
|
|
|
|
## Set NO_DOCKER_RESTART to prevent restarting docker after upgrade. |
|
|
|
## Set ALLOW_DOCKER_CHANGE to allow change your docker version. |
|
|
|
## Set TARGET_DOCKER_VERSION to force a version (use cautiously!) |
|
|
|
|
|
|
|
distro=$(lsb_release -is) |
|
|
|
release=$(lsb_release -rs) |
|
|
|
|
|
|
|
if [ -z "$TARGET_DOCKER_VERSION" ]; then |
|
|
|
case "$distro" in |
|
|
|
Debian) |
|
|
|
case "$release" in |
|
|
|
10|11|12) target_version=24;; |
|
|
|
*) target_version=19;; |
|
|
|
esac |
|
|
|
;; |
|
|
|
Ubuntu) |
|
|
|
case "$release" in |
|
|
|
22.04) target_version=24;; |
|
|
|
*) target_version=19;; |
|
|
|
esac |
|
|
|
;; |
|
|
|
*) target_version=17;; |
|
|
|
esac |
|
|
|
else |
|
|
|
target_version="$TARGET_DOCKER_VERSION" |
|
|
|
fi |
|
|
|
|
|
|
|
|
|
|
|
just_installed= |
|
|
|
if ! type -p docker; then |
|
|
|
if ! type -p docker >/dev/null; then |
|
|
|
echo "Installing docker..." |
|
|
|
type -p curl >/dev/null || |
|
|
|
apt-get install -y curl </dev/null |
|
|
@ -11,37 +38,76 @@ if ! type -p docker; then |
|
|
|
just_installed=1 |
|
|
|
fi |
|
|
|
|
|
|
|
read docker_client_version docker_server_version < <( |
|
|
|
docker version --format '{{.Client.Version}} {{.Server.Version}}' |
|
|
|
) |
|
|
|
|
|
|
|
if [ -n "$just_installed" ]; then |
|
|
|
need_restart= |
|
|
|
|
|
|
|
case $(lsb_release -is) in |
|
|
|
Debian) |
|
|
|
case $(lsb_release -rs) in |
|
|
|
10) docker_version=20.10;; |
|
|
|
*) docker_version=17;; |
|
|
|
esac |
|
|
|
;; |
|
|
|
Ubuntu) |
|
|
|
case $(lsb_release -rs) in |
|
|
|
22.04) docker_version=20.10;; |
|
|
|
*) docker_version=17;; |
|
|
|
esac |
|
|
|
;; |
|
|
|
*) docker_version=17;; |
|
|
|
esac |
|
|
|
mismatch_client_server= |
|
|
|
## issue a warning if client and server versions are different |
|
|
|
if [ "$docker_client_version" != "$docker_server_version" ]; then |
|
|
|
echo "WARNING: docker client and server versions differ!" >&2 |
|
|
|
echo " client: $docker_client_version" >&2 |
|
|
|
echo " server: $docker_server_version" >&2 |
|
|
|
mismatch_client_server=1 |
|
|
|
fi |
|
|
|
|
|
|
|
if ! [[ "$(docker --version)" == "Docker version $docker_version"* ]]; then |
|
|
|
version="$(apt-cache madison docker-ce | |
|
|
|
distrib_available_version="$(apt-cache madison docker-ce | |
|
|
|
cut -f 2 -d \| | |
|
|
|
grep "$docker_version" | |
|
|
|
grep "$target_version" | |
|
|
|
head -n 1 | xargs echo)" |
|
|
|
apt-get install -y --allow-downgrades docker-ce="$version" |
|
|
|
need_restart=true |
|
|
|
|
|
|
|
cleaned_distrib_available_version="${distrib_available_version%%~*}" |
|
|
|
cleaned_distrib_available_version="${distrib_available_version%-*}" |
|
|
|
cleaned_distrib_available_version="${cleaned_distrib_available_version#*:}" |
|
|
|
if [ -z "$mismatch_client_server" ] && |
|
|
|
[[ "$docker_client_version" == "$cleaned_distrib_available_version" ]]; then |
|
|
|
## current and target versions match, no upgrade needed |
|
|
|
echo "INFO: recommended target docker version $docker_client_version already installed" >&2 |
|
|
|
echo " To set a different target version, set TARGET_DOCKER_VERSION" >&2 |
|
|
|
exit 0 |
|
|
|
fi |
|
|
|
|
|
|
|
## Should we skip upgrade? |
|
|
|
if [ -z "$just_installed" ] && [ -z "$ALLOW_DOCKER_CHANGE" ]; then |
|
|
|
if [ -n "$distrib_available_version" ]; then |
|
|
|
if [ -z "$mismatch_client_server" ]; then |
|
|
|
## get current version for docker-ce debian package installed |
|
|
|
current_debian_package_version="$(dpkg -s docker-ce | grep '^Version:' | cut -f 2 -d ' ')" |
|
|
|
echo "INFO: docker recommended version available (current: $current_debian_package_version => recommended: $distrib_available_version)" >&2 |
|
|
|
else |
|
|
|
current_docker_ce_package_version="$(dpkg -s docker-ce | grep '^Version:' | cut -f 2 -d ' ')" |
|
|
|
current_docker_ce_cli_package_version="$(dpkg -s docker-ce-cli | grep '^Version:' | cut -f 2 -d ' ')" |
|
|
|
echo "WARNING: docker recommended version available (recommended: $distrib_available_version)" >&2 |
|
|
|
echo " And your client ($current_docker_ce_cli_package_version) and server ($current_docker_ce_package_version) versions differ." >&2 |
|
|
|
fi |
|
|
|
echo " To change version, run this script with ALLOW_DOCKER_CHANGE=1" >&2 |
|
|
|
else |
|
|
|
echo "WARNING: your docker version does match target recommended version" >&2 |
|
|
|
echo " And your distribution does not package target version." >&2 |
|
|
|
echo " - Your distribution: $distro $release" >&2 |
|
|
|
echo " - Current docker version: $docker_client_version" >&2 |
|
|
|
echo " - Target recommended version: ${target_version}*" >&2 |
|
|
|
fi |
|
|
|
exit 0 |
|
|
|
fi |
|
|
|
|
|
|
|
if [ -n "$need_restart" ] && [ -z "$NO_DOCKER_RESTART" ]; then |
|
|
|
if [ -z "$distrib_available_version" ]; then |
|
|
|
echo "ERROR: no docker version matching '${target_version}*' available for your distribution" >&2 |
|
|
|
echo " Your distribution: $distro $release" >&2 |
|
|
|
echo " Available versions:" >&2 |
|
|
|
apt-cache madison docker-ce | cut -f 2 -d \| | sed -r 's/^/ -/g' >&2 |
|
|
|
echo |
|
|
|
echo " 1) You may need to contact your system administrator to upgrade this script." >&2 |
|
|
|
echo " 2) Meanwhile, if you know what you are doing, you can set yourself a target version" >&2 |
|
|
|
echo " by setting TARGET_DOCKER_VERSION." >&2 |
|
|
|
exit 1 |
|
|
|
fi |
|
|
|
|
|
|
|
apt-get install -y --allow-downgrades \ |
|
|
|
docker-ce="$distrib_available_version" \ |
|
|
|
docker-ce-cli="$distrib_available_version" </dev/null && |
|
|
|
if [ -z "$NO_DOCKER_RESTART" ]; then |
|
|
|
systemctl daemon-reload && |
|
|
|
service docker restart |
|
|
|
fi |
|
|
|
fi |
|
|
|
|