From f2738921056317e53188fdc7277356f825298b7e Mon Sep 17 00:00:00 2001 From: ecss Date: Wed, 5 Aug 2026 01:54:44 -0500 Subject: [PATCH] refactor: support for debian system --- install-docker.sh | 174 +++++++++++++++++++++------------------------- 1 file changed, 78 insertions(+), 96 deletions(-) diff --git a/install-docker.sh b/install-docker.sh index b9c5542..3f388c4 100644 --- a/install-docker.sh +++ b/install-docker.sh @@ -1,111 +1,93 @@ -#!/usr/bin/env bash -# ========================================================== -# install-docker.sh -# Install latest Docker Engine + Docker Compose plugin on -# Ubuntu (20.04 / 22.04 / 24.04). Follows the official -# Docker install guide at: -# https://docs.docker.com/engine/install/ubuntu/ -# -# Run: -# sudo bash scripts/install-docker.sh -# ========================================================== +#!/bin/bash set -euo pipefail -# -- Color helpers -info() { printf "\033[34m[INFO]\033[0m %s\n" "$*"; } -ok() { printf "\033[32m[OK]\033[0m %s\n" "$*"; } -warn() { printf "\033[33m[WARN]\033[0m %s\n" "$*"; } -err() { printf "\033[31m[ERROR]\033[0m %s\n" "$*"; } +# 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/ -# -- Must run as root -if [[ $EUID -ne 0 ]]; then - err "This script must be run as root (use sudo)." - exit 1 +if [ "$EUID" -ne 0 ]; then + echo "Please run as root (sudo)." >&2 + exit 1 fi -# -- Must be Ubuntu -if ! grep -qi 'ubuntu' /etc/os-release 2>/dev/null; then - err "This script only supports Ubuntu. Detected:" - cat /etc/os-release 2>/dev/null || echo "(unknown)" - exit 1 -fi - -# ========================================================== -# 1. Uninstall old, potentially-conflicting packages -# ========================================================== -info "Removing old Docker packages (it's fine if none were installed)…" -for pkg in docker.io docker-doc docker-compose docker-compose-v2 \ - podman-docker containerd runc; do - if dpkg -s "$pkg" &>/dev/null; then - apt-get remove -y "$pkg" - fi -done -ok "Old packages removed." - -# ========================================================== -# 2. Install prerequisites -# ========================================================== -info "Installing prerequisites…" -apt-get update -qq -apt-get install -y -qq ca-certificates curl -ok "Prerequisites installed." - -# ========================================================== -# 3. Add Docker's official GPG key -# ========================================================== -info "Adding Docker GPG key…" -install -m 0755 -d /etc/apt/keyrings -curl -fsSL https://download.docker.com/linux/ubuntu/gpg \ - -o /etc/apt/keyrings/docker.asc -chmod a+r /etc/apt/keyrings/docker.asc -ok "GPG key added." - -# ========================================================== -# 4. Add Docker apt repository -# ========================================================== -info "Adding Docker apt repository…" -# Derive codename from /etc/os-release (e.g. noble, jammy, focal) . /etc/os-release -echo \ - "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] \ - https://download.docker.com/linux/ubuntu ${VERSION_CODENAME} stable" \ - | tee /etc/apt/sources.list.d/docker.list > /dev/null -apt-get update -qq -ok "Docker repository added." -# ========================================================== -# 5. Install Docker Engine + CLI + containerd + Compose plugin -# ========================================================== -info "Installing Docker Engine, CLI, containerd, and Compose plugin…" -apt-get install -y -qq docker-ce docker-ce-cli containerd.io \ - docker-buildx-plugin docker-compose-plugin -ok "Docker packages installed." +case "$ID" in + debian|ubuntu) ;; + *) + echo "Unsupported distribution: $ID (only Debian and Ubuntu are supported)." >&2 + exit 1 + ;; +esac -# ========================================================== -# 6. Optional: add current non-root user to docker group -# ========================================================== -if [[ -n "${SUDO_USER:-}" ]]; then - info "Adding user '$SUDO_USER' to the docker group…" - usermod -aG docker "$SUDO_USER" - ok "User '$SUDO_USER' added to docker group." - warn "Log out and back in (or run 'newgrp docker') for group change to take effect." +# 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 -# ========================================================== -# 7. Verify installation -# ========================================================== -info "Verifying installation…" -docker --version -docker compose version +echo "==> Installing prerequisites" +apt-get update +apt-get install -y ca-certificates curl -# Start & enable Docker daemon -systemctl enable docker --now 2>/dev/null || true +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 -# Quick smoke test -if docker run --rm hello-world &>/dev/null; then - ok "Docker is working — hello-world ran successfully." +echo "==> Adding Docker apt repository" +tee /etc/apt/sources.list.d/docker.sources >/dev/null < 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 - warn "hello-world check skipped (no network or daemon not ready)." + echo "Warning: could not run hello-world (check network access to Docker Hub)." >&2 fi -ok "Installation complete!" +# 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"