112 lines
4.0 KiB
Bash
112 lines
4.0 KiB
Bash
#!/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
|
|
# ==========================================================
|
|
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" "$*"; }
|
|
|
|
# -- Must run as root
|
|
if [[ $EUID -ne 0 ]]; then
|
|
err "This script must be run as root (use sudo)."
|
|
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."
|
|
|
|
# ==========================================================
|
|
# 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."
|
|
fi
|
|
|
|
# ==========================================================
|
|
# 7. Verify installation
|
|
# ==========================================================
|
|
info "Verifying installation…"
|
|
docker --version
|
|
docker compose version
|
|
|
|
# Start & enable Docker daemon
|
|
systemctl enable docker --now 2>/dev/null || true
|
|
|
|
# Quick smoke test
|
|
if docker run --rm hello-world &>/dev/null; then
|
|
ok "Docker is working — hello-world ran successfully."
|
|
else
|
|
warn "hello-world check skipped (no network or daemon not ready)."
|
|
fi
|
|
|
|
ok "Installation complete!"
|