public/docker-install-deb.sh

77 lines
2.4 KiB
Bash

#!/usr/bin/env bash
#RUN AS ROOT/SUDO
set -euo pipefail
# Function to print messages
info() { echo -e "\e[1;34m[INFO]\e[0m $*"; }
error() { echo -e "\e[1;31m[ERROR]\e[0m $*" >&2; }
# Check if running as root
if (( EUID != 0 )); then
error "This script must be run as root (or via sudo)."
exit 1
fi
# Detect Debian codename automatically
DEBIAN_CODENAME=$(lsb_release -cs 2>/dev/null || grep VERSION_CODENAME /etc/os-release | cut -d= -f2)
if [ -z "$DEBIAN_CODENAME" ]; then
error "Could not detect Debian codename. Please specify manually."
exit 1
fi
info "Detected Debian codename: $DEBIAN_CODENAME"
# 1. Update package index
info "Updating package index..."
apt update
# 2. Install prerequisites
info "Installing prerequisites..."
apt install -y apt-transport-https ca-certificates curl gpg lsb-release
# 3. Add Docker GPG key
info "Adding Docker GPG key..."
curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker.gpg
# 4. Add Docker repository for detected Debian version
ARCH=$(dpkg --print-architecture)
info "Adding Docker repository for architecture: $ARCH..."
echo "deb [arch=${ARCH} signed-by=/usr/share/keyrings/docker.gpg] https://download.docker.com/linux/debian ${DEBIAN_CODENAME} stable" \
> /etc/apt/sources.list.d/docker.list
# 5. Update package index again to pick up Docker repo
info "Updating package index (post repo addition)..."
apt update
# 6. Install Docker components
info "Installing Docker components..."
apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# 7. Enable and start Docker service
info "Enabling and starting Docker service..."
systemctl enable docker
systemctl start docker
# 8. Verify Docker is running
info "Verifying Docker status..."
if systemctl is-active --quiet docker; then
info "Docker is active."
else
error "Docker is not running!"
journalctl -u docker --no-pager | head -n 20
exit 1
fi
# 9. Test Docker installation
info "Running hello-world container to test Docker..."
docker run --rm hello-world
# 10. Add non-root user to Docker group (if applicable)
if [ -n "${SUDO_USER:-}" ]; then
info "Adding user '$SUDO_USER' to docker group..."
usermod -aG docker "$SUDO_USER"
info "You may need to logout/login or reboot for group changes to take effect."
fi
info "Docker installation completed successfully."