Zuletzt aktiv 7 months ago

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

albert's Avatar albert hat die Gist bearbeitet 7 months ago. Zu Änderung gehen

1 file changed, 1 insertion, 1 deletion

docker.sh

@@ -5,7 +5,7 @@
5 5 #
6 6 # Usage:
7 7 # ./docker.sh
8 - # curl -fsSL https://cdn.albert.lol/docker.sh | bash
8 + # bash <(curl -s https://cdn.albert.lol/docker.sh)
9 9 #
10 10 # Requires:
11 11 # - Running on a Debian-based system (Debian, Ubuntu, etc.)

albert's Avatar albert hat die Gist bearbeitet 7 months ago. Zu Änderung gehen

Keine Änderungen

albert's Avatar albert hat die Gist bearbeitet 7 months ago. Zu Änderung gehen

1 file changed, 2 insertions, 5 deletions

docker.sh

@@ -202,8 +202,5 @@ main() {
202 202 log_success "-----------------------------------------------------"
203 203 }
204 204
205 - # Run the main function if the script is executed directly
206 - # This check prevents execution if the script is sourced, though unlikely for this use case.
207 - if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
208 - main
209 - fi
205 + # Run the main function
206 + main

albert's Avatar albert hat die Gist bearbeitet 7 months ago. Zu Änderung gehen

1 file changed, 101 insertions, 54 deletions

docker.sh

@@ -1,6 +1,7 @@
1 1 #!/bin/bash
2 2 #
3 - # Installs Docker Engine on Debian-based Linux distributions.
3 + # Installs Docker Engine on Debian-based Linux distributions with colored output.
4 + # Runs apt operations quietly, showing high-level status messages.
4 5 #
5 6 # Usage:
6 7 # ./docker.sh
@@ -10,6 +11,7 @@
10 11 # - Running on a Debian-based system (Debian, Ubuntu, etc.)
11 12 # - User executing the script must have sudo privileges.
12 13 # - Internet connection.
14 + # - A terminal that supports ANSI color codes.
13 15
14 16 # Strict mode
15 17 set -euo pipefail
@@ -18,26 +20,45 @@ set -euo pipefail
18 20 readonly DOCKER_GPG_KEY_PATH="/etc/apt/keyrings/docker.asc"
19 21 readonly DOCKER_APT_SOURCE_PATH="/etc/apt/sources.list.d/docker.list"
20 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 +
21 32 # --- Helper Functions ---
22 33
23 - # Logs an informational message.
24 - # Arguments:
25 - # $*: Message to log.
34 + # Logs an informational message (Blue).
35 + # Arguments: $*: Message to log.
26 36 log_info() {
27 - echo "[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} $*"
28 44 }
29 45
30 - # Logs an error message and exits.
31 - # Arguments:
32 - # $*: Message to log.
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.
33 54 log_error() {
34 - echo >&2 "[ERROR] $*"
55 + # Ensure error message goes to stderr
56 + echo -e >&2 "${COLOR_RED}${COLOR_BOLD}[ERROR]${COLOR_RESET}${COLOR_RED} $*${COLOR_RESET}"
35 57 exit 1
36 58 }
37 59
38 60 # Checks if a command exists.
39 - # Arguments:
40 - # $1: Command name.
61 + # Arguments: $1: Command name.
41 62 command_exists() {
42 63 command -v "$1" >/dev/null 2>&1
43 64 }
@@ -46,13 +67,19 @@ command_exists() {
46 67
47 68 # Step 1: Update package lists and install prerequisite packages.
48 69 install_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."
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."
56 83 }
57 84
58 85 # Step 2: Set up Docker's official APT repository.
@@ -60,15 +87,20 @@ setup_apt_repository() {
60 87 log_info "Setting up Docker's APT repository..."
61 88
62 89 # Create the directory for the GPG key if it doesn't exist
63 - log_info "Creating keyring directory: /etc/apt/keyrings"
90 + log_info "Ensuring keyring directory exists: /etc/apt/keyrings"
64 91 sudo install -m 0755 -d "$(dirname "${DOCKER_GPG_KEY_PATH}")"
65 92
66 93 # Download Docker's official GPG key
67 94 log_info "Downloading Docker GPG key to ${DOCKER_GPG_KEY_PATH}"
68 95 # 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}"
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
70 101 # Ensure the key is readable by apt
71 102 sudo chmod a+r "${DOCKER_GPG_KEY_PATH}"
103 + log_success "Docker GPG key downloaded and permissions set."
72 104
73 105 # Detect architecture and OS codename
74 106 local arch
@@ -91,58 +123,69 @@ setup_apt_repository() {
91 123 echo \
92 124 "deb [arch=${arch} signed-by=${DOCKER_GPG_KEY_PATH}] https://download.docker.com/linux/debian \
93 125 ${codename} stable" | sudo tee "${DOCKER_APT_SOURCE_PATH}" > /dev/null
94 -
95 - log_info "Docker APT repository setup complete."
126 + log_success "Docker APT repository added."
96 127 }
97 128
98 129 # Step 3: Install Docker Engine packages.
99 130 install_docker() {
131 + local docker_pkgs=("docker-ce" "docker-ce-cli" "containerd.io" "docker-buildx-plugin" "docker-compose-plugin")
100 132 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."
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."
111 145 }
112 146
113 147 # Step 4: Perform post-installation steps (add user to docker group).
114 148 post_install() {
115 149 log_info "Adding current user (${USER}) to the 'docker' group..."
116 150 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."
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
121 165 else
122 - log_info "The 'docker' group does not exist. Skipping user addition."
123 - log_info "This might happen if Docker installation failed earlier."
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."
124 168 fi
125 169 }
126 170
127 171 # --- Main Execution ---
128 172
129 173 main() {
130 - log_info "Starting Docker installation script..."
174 + log_info "${COLOR_BOLD}Starting Docker installation script...${COLOR_RESET}"
131 175
132 176 # Basic checks
133 177 if ! command_exists sudo; then
134 178 log_error "'sudo' command not found. This script requires sudo privileges."
135 179 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."
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."
143 186 fi
144 187 if ! [[ -f /etc/os-release ]]; then
145 - log_error "File /etc/os-release not found. This script is intended for Debian-based systems."
188 + log_error "File /etc/os-release not found. Cannot determine OS version. Aborting."
146 189 fi
147 190
148 191 # Execute steps
@@ -151,12 +194,16 @@ main() {
151 194 install_docker
152 195 post_install
153 196
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 "-----------------------------------------------------"
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 "-----------------------------------------------------"
159 203 }
160 204
161 - # Run the main function
162 - main
205 + # Run the main function if the script is executed directly
206 + # This check prevents execution if the script is sourced, though unlikely for this use case.
207 + if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
208 + main
209 + fi

albert's Avatar albert hat die Gist bearbeitet 7 months ago. Zu Änderung gehen

1 file changed, 2 insertions, 2 deletions

docker.sh

@@ -3,8 +3,8 @@
3 3 # Installs Docker Engine on Debian-based Linux distributions.
4 4 #
5 5 # Usage:
6 - # ./install_docker.sh
7 - # curl -fsSL <URL_TO_THIS_SCRIPT> | bash
6 + # ./docker.sh
7 + # curl -fsSL https://cdn.albert.lol/docker.sh | bash
8 8 #
9 9 # Requires:
10 10 # - Running on a Debian-based system (Debian, Ubuntu, etc.)

albert's Avatar albert hat die Gist bearbeitet 7 months ago. Zu Änderung gehen

1 file changed, 149 insertions, 27 deletions

docker.sh

@@ -1,40 +1,162 @@
1 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.
2 13
3 - # Exit immediately if a command exits with a non-zero status
4 - set -e
14 + # Strict mode
15 + set -euo pipefail
5 16
6 - # Update package index
7 - sudo apt-get update
17 + # --- Configuration ---
18 + readonly DOCKER_GPG_KEY_PATH="/etc/apt/keyrings/docker.asc"
19 + readonly DOCKER_APT_SOURCE_PATH="/etc/apt/sources.list.d/docker.list"
8 20
9 - # Install required packages for Docker installation
10 - sudo apt-get install -y ca-certificates curl
21 + # --- Helper Functions ---
11 22
12 - # Create the directory for Docker's apt keyrings
13 - sudo install -m 0755 -d /etc/apt/keyrings
23 + # Logs an informational message.
24 + # Arguments:
25 + # $*: Message to log.
26 + log_info() {
27 + echo "[INFO] $*"
28 + }
14 29
15 - # Download and add Docker's GPG key
16 - sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
30 + # Logs an error message and exits.
31 + # Arguments:
32 + # $*: Message to log.
33 + log_error() {
34 + echo >&2 "[ERROR] $*"
35 + exit 1
36 + }
17 37
18 - # Set appropriate permissions for the GPG key
19 - sudo chmod a+r /etc/apt/keyrings/docker.asc
38 + # Checks if a command exists.
39 + # Arguments:
40 + # $1: Command name.
41 + command_exists() {
42 + command -v "$1" >/dev/null 2>&1
43 + }
20 44
21 - # Add Docker's official apt repository
22 - echo \
23 - "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
24 - $(. /etc/os-release && echo \"$VERSION_CODENAME\") stable" | \
25 - sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
45 + # --- Main Functions ---
26 46
27 - # Update package index again to include Docker's repository
28 - sudo apt-get update
47 + # Step 1: Update package lists and install prerequisite packages.
48 + install_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 + }
29 57
30 - # Install Docker packages
31 - sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
58 + # Step 2: Set up Docker's official APT repository.
59 + setup_apt_repository() {
60 + log_info "Setting up Docker's APT repository..."
32 61
33 - # Add the current user to the Docker group
34 - sudo usermod -aG docker $USER
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}")"
35 65
36 - # Refresh group membership without needing to log out and back in
37 - newgrp docker
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}"
38 72
39 - # Print success message
40 - echo "Docker has been installed successfully."
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.
99 + install_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).
114 + post_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 +
129 + main() {
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
162 + main

albert's Avatar albert hat die Gist bearbeitet 10 months ago. Zu Änderung gehen

1 file changed, 1 insertion, 1 deletion

docker.sh

@@ -37,4 +37,4 @@ sudo usermod -aG docker $USER
37 37 newgrp docker
38 38
39 39 # Print success message
40 - echo "Docker has been installed successfully.
40 + echo "Docker has been installed successfully."

albert's Avatar albert hat die Gist bearbeitet 10 months ago. Zu Änderung gehen

Keine Änderungen

albert's Avatar albert hat die Gist bearbeitet 10 months ago. Zu Änderung gehen

1 file changed, 1 insertion, 1 deletion

docker.sh

@@ -37,4 +37,4 @@ sudo usermod -aG docker $USER
37 37 newgrp docker
38 38
39 39 # Print success message
40 - echo "Docker has been installed successfully."
40 + echo "Docker has been installed successfully.

albert's Avatar albert hat die Gist bearbeitet 10 months ago. Zu Änderung gehen

Keine Änderungen

Neuer Älter