docker.sh
· 7.4 KiB · Bash
Исходник
#!/bin/bash
#
# Installs Docker Engine on Debian-based Linux distributions with colored output.
# Runs apt operations quietly, showing high-level status messages.
#
# Usage:
# ./docker.sh
# bash <(curl -s https://cdn.albert.lol/docker.sh)
#
# Requires:
# - Running on a Debian-based system (Debian, Ubuntu, etc.)
# - User executing the script must have sudo privileges.
# - Internet connection.
# - A terminal that supports ANSI color codes.
# Strict mode
set -euo pipefail
# --- Configuration ---
readonly DOCKER_GPG_KEY_PATH="/etc/apt/keyrings/docker.asc"
readonly DOCKER_APT_SOURCE_PATH="/etc/apt/sources.list.d/docker.list"
# --- Colors ---
# Reference: https://misc.flogisoft.com/bash/tip_colors_and_formatting
readonly COLOR_RESET='\033[0m'
readonly COLOR_RED='\033[0;31m'
readonly COLOR_GREEN='\033[0;32m'
readonly COLOR_YELLOW='\033[0;33m'
readonly COLOR_BLUE='\033[0;34m'
readonly COLOR_BOLD='\033[1m'
# --- Helper Functions ---
# Logs an informational message (Blue).
# Arguments: $*: Message to log.
log_info() {
echo -e "${COLOR_BLUE}[INFO]${COLOR_RESET} $*"
}
# Logs a success message (Green).
# Arguments: $*: Message to log.
log_success() {
echo -e "${COLOR_GREEN}[SUCCESS]${COLOR_RESET} $*"
}
# Logs a warning message (Yellow).
# Arguments: $*: Message to log.
log_warning() {
echo -e "${COLOR_YELLOW}[WARNING]${COLOR_RESET} $*"
}
# Logs an error message (Red) and exits.
# Arguments: $*: Message to log.
log_error() {
# Ensure error message goes to stderr
echo -e >&2 "${COLOR_RED}${COLOR_BOLD}[ERROR]${COLOR_RESET}${COLOR_RED} $*${COLOR_RESET}"
exit 1
}
# Checks if a command exists.
# Arguments: $1: Command name.
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# --- Main Functions ---
# Step 1: Update package lists and install prerequisite packages.
install_prerequisites() {
local pkgs=("ca-certificates" "curl" "gnupg")
log_info "Updating package list (apt-get update)..."
# Run update quietly (-qq)
if ! sudo apt-get update -qq; then
log_error "Failed to update package lists."
fi
log_info "Installing prerequisite packages: ${pkgs[*]}..."
# Run install quietly (-qq) and assume yes (-y)
if ! sudo apt-get install -y -qq "${pkgs[@]}"; then
log_error "Failed to install prerequisite packages: ${pkgs[*]}."
fi
log_success "Prerequisites installed."
}
# Step 2: Set up Docker's official APT repository.
setup_apt_repository() {
log_info "Setting up Docker's APT repository..."
# Create the directory for the GPG key if it doesn't exist
log_info "Ensuring keyring directory exists: /etc/apt/keyrings"
sudo install -m 0755 -d "$(dirname "${DOCKER_GPG_KEY_PATH}")"
# Download Docker's official GPG key
log_info "Downloading Docker GPG key to ${DOCKER_GPG_KEY_PATH}"
# Use curl with fail-fast, silent, show-error, location-follow flags
# Capture curl stderr to check for errors even with -s
local curl_stderr
if ! curl_stderr=$(sudo curl -fsSL "https://download.docker.com/linux/debian/gpg" -o "${DOCKER_GPG_KEY_PATH}" 2>&1); then
log_error "Failed to download Docker GPG key. Curl output: ${curl_stderr}"
fi
# Ensure the key is readable by apt
sudo chmod a+r "${DOCKER_GPG_KEY_PATH}"
log_success "Docker GPG key downloaded and permissions set."
# Detect architecture and OS codename
local arch
arch="$(dpkg --print-architecture)"
local codename
# Source os-release safely
if [[ -f /etc/os-release ]]; then
# shellcheck source=/dev/null
codename="$(. /etc/os-release && echo "$VERSION_CODENAME")"
else
log_error "Cannot determine OS codename. /etc/os-release not found."
fi
if [[ -z "$codename" ]]; then
log_error "Could not determine VERSION_CODENAME from /etc/os-release."
fi
# Add the Docker repository to Apt sources
log_info "Adding Docker repository source to ${DOCKER_APT_SOURCE_PATH}"
echo \
"deb [arch=${arch} signed-by=${DOCKER_GPG_KEY_PATH}] https://download.docker.com/linux/debian \
${codename} stable" | sudo tee "${DOCKER_APT_SOURCE_PATH}" > /dev/null
log_success "Docker APT repository added."
}
# Step 3: Install Docker Engine packages.
install_docker() {
local docker_pkgs=("docker-ce" "docker-ce-cli" "containerd.io" "docker-buildx-plugin" "docker-compose-plugin")
log_info "Updating package list after adding Docker repository..."
# Run update quietly (-qq)
if ! sudo apt-get update -qq; then
log_warning "Second 'apt-get update' failed. This might be okay if sources are correct, continuing installation..."
# Don't exit here, as sometimes transient network issues cause this but install might still work if cache is okay.
fi
log_info "Installing Docker packages: ${docker_pkgs[*]}..."
# Run install quietly (-qq) and assume yes (-y)
if ! sudo apt-get install -y -qq "${docker_pkgs[@]}"; then
log_error "Failed to install Docker packages: ${docker_pkgs[*]}."
fi
log_success "Docker packages installed."
}
# Step 4: Perform post-installation steps (add user to docker group).
post_install() {
log_info "Adding current user (${USER}) to the 'docker' group..."
if getent group docker > /dev/null; then
# Use GID instead of name for group check in usermod, slightly more robust
local docker_gid
docker_gid=$(getent group docker | cut -d: -f3)
if ! groups "$USER" | grep -qw "$docker_gid" && ! groups "$USER" | grep -qw "docker"; then
if sudo usermod -aG docker "$USER"; then
log_success "User '${USER}' added to the 'docker' group."
log_warning "${COLOR_BOLD}IMPORTANT:${COLOR_RESET}${COLOR_YELLOW} You must log out and log back in for this change to take effect!${COLOR_RESET}"
log_info "Alternatively, run 'newgrp docker' in your current shell (may require password)."
else
log_error "Failed to add user '${USER}' to the 'docker' group."
fi
else
log_info "User '${USER}' is already in the 'docker' group."
fi
else
log_warning "The 'docker' group does not seem to exist. Skipping adding user."
log_warning "This might indicate an issue during Docker package installation."
fi
}
# --- Main Execution ---
main() {
log_info "${COLOR_BOLD}Starting Docker installation script...${COLOR_RESET}"
# Basic checks
if ! command_exists sudo; then
log_error "'sudo' command not found. This script requires sudo privileges."
fi
# Allow running as root, but it's less common for interactive use.
# if [[ $EUID -eq 0 ]]; then
# log_warning "Running as root. It's generally recommended to run as a user with sudo privileges."
# fi
if ! command_exists dpkg || ! command_exists apt-get; then
log_error "'dpkg' or 'apt-get' not found. This script requires a Debian-based system."
fi
if ! [[ -f /etc/os-release ]]; then
log_error "File /etc/os-release not found. Cannot determine OS version. Aborting."
fi
# Execute steps
install_prerequisites
setup_apt_repository
install_docker
post_install
echo # Add a newline for spacing
log_success "-----------------------------------------------------"
log_success "${COLOR_BOLD}Docker installation completed successfully! 🎉${COLOR_RESET}"
log_warning "${COLOR_BOLD}REMEMBER TO LOG OUT AND LOG BACK IN${COLOR_RESET}${COLOR_YELLOW} to use Docker without 'sudo'.${COLOR_RESET}"
log_info "After logging back in, verify installation by running: ${COLOR_BOLD}docker run hello-world${COLOR_RESET}"
log_success "-----------------------------------------------------"
}
# Run the main function
main
| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Installs Docker Engine on Debian-based Linux distributions with colored output. |
| 4 | # Runs apt operations quietly, showing high-level status messages. |
| 5 | # |
| 6 | # Usage: |
| 7 | # ./docker.sh |
| 8 | # bash <(curl -s https://cdn.albert.lol/docker.sh) |
| 9 | # |
| 10 | # Requires: |
| 11 | # - Running on a Debian-based system (Debian, Ubuntu, etc.) |
| 12 | # - User executing the script must have sudo privileges. |
| 13 | # - Internet connection. |
| 14 | # - A terminal that supports ANSI color codes. |
| 15 | |
| 16 | # Strict mode |
| 17 | set -euo pipefail |
| 18 | |
| 19 | # --- Configuration --- |
| 20 | readonly DOCKER_GPG_KEY_PATH="/etc/apt/keyrings/docker.asc" |
| 21 | readonly DOCKER_APT_SOURCE_PATH="/etc/apt/sources.list.d/docker.list" |
| 22 | |
| 23 | # --- Colors --- |
| 24 | # Reference: https://misc.flogisoft.com/bash/tip_colors_and_formatting |
| 25 | readonly COLOR_RESET='\033[0m' |
| 26 | readonly COLOR_RED='\033[0;31m' |
| 27 | readonly COLOR_GREEN='\033[0;32m' |
| 28 | readonly COLOR_YELLOW='\033[0;33m' |
| 29 | readonly COLOR_BLUE='\033[0;34m' |
| 30 | readonly COLOR_BOLD='\033[1m' |
| 31 | |
| 32 | # --- Helper Functions --- |
| 33 | |
| 34 | # Logs an informational message (Blue). |
| 35 | # Arguments: $*: Message to log. |
| 36 | log_info() { |
| 37 | echo -e "${COLOR_BLUE}[INFO]${COLOR_RESET} $*" |
| 38 | } |
| 39 | |
| 40 | # Logs a success message (Green). |
| 41 | # Arguments: $*: Message to log. |
| 42 | log_success() { |
| 43 | echo -e "${COLOR_GREEN}[SUCCESS]${COLOR_RESET} $*" |
| 44 | } |
| 45 | |
| 46 | # Logs a warning message (Yellow). |
| 47 | # Arguments: $*: Message to log. |
| 48 | log_warning() { |
| 49 | echo -e "${COLOR_YELLOW}[WARNING]${COLOR_RESET} $*" |
| 50 | } |
| 51 | |
| 52 | # Logs an error message (Red) and exits. |
| 53 | # Arguments: $*: Message to log. |
| 54 | log_error() { |
| 55 | # Ensure error message goes to stderr |
| 56 | echo -e >&2 "${COLOR_RED}${COLOR_BOLD}[ERROR]${COLOR_RESET}${COLOR_RED} $*${COLOR_RESET}" |
| 57 | exit 1 |
| 58 | } |
| 59 | |
| 60 | # Checks if a command exists. |
| 61 | # Arguments: $1: Command name. |
| 62 | command_exists() { |
| 63 | command -v "$1" >/dev/null 2>&1 |
| 64 | } |
| 65 | |
| 66 | # --- Main Functions --- |
| 67 | |
| 68 | # Step 1: Update package lists and install prerequisite packages. |
| 69 | install_prerequisites() { |
| 70 | local pkgs=("ca-certificates" "curl" "gnupg") |
| 71 | log_info "Updating package list (apt-get update)..." |
| 72 | # Run update quietly (-qq) |
| 73 | if ! sudo apt-get update -qq; then |
| 74 | log_error "Failed to update package lists." |
| 75 | fi |
| 76 | |
| 77 | log_info "Installing prerequisite packages: ${pkgs[*]}..." |
| 78 | # Run install quietly (-qq) and assume yes (-y) |
| 79 | if ! sudo apt-get install -y -qq "${pkgs[@]}"; then |
| 80 | log_error "Failed to install prerequisite packages: ${pkgs[*]}." |
| 81 | fi |
| 82 | log_success "Prerequisites installed." |
| 83 | } |
| 84 | |
| 85 | # Step 2: Set up Docker's official APT repository. |
| 86 | setup_apt_repository() { |
| 87 | log_info "Setting up Docker's APT repository..." |
| 88 | |
| 89 | # Create the directory for the GPG key if it doesn't exist |
| 90 | log_info "Ensuring keyring directory exists: /etc/apt/keyrings" |
| 91 | sudo install -m 0755 -d "$(dirname "${DOCKER_GPG_KEY_PATH}")" |
| 92 | |
| 93 | # Download Docker's official GPG key |
| 94 | log_info "Downloading Docker GPG key to ${DOCKER_GPG_KEY_PATH}" |
| 95 | # Use curl with fail-fast, silent, show-error, location-follow flags |
| 96 | # Capture curl stderr to check for errors even with -s |
| 97 | local curl_stderr |
| 98 | if ! curl_stderr=$(sudo curl -fsSL "https://download.docker.com/linux/debian/gpg" -o "${DOCKER_GPG_KEY_PATH}" 2>&1); then |
| 99 | log_error "Failed to download Docker GPG key. Curl output: ${curl_stderr}" |
| 100 | fi |
| 101 | # Ensure the key is readable by apt |
| 102 | sudo chmod a+r "${DOCKER_GPG_KEY_PATH}" |
| 103 | log_success "Docker GPG key downloaded and permissions set." |
| 104 | |
| 105 | # Detect architecture and OS codename |
| 106 | local arch |
| 107 | arch="$(dpkg --print-architecture)" |
| 108 | local codename |
| 109 | # Source os-release safely |
| 110 | if [[ -f /etc/os-release ]]; then |
| 111 | # shellcheck source=/dev/null |
| 112 | codename="$(. /etc/os-release && echo "$VERSION_CODENAME")" |
| 113 | else |
| 114 | log_error "Cannot determine OS codename. /etc/os-release not found." |
| 115 | fi |
| 116 | |
| 117 | if [[ -z "$codename" ]]; then |
| 118 | log_error "Could not determine VERSION_CODENAME from /etc/os-release." |
| 119 | fi |
| 120 | |
| 121 | # Add the Docker repository to Apt sources |
| 122 | log_info "Adding Docker repository source to ${DOCKER_APT_SOURCE_PATH}" |
| 123 | echo \ |
| 124 | "deb [arch=${arch} signed-by=${DOCKER_GPG_KEY_PATH}] https://download.docker.com/linux/debian \ |
| 125 | ${codename} stable" | sudo tee "${DOCKER_APT_SOURCE_PATH}" > /dev/null |
| 126 | log_success "Docker APT repository added." |
| 127 | } |
| 128 | |
| 129 | # Step 3: Install Docker Engine packages. |
| 130 | install_docker() { |
| 131 | local docker_pkgs=("docker-ce" "docker-ce-cli" "containerd.io" "docker-buildx-plugin" "docker-compose-plugin") |
| 132 | log_info "Updating package list after adding Docker repository..." |
| 133 | # Run update quietly (-qq) |
| 134 | if ! sudo apt-get update -qq; then |
| 135 | log_warning "Second 'apt-get update' failed. This might be okay if sources are correct, continuing installation..." |
| 136 | # Don't exit here, as sometimes transient network issues cause this but install might still work if cache is okay. |
| 137 | fi |
| 138 | |
| 139 | log_info "Installing Docker packages: ${docker_pkgs[*]}..." |
| 140 | # Run install quietly (-qq) and assume yes (-y) |
| 141 | if ! sudo apt-get install -y -qq "${docker_pkgs[@]}"; then |
| 142 | log_error "Failed to install Docker packages: ${docker_pkgs[*]}." |
| 143 | fi |
| 144 | log_success "Docker packages installed." |
| 145 | } |
| 146 | |
| 147 | # Step 4: Perform post-installation steps (add user to docker group). |
| 148 | post_install() { |
| 149 | log_info "Adding current user (${USER}) to the 'docker' group..." |
| 150 | if getent group docker > /dev/null; then |
| 151 | # Use GID instead of name for group check in usermod, slightly more robust |
| 152 | local docker_gid |
| 153 | docker_gid=$(getent group docker | cut -d: -f3) |
| 154 | if ! groups "$USER" | grep -qw "$docker_gid" && ! groups "$USER" | grep -qw "docker"; then |
| 155 | if sudo usermod -aG docker "$USER"; then |
| 156 | log_success "User '${USER}' added to the 'docker' group." |
| 157 | log_warning "${COLOR_BOLD}IMPORTANT:${COLOR_RESET}${COLOR_YELLOW} You must log out and log back in for this change to take effect!${COLOR_RESET}" |
| 158 | log_info "Alternatively, run 'newgrp docker' in your current shell (may require password)." |
| 159 | else |
| 160 | log_error "Failed to add user '${USER}' to the 'docker' group." |
| 161 | fi |
| 162 | else |
| 163 | log_info "User '${USER}' is already in the 'docker' group." |
| 164 | fi |
| 165 | else |
| 166 | log_warning "The 'docker' group does not seem to exist. Skipping adding user." |
| 167 | log_warning "This might indicate an issue during Docker package installation." |
| 168 | fi |
| 169 | } |
| 170 | |
| 171 | # --- Main Execution --- |
| 172 | |
| 173 | main() { |
| 174 | log_info "${COLOR_BOLD}Starting Docker installation script...${COLOR_RESET}" |
| 175 | |
| 176 | # Basic checks |
| 177 | if ! command_exists sudo; then |
| 178 | log_error "'sudo' command not found. This script requires sudo privileges." |
| 179 | fi |
| 180 | # Allow running as root, but it's less common for interactive use. |
| 181 | # if [[ $EUID -eq 0 ]]; then |
| 182 | # log_warning "Running as root. It's generally recommended to run as a user with sudo privileges." |
| 183 | # fi |
| 184 | if ! command_exists dpkg || ! command_exists apt-get; then |
| 185 | log_error "'dpkg' or 'apt-get' not found. This script requires a Debian-based system." |
| 186 | fi |
| 187 | if ! [[ -f /etc/os-release ]]; then |
| 188 | log_error "File /etc/os-release not found. Cannot determine OS version. Aborting." |
| 189 | fi |
| 190 | |
| 191 | # Execute steps |
| 192 | install_prerequisites |
| 193 | setup_apt_repository |
| 194 | install_docker |
| 195 | post_install |
| 196 | |
| 197 | echo # Add a newline for spacing |
| 198 | log_success "-----------------------------------------------------" |
| 199 | log_success "${COLOR_BOLD}Docker installation completed successfully! 🎉${COLOR_RESET}" |
| 200 | log_warning "${COLOR_BOLD}REMEMBER TO LOG OUT AND LOG BACK IN${COLOR_RESET}${COLOR_YELLOW} to use Docker without 'sudo'.${COLOR_RESET}" |
| 201 | log_info "After logging back in, verify installation by running: ${COLOR_BOLD}docker run hello-world${COLOR_RESET}" |
| 202 | log_success "-----------------------------------------------------" |
| 203 | } |
| 204 | |
| 205 | # Run the main function |
| 206 | main |