Last active 1746373589

bash <(curl -s https://cdn.albert.lol/docker.sh)

Revision 3e5db2917aff514ab3d107fa20954a39b6813f1c

docker.sh Raw
1#!/bin/bash
2#
3# Installs Docker Engine on Debian-based Linux distributions.
4#
5# Usage:
6# ./install_docker.sh
7# curl -fsSL <URL_TO_THIS_SCRIPT> | bash
8#
9# Requires:
10# - Running on a Debian-based system (Debian, Ubuntu, etc.)
11# - User executing the script must have sudo privileges.
12# - Internet connection.
13
14# Strict mode
15set -euo pipefail
16
17# --- Configuration ---
18readonly DOCKER_GPG_KEY_PATH="/etc/apt/keyrings/docker.asc"
19readonly DOCKER_APT_SOURCE_PATH="/etc/apt/sources.list.d/docker.list"
20
21# --- Helper Functions ---
22
23# Logs an informational message.
24# Arguments:
25# $*: Message to log.
26log_info() {
27 echo "[INFO] $*"
28}
29
30# Logs an error message and exits.
31# Arguments:
32# $*: Message to log.
33log_error() {
34 echo >&2 "[ERROR] $*"
35 exit 1
36}
37
38# Checks if a command exists.
39# Arguments:
40# $1: Command name.
41command_exists() {
42 command -v "$1" >/dev/null 2>&1
43}
44
45# --- Main Functions ---
46
47# Step 1: Update package lists and install prerequisite packages.
48install_prerequisites() {
49 log_info "Updating package list and installing prerequisites..."
50 sudo apt-get update
51 sudo apt-get install -y \
52 ca-certificates \
53 curl \
54 gnupg
55 log_info "Prerequisites installed."
56}
57
58# Step 2: Set up Docker's official APT repository.
59setup_apt_repository() {
60 log_info "Setting up Docker's APT repository..."
61
62 # Create the directory for the GPG key if it doesn't exist
63 log_info "Creating keyring directory: /etc/apt/keyrings"
64 sudo install -m 0755 -d "$(dirname "${DOCKER_GPG_KEY_PATH}")"
65
66 # Download Docker's official GPG key
67 log_info "Downloading Docker GPG key to ${DOCKER_GPG_KEY_PATH}"
68 # Use curl with fail-fast, silent, show-error, location-follow flags
69 sudo curl -fsSL "https://download.docker.com/linux/debian/gpg" -o "${DOCKER_GPG_KEY_PATH}"
70 # Ensure the key is readable by apt
71 sudo chmod a+r "${DOCKER_GPG_KEY_PATH}"
72
73 # Detect architecture and OS codename
74 local arch
75 arch="$(dpkg --print-architecture)"
76 local codename
77 # Source os-release safely
78 if [[ -f /etc/os-release ]]; then
79 # shellcheck source=/dev/null
80 codename="$(. /etc/os-release && echo "$VERSION_CODENAME")"
81 else
82 log_error "Cannot determine OS codename. /etc/os-release not found."
83 fi
84
85 if [[ -z "$codename" ]]; then
86 log_error "Could not determine VERSION_CODENAME from /etc/os-release."
87 fi
88
89 # Add the Docker repository to Apt sources
90 log_info "Adding Docker repository source to ${DOCKER_APT_SOURCE_PATH}"
91 echo \
92 "deb [arch=${arch} signed-by=${DOCKER_GPG_KEY_PATH}] https://download.docker.com/linux/debian \
93 ${codename} stable" | sudo tee "${DOCKER_APT_SOURCE_PATH}" > /dev/null
94
95 log_info "Docker APT repository setup complete."
96}
97
98# Step 3: Install Docker Engine packages.
99install_docker() {
100 log_info "Updating package list after adding Docker repository..."
101 sudo apt-get update
102
103 log_info "Installing Docker packages..."
104 sudo apt-get install -y \
105 docker-ce \
106 docker-ce-cli \
107 containerd.io \
108 docker-buildx-plugin \
109 docker-compose-plugin
110 log_info "Docker packages installed."
111}
112
113# Step 4: Perform post-installation steps (add user to docker group).
114post_install() {
115 log_info "Adding current user (${USER}) to the 'docker' group..."
116 if getent group docker > /dev/null; then
117 sudo usermod -aG docker "$USER"
118 log_info "User '${USER}' added to the 'docker' group."
119 log_info "IMPORTANT: You need to log out and log back in for the group change to take effect."
120 log_info "Alternatively, you can run 'newgrp docker' in your current shell, but logging out is recommended."
121 else
122 log_info "The 'docker' group does not exist. Skipping user addition."
123 log_info "This might happen if Docker installation failed earlier."
124 fi
125}
126
127# --- Main Execution ---
128
129main() {
130 log_info "Starting Docker installation script..."
131
132 # Basic checks
133 if ! command_exists sudo; then
134 log_error "'sudo' command not found. This script requires sudo privileges."
135 fi
136 if [[ $EUID -eq 0 ]]; then
137 log_info "Script is running as root. Using sudo is recommended for specific commands only."
138 # Consider adapting script if root execution should behave differently,
139 # but for now, relying on internal sudo calls is fine.
140 fi
141 if ! command_exists dpkg; then
142 log_error "'dpkg' command not found. This script is intended for Debian-based systems."
143 fi
144 if ! [[ -f /etc/os-release ]]; then
145 log_error "File /etc/os-release not found. This script is intended for Debian-based systems."
146 fi
147
148 # Execute steps
149 install_prerequisites
150 setup_apt_repository
151 install_docker
152 post_install
153
154 log_info "-----------------------------------------------------"
155 log_info "Docker installation completed successfully! 🎉"
156 log_info "REMEMBER TO LOG OUT AND LOG BACK IN to use Docker without sudo."
157 log_info "Verify installation by running: docker run hello-world"
158 log_info "-----------------------------------------------------"
159}
160
161# Run the main function
162main