Files
shell-scripts/install-docker.sh

94 lines
2.6 KiB
Bash

#!/bin/bash
set -euo pipefail
# Install Docker Engine on Debian/Ubuntu using Docker's official apt repository.
# Follows the official docs:
# https://docs.docker.com/engine/install/debian/
# https://docs.docker.com/engine/install/ubuntu/
if [ "$EUID" -ne 0 ]; then
echo "Please run as root (sudo)." >&2
exit 1
fi
. /etc/os-release
case "$ID" in
debian|ubuntu) ;;
*)
echo "Unsupported distribution: $ID (only Debian and Ubuntu are supported)." >&2
exit 1
;;
esac
# Optional override for derivative distros (e.g. Kali, Linux Mint) whose
# codename doesn't match an official Docker release. Example:
# DOCKER_CODENAME=trixie ./install-docker.sh
CODENAME="${DOCKER_CODENAME:-${UBUNTU_CODENAME:-$VERSION_CODENAME}}"
ARCH="$(dpkg --print-architecture)"
case "$ARCH" in
amd64|armhf|arm64|ppc64el|s390x) ;;
*)
echo "Unsupported architecture: $ARCH" >&2
exit 1
;;
esac
export DEBIAN_FRONTEND=noninteractive
echo "==> Removing conflicting packages (if any)"
CONFLICTS="$(dpkg --get-selections \
docker.io docker-compose docker-compose-v2 docker-doc docker-buildx \
podman-docker containerd runc 2>/dev/null | awk '{print $1}')"
if [ -n "$CONFLICTS" ]; then
# shellcheck disable=SC2086
apt-get remove -y $CONFLICTS
fi
echo "==> Installing prerequisites"
apt-get update
apt-get install -y ca-certificates curl
echo "==> Adding Docker's official GPG key"
install -m 0755 -d /etc/apt/keyrings
curl -fsSL "https://download.docker.com/linux/$ID/gpg" -o /etc/apt/keyrings/docker.asc
chmod a+r /etc/apt/keyrings/docker.asc
echo "==> Adding Docker apt repository"
tee /etc/apt/sources.list.d/docker.sources >/dev/null <<EOF
Types: deb
URIs: https://download.docker.com/linux/$ID
Suites: $CODENAME
Components: stable
Architectures: $ARCH
Signed-By: /etc/apt/keyrings/docker.asc
EOF
echo "==> Installing Docker Engine"
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io \
docker-buildx-plugin docker-compose-plugin
echo "==> Enabling and starting Docker"
systemctl enable --now docker
echo "==> Verifying installation"
if docker run --rm hello-world; then
echo "hello-world ran successfully."
else
echo "Warning: could not run hello-world (check network access to Docker Hub)." >&2
fi
# Post-install: let the invoking (non-root) user run docker without sudo
if [ -n "${SUDO_USER:-}" ] && [ "$SUDO_USER" != "root" ]; then
usermod -aG docker "$SUDO_USER"
echo "Added user '$SUDO_USER' to the 'docker' group."
echo "Log out and back in (or run 'newgrp docker') for the change to take effect."
fi
echo
echo "Docker installed successfully."
echo "Check version: docker --version"
echo "Check compose: docker compose version"