docker.sh
· 4.9 KiB · Bash
Raw
#!/bin/bash
#
# Installs Docker Engine on Debian-based Linux distributions.
#
# Usage:
# ./install_docker.sh
# curl -fsSL <URL_TO_THIS_SCRIPT> | bash
#
# Requires:
# - Running on a Debian-based system (Debian, Ubuntu, etc.)
# - User executing the script must have sudo privileges.
# - Internet connection.
# 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"
# --- Helper Functions ---
# Logs an informational message.
# Arguments:
# $*: Message to log.
log_info() {
echo "[INFO] $*"
}
# Logs an error message and exits.
# Arguments:
# $*: Message to log.
log_error() {
echo >&2 "[ERROR] $*"
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() {
log_info "Updating package list and installing prerequisites..."
sudo apt-get update
sudo apt-get install -y \
ca-certificates \
curl \
gnupg
log_info "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 "Creating keyring directory: /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
sudo curl -fsSL "https://download.docker.com/linux/debian/gpg" -o "${DOCKER_GPG_KEY_PATH}"
# Ensure the key is readable by apt
sudo chmod a+r "${DOCKER_GPG_KEY_PATH}"
# 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_info "Docker APT repository setup complete."
}
# Step 3: Install Docker Engine packages.
install_docker() {
log_info "Updating package list after adding Docker repository..."
sudo apt-get update
log_info "Installing Docker packages..."
sudo apt-get install -y \
docker-ce \
docker-ce-cli \
containerd.io \
docker-buildx-plugin \
docker-compose-plugin
log_info "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
sudo usermod -aG docker "$USER"
log_info "User '${USER}' added to the 'docker' group."
log_info "IMPORTANT: You need to log out and log back in for the group change to take effect."
log_info "Alternatively, you can run 'newgrp docker' in your current shell, but logging out is recommended."
else
log_info "The 'docker' group does not exist. Skipping user addition."
log_info "This might happen if Docker installation failed earlier."
fi
}
# --- Main Execution ---
main() {
log_info "Starting Docker installation script..."
# Basic checks
if ! command_exists sudo; then
log_error "'sudo' command not found. This script requires sudo privileges."
fi
if [[ $EUID -eq 0 ]]; then
log_info "Script is running as root. Using sudo is recommended for specific commands only."
# Consider adapting script if root execution should behave differently,
# but for now, relying on internal sudo calls is fine.
fi
if ! command_exists dpkg; then
log_error "'dpkg' command not found. This script is intended for Debian-based systems."
fi
if ! [[ -f /etc/os-release ]]; then
log_error "File /etc/os-release not found. This script is intended for Debian-based systems."
fi
# Execute steps
install_prerequisites
setup_apt_repository
install_docker
post_install
log_info "-----------------------------------------------------"
log_info "Docker installation completed successfully! 🎉"
log_info "REMEMBER TO LOG OUT AND LOG BACK IN to use Docker without sudo."
log_info "Verify installation by running: docker run hello-world"
log_info "-----------------------------------------------------"
}
# Run the main function
main
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 |
15 | set -euo pipefail |
16 | |
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" |
20 | |
21 | # --- Helper Functions --- |
22 | |
23 | # Logs an informational message. |
24 | # Arguments: |
25 | # $*: Message to log. |
26 | log_info() { |
27 | echo "[INFO] $*" |
28 | } |
29 | |
30 | # Logs an error message and exits. |
31 | # Arguments: |
32 | # $*: Message to log. |
33 | log_error() { |
34 | echo >&2 "[ERROR] $*" |
35 | exit 1 |
36 | } |
37 | |
38 | # Checks if a command exists. |
39 | # Arguments: |
40 | # $1: Command name. |
41 | command_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. |
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 | } |
57 | |
58 | # Step 2: Set up Docker's official APT repository. |
59 | setup_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. |
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 |