#!/usr/bin/env bash # shellcheck disable=SC2034 # shared helpers set variables not every script uses # Installs Docker Engine + Compose plugin, starts it on boot and adds you to the docker group. # Source: https://bianconi.tech/s/docker # Author: Luis Fernando Bianconi (https://bianconi.tech) main() { SCRIPT_URL="https://bianconi.tech/s/docker" SITE_URL="https://bianconi.tech" # ---- common helpers (shared by every script) ---- set -euo pipefail info() { printf '\033[1;34m==>\033[0m %s\n' "$*"; } ok() { printf '\033[1;32m✔\033[0m %s\n' "$*"; } warn() { printf '\033[1;33m!\033[0m %s\n' "$*" >&2; } fail() { printf '\033[1;31m✘\033[0m %s\n' "$*" >&2; exit 1; } require_root() { [ "$(id -u)" -eq 0 ] || fail "Run as root: curl -fsSL ${SCRIPT_URL} | sudo bash" } # Detects the package manager (apt or dnf) from /etc/os-release detect_os() { [ -r /etc/os-release ] || fail "Unsupported system: /etc/os-release not found" # shellcheck disable=SC1091 . /etc/os-release OS_ID="${ID:-unknown}" OS_VERSION="${VERSION_ID:-}" OS_CODENAME="${UBUNTU_CODENAME:-${VERSION_CODENAME:-}}" case "${ID:-} ${ID_LIKE:-}" in *debian*|*ubuntu*) PM=apt ;; *rhel*|*fedora*|*centos*) PM=dnf ;; *) fail "Unsupported distribution: ${PRETTY_NAME:-$OS_ID}. Supported: Ubuntu, Debian, RHEL, Rocky, AlmaLinux, Fedora" ;; esac info "Detected ${PRETTY_NAME:-$OS_ID}" } APT_UPDATED=0 pkg_install() { if [ "$PM" = apt ]; then if [ "$APT_UPDATED" -eq 0 ]; then apt-get update -qq; APT_UPDATED=1; fi DEBIAN_FRONTEND=noninteractive apt-get install -y -qq "$@" >/dev/null else dnf install -y -q --allowerasing "$@" >/dev/null fi } # Forces the next pkg_install to refresh apt lists (after adding a repository) apt_refresh() { APT_UPDATED=0; } # EPEL provides extra packages (htop, fail2ban...) on RHEL-like systems enable_epel() { [ "$PM" = dnf ] || return 0 case "$OS_ID" in fedora) return 0 ;; rhel) dnf install -y -q "https://dl.fedoraproject.org/pub/epel/epel-release-latest-${OS_VERSION%%.*}.noarch.rpm" >/dev/null ;; *) dnf install -y -q epel-release >/dev/null ;; esac } cpu_arch() { case "$(uname -m)" in x86_64|amd64) echo amd64 ;; aarch64|arm64) echo arm64 ;; *) fail "Unsupported CPU architecture: $(uname -m)" ;; esac } has_systemd() { [ -d /run/systemd/system ]; } # The user who ran sudo (empty when logged in directly as root) TARGET_USER="${SUDO_USER:-}" [ "$TARGET_USER" = root ] && TARGET_USER="" # For scripts that configure a user's home: sets USER_NAME, USER_GROUP and HOME_DIR setup_user() { USER_NAME="${TARGET_USER:-root}" HOME_DIR="$(getent passwd "$USER_NAME" | cut -d: -f6)" [ -d "$HOME_DIR" ] || fail "Home folder for $USER_NAME not found" USER_GROUP="$(id -gn "$USER_NAME")" } # Runs a command as that user, so files in their home stay theirs run_as_user() { if [ "$USER_NAME" = root ]; then bash -c "$1" else sudo -u "$USER_NAME" -H bash -c "$1" fi } TMP_DIR="$(mktemp -d)" trap 'rm -rf "$TMP_DIR"' EXIT # ---- end of common helpers ---- require_root detect_os if command -v docker >/dev/null 2>&1; then ok "Docker is already installed: $(docker --version)" else info "Installing Docker Engine" case "$OS_ID" in rocky|almalinux) # get.docker.com does not support Rocky/Alma, so use the official CentOS repository curl -fsSL https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo pkg_install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin ;; *) curl -fsSL https://get.docker.com -o "$TMP_DIR/get-docker.sh" sh "$TMP_DIR/get-docker.sh" ;; esac fi if has_systemd; then systemctl enable --now docker >/dev/null 2>&1 && ok "Docker starts automatically on boot" fi if [ -n "$TARGET_USER" ]; then usermod -aG docker "$TARGET_USER" ok "Added $TARGET_USER to the docker group. Log out and back in (or run: newgrp docker) to use docker without sudo" fi ok "Docker is ready: $(docker --version)" } main "$@"