-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-install.sh
More file actions
706 lines (607 loc) · 20.3 KB
/
docker-install.sh
File metadata and controls
706 lines (607 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
#!/bin/bash
set -eux
# Error handling
handle_error() {
local exit_code=$1
local line_number=$2
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Error occurred in script at line ${line_number}, exit code ${exit_code}"
exit "${exit_code}"
}
trap 'handle_error $? $LINENO' ERR
# Basic logging function
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*"
}
# Retry mechanism for commands
retry_command() {
local -r cmd="${1}"
local -r timeout="${2:-10m}" # Default timeout 10 minutes
local -r sleep_time="${3:-10}" # Default sleep 10 seconds
timeout "${timeout}" bash -c "until ${cmd}; do echo 'Command failed, retrying in ${sleep_time} seconds...'; sleep ${sleep_time}; done"
}
# Check if a package is installed (DNF systems)
is_package_installed_dnf() {
if dnf list installed "$1" &>/dev/null; then
return 0
fi
return 1
}
# Check if a package is installed (APT systems)
is_package_installed_apt() {
if dpkg -l "$1" 2>/dev/null | grep -q '^ii'; then
return 0
fi
return 1
}
# Check if Docker service is configured and running
check_docker_service() {
if systemctl is-active docker &>/dev/null; then
if systemctl is-enabled docker &>/dev/null; then
log "Docker service is already configured and running"
return 0
fi
fi
return 1
}
# Check Docker installation
check_docker() {
if ! command -v docker &> /dev/null || ! docker compose version &> /dev/null; then
log "Docker or Docker Compose is not installed. Proceeding with installation..."
return 1
fi
# Check Docker version
DOCKER_VERSION=$(docker --version | awk '{ gsub(/,/, "", $3); print $3 }')
DOCKER_MAJOR_VERSION=$(docker --version | awk '{ split($3, version, "."); print version[1]; }')
if [ "${DOCKER_MAJOR_VERSION}" -lt 23 ]; then
log "Docker ${DOCKER_VERSION} detected. This version is no longer supported. Will update."
return 1
fi
log "Docker ${DOCKER_VERSION} is already installed and properly configured"
return 0
}
# Check if user can use sudo
check_sudo() {
if [ "${EUID}" -eq 0 ]; then
return 0
fi
if groups | grep -q '\bsudo\b' || groups | grep -q '\badmin\b'; then
return 0
fi
return 1
}
# Cleanup handler
cleanup() {
log "Cleaning up temporary files..."
# Add specific cleanup tasks here
}
# Set up cleanup trap
trap cleanup EXIT
fix_repository_issues() {
local temp_error=$(mktemp)
local sources_dir="/etc/apt/sources.list.d"
local main_list="/etc/apt/sources.list"
local backup_dir="/etc/apt/sources.backup-$(date +%Y%m%d-%H%M%S)"
log "Checking and fixing repository issues..."
# Create backup
mkdir -p "$backup_dir"
cp $main_list "$backup_dir/"
cp -r $sources_dir/* "$backup_dir/" 2>/dev/null || true
# Attempt update and capture errors
apt-get update &> "$temp_error"
# If there are Release file issues
if grep -q "does not have a Release file" "$temp_error"; then
log "Found repositories with missing Release files"
# Process each problem repository
find "$sources_dir" -name "*.list" | while read -r list_file; do
if [ -f "${list_file}" ] && grep -q "$(grep -o 'https\?://[^ ]*' "$temp_error")" "${list_file}" 2>/dev/null; then
log "Disabling problematic repository: ${list_file}"
mv "${list_file}" "${list_file}.disabled"
fi
done
# Update base repositories based on distribution
if [ -f /etc/debian_version ]; then
log "Configuring Debian repositories"
tee $main_list > /dev/null << EOF
deb http://deb.debian.org/debian $(lsb_release -cs) main contrib non-free
deb http://deb.debian.org/debian-security/ $(lsb_release -cs)-security main contrib non-free
deb http://deb.debian.org/debian $(lsb_release -cs)-updates main contrib non-free
EOF
else
log "Configuring Ubuntu repositories"
local ubuntu_codename=$(lsb_release -cs)
tee $main_list > /dev/null << EOF
deb http://archive.ubuntu.com/ubuntu/ ${ubuntu_codename} main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ ${ubuntu_codename}-updates main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu/ ${ubuntu_codename}-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu/ ${ubuntu_codename}-security main restricted universe multiverse
EOF
fi
# Clean and update
rm -rf /var/lib/apt/lists/*
apt-get clean
retry_command "apt-get update"
fi
rm -f "$temp_error"
}
# Check Docker repository for Debian/Ubuntu
check_docker_repo_debian() {
if [ -f "/etc/apt/keyrings/docker.gpg" ] && [ -f "/etc/apt/sources.list.d/docker.list" ]; then
log "Docker repository files exist, checking configuration..."
# Verify repository content
if grep -q "download.docker.com/linux/$(. /etc/os-release && echo "$ID")" "/etc/apt/sources.list.d/docker.list"; then
if [ -s "/etc/apt/keyrings/docker.gpg" ]; then
log "Docker repository is properly configured"
return 0
else
log "Docker GPG key appears to be empty or corrupted"
fi
else
log "Docker repository configuration appears incorrect"
fi
fi
log "Docker repository needs to be configured"
return 1
}
# Check Docker repository for Fedora
check_docker_repo_fedora() {
if [ -f "/etc/yum.repos.d/docker-ce.repo" ]; then
log "Docker repository file exists, checking configuration..."
# Verify repository content
if grep -q "download.docker.com/linux/fedora" "/etc/yum.repos.d/docker-ce.repo"; then
if grep -q "enabled=1" "/etc/yum.repos.d/docker-ce.repo"; then
log "Docker repository is properly configured"
return 0
else
log "Docker repository is disabled"
fi
else
log "Docker repository configuration appears incorrect"
fi
fi
log "Docker repository needs to be configured"
return 1
}
# Check Docker repository for RHEL/CentOS
check_docker_repo_rhel() {
if [ -f "/etc/yum.repos.d/docker-ce.repo" ]; then
log "Docker repository file exists, checking configuration..."
# Verify repository content
if grep -q "download.docker.com/linux/centos" "/etc/yum.repos.d/docker-ce.repo"; then
if grep -q "enabled=1" "/etc/yum.repos.d/docker-ce.repo"; then
log "Docker repository is properly configured"
return 0
else
log "Docker repository is disabled"
fi
else
log "Docker repository configuration appears incorrect"
fi
fi
log "Docker repository needs to be configured"
return 1
}
# Configure Docker repository for Fedora
configure_docker_repo_fedora() {
log "Configuring Docker repository for Fedora..."
tee /etc/yum.repos.d/docker-ce.repo <<EOF
[docker-ce-stable]
name=Docker CE Stable - \$basearch
baseurl=https://download.docker.com/linux/fedora/\$releasever/\$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://download.docker.com/linux/fedora/gpg
EOF
# Verify configuration
if ! check_docker_repo_fedora; then
log "Error: Failed to configure Docker repository"
return 1
fi
return 0
}
# Configure Docker repository for RHEL/CentOS
configure_docker_repo_rhel() {
log "Configuring Docker repository for RHEL/CentOS..."
retry_command "dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo"
# Verify configuration
if ! check_docker_repo_rhel; then
log "Error: Failed to configure Docker repository"
return 1
fi
return 0
}
# Configure Docker repository for Debian/Ubuntu
configure_docker_repo_debian() {
log "Configuring Docker repository for $(. /etc/os-release && echo "$ID")..."
# Set up directory for GPG key
install -m 0755 -d /etc/apt/keyrings
# Download and install GPG key with retry
retry_command "curl -fsSL https://download.docker.com/linux/$(. /etc/os-release && echo "$ID")/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg"
chmod a+r /etc/apt/keyrings/docker.gpg
# Add repository
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/$(. /etc/os-release && echo "$ID") \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null
# Update package list with retry
retry_command "apt-get update"
# Verify configuration
if ! check_docker_repo_debian; then
log "Error: Failed to configure Docker repository"
return 1
fi
return 0
}
install_docker_ubuntu_debian() {
log "Installing Docker on Ubuntu/Debian"
# First fix any repository issues
fix_repository_issues
# Remove conflicting packages only if they exist
local packages_to_remove=(
docker.io
docker-engine
docker
docker-doc
docker-compose
docker-compose-v2
podman-docker
containerd
runc
)
local found_conflicting=false
for pkg in "${packages_to_remove[@]}"; do
if is_package_installed_apt "$pkg"; then
found_conflicting=true
log "Found conflicting package: $pkg"
fi
done
if [ "$found_conflicting" = true ]; then
log "Removing conflicting packages..."
retry_command "apt-get remove -y ${packages_to_remove[*]}"
fi
# Install prerequisites
local prerequisites=(
ca-certificates
curl
gnupg
apt-transport-https
software-properties-common
)
log "Updating package lists..."
retry_command "apt-get update"
local need_prereq=false
for pkg in "${prerequisites[@]}"; do
if ! is_package_installed_apt "$pkg"; then
need_prereq=true
break
fi
done
if [ "$need_prereq" = true ]; then
log "Installing prerequisites..."
retry_command "apt-get install -y ${prerequisites[*]}"
fi
# Configure Docker repository if needed
if ! check_docker_repo_debian; then
configure_docker_repo_debian
fi
# Install Docker packages
local docker_packages=(
docker-ce
docker-ce-cli
containerd.io
docker-buildx-plugin
docker-compose-plugin
)
local need_install=false
for pkg in "${docker_packages[@]}"; do
if ! is_package_installed_apt "$pkg"; then
need_install=true
log "Need to install: $pkg"
fi
done
if [ "$need_install" = true ]; then
log "Installing Docker packages..."
retry_command "apt-get install -y ${docker_packages[*]}"
fi
# Verify and configure Docker service
if ! check_docker_service; then
log "Configuring Docker service..."
systemctl enable docker
systemctl start docker
fi
# Verify installation
if ! command -v docker >/dev/null 2>&1; then
log "Error: Docker installation failed"
return 1
fi
log "Docker installation successful"
return 0
}
# Install Docker and Docker Compose on Fedora
install_docker_fedora() {
log "Installing Docker on Fedora"
# Remove conflicting packages only if they exist
local packages_to_remove=(
docker
docker-client
docker-client-latest
docker-common
docker-latest
docker-latest-logrotate
docker-logrotate
docker-selinux
docker-engine-selinux
docker-engine
podman
)
local found_conflicting=false
for pkg in "${packages_to_remove[@]}"; do
if is_package_installed_dnf "$pkg"; then
found_conflicting=true
log "Found conflicting package: $pkg"
fi
done
if [ "$found_conflicting" = true ]; then
log "Removing conflicting packages..."
retry_command "dnf -y remove ${packages_to_remove[*]}"
fi
# Install dnf-plugins-core if needed
if ! is_package_installed_dnf "dnf-plugins-core"; then
log "Installing dnf-plugins-core..."
retry_command "dnf -y install dnf-plugins-core"
fi
# Configure Docker repository if needed
if ! check_docker_repo_fedora; then
configure_docker_repo_fedora
fi
# Install Docker packages
local docker_packages=(
docker-ce
docker-ce-cli
containerd.io
docker-buildx-plugin
docker-compose-plugin
)
local need_install=false
for pkg in "${docker_packages[@]}"; do
if ! is_package_installed_dnf "$pkg"; then
need_install=true
log "Need to install: $pkg"
fi
done
if [ "$need_install" = true ]; then
log "Installing Docker packages..."
retry_command "dnf -y install ${docker_packages[*]}"
fi
# Configure Docker service if needed
if ! check_docker_service; then
log "Configuring Docker service..."
systemctl enable docker
systemctl start docker
fi
# Verify installation
if ! command -v docker >/dev/null 2>&1; then
log "Error: Docker installation failed"
return 1
fi
log "Docker installation successful"
return 0
}
# Install Docker and Docker Compose on RHEL/CentOS
install_docker_rhel() {
log "Installing Docker on RHEL/CentOS"
# Remove conflicting packages only if they exist
local packages_to_remove=(
docker
docker-client
docker-client-latest
docker-common
docker-latest
docker-latest-logrotate
docker-logrotate
docker-engine
podman
runc
)
local found_conflicting=false
for pkg in "${packages_to_remove[@]}"; do
if is_package_installed_dnf "$pkg"; then
found_conflicting=true
log "Found conflicting package: $pkg"
fi
done
if [ "$found_conflicting" = true ]; then
log "Removing conflicting packages..."
retry_command "dnf -y remove ${packages_to_remove[*]}"
fi
# Install dnf-plugins-core if needed
if ! is_package_installed_dnf "dnf-plugins-core"; then
log "Installing dnf-plugins-core..."
retry_command "dnf -y install dnf-plugins-core"
fi
# Configure Docker repository if needed
if ! check_docker_repo_rhel; then
configure_docker_repo_rhel
fi
# Install Docker packages
local docker_packages=(
docker-ce
docker-ce-cli
containerd.io
docker-buildx-plugin
docker-compose-plugin
)
local need_install=false
for pkg in "${docker_packages[@]}"; do
if ! is_package_installed_dnf "$pkg"; then
need_install=true
log "Need to install: $pkg"
fi
done
if [ "$need_install" = true ]; then
log "Installing Docker packages..."
retry_command "dnf -y install ${docker_packages[*]}"
fi
# Configure Docker service if needed
if ! check_docker_service; then
log "Configuring Docker service..."
systemctl enable docker
systemctl start docker
fi
# Verify installation
if ! command -v docker >/dev/null 2>&1; then
log "Error: Docker installation failed"
return 1
fi
log "Docker installation successful"
return 0
}
get_distribution() {
local distro=""
if [ -f /etc/os-release ]; then
# Source the os-release file
. /etc/os-release
distro=$ID
elif [ -f /etc/debian_version ]; then
distro="debian"
elif [ -f /etc/fedora-release ]; then
distro="fedora"
elif [ -f /etc/redhat-release ]; then
if grep -q "CentOS" /etc/redhat-release; then
distro="centos"
else
distro="rhel"
fi
fi
echo "$distro"
}
# Get OS version
get_version() {
local version=""
if [ -f /etc/os-release ]; then
. /etc/os-release
version=$VERSION_ID
elif [ -f /etc/debian_version ]; then
version=$(cat /etc/debian_version)
elif [ -f /etc/fedora-release ]; then
version=$(grep -oE '[0-9]+' /etc/fedora-release)
elif [ -f /etc/redhat-release ]; then
version=$(grep -oE '[0-9]+\.[0-9]+' /etc/redhat-release | cut -d. -f1)
fi
echo "$version"
}
# Verify minimum version requirements
check_version_requirements() {
local distro=$1
local version=$2
case "$distro" in
ubuntu)
if [ "${version%.*}" -lt 20 ]; then
log "Error: Ubuntu version $version is not supported. Minimum required version is 20.04"
return 1
fi
;;
debian)
if [ "${version%.*}" -lt 10 ]; then
log "Error: Debian version $version is not supported. Minimum required version is 10"
return 1
fi
;;
fedora)
if [ "$version" -lt 35 ]; then
log "Error: Fedora version $version is not supported. Minimum required version is 35"
return 1
fi
;;
centos|rhel)
if [ "$version" -lt 7 ]; then
log "Error: RHEL/CentOS version $version is not supported. Minimum required version is 7"
return 1
fi
;;
esac
return 0
}
# Main installation logic
main() {
local distro
local version
# Check if script is run as root
if [ "$EUID" -ne 0 ]; then
if ! check_sudo; then
log "Error: This script must be run as root or with sudo privileges"
exit 1
fi
fi
# Detect OS distribution and version
distro=$(get_distribution)
version=$(get_version)
if [ -z "$distro" ]; then
log "Error: Unable to detect Linux distribution"
exit 1
fi
log "Detected distribution: $distro version $version"
# Check version requirements
if ! check_version_requirements "$distro" "$version"; then
exit 1
fi
# Check if Docker is already installed properly
if check_docker; then
log "Docker is already installed and properly configured"
docker --version
docker compose version
exit 0
fi
# Perform installation based on distribution
case "$distro" in
ubuntu|debian)
if ! install_docker_ubuntu_debian; then
log "Error: Docker installation failed on $distro"
exit 1
fi
;;
fedora)
if ! install_docker_fedora; then
log "Error: Docker installation failed on Fedora"
exit 1
fi
;;
centos|rhel)
if ! install_docker_rhel; then
log "Error: Docker installation failed on RHEL/CentOS"
exit 1
fi
;;
*)
log "Error: Unsupported distribution: $distro"
exit 1
;;
esac
# Final verification
if command -v docker >/dev/null 2>&1; then
log "Docker installation completed successfully"
log "Docker version: $(docker --version)"
log "Docker Compose version: $(docker compose version)"
# Add current user to docker group if not root
if [ "$EUID" -ne 0 ]; then
local current_user=${SUDO_USER:-$USER}
if ! groups "$current_user" | grep -q docker; then
log "Adding user $current_user to docker group..."
usermod -aG docker "$current_user"
log "Please log out and back in for the group changes to take effect"
fi
fi
log "Installation completed successfully"
exit 0
else
log "Error: Docker installation verification failed"
exit 1
fi
}
# Set strict error handling
set -euo pipefail
# Execute main function with error handling
if [ "${BASH_SOURCE[0]}" -ef "$0" ]; then
trap 'handle_error $? $LINENO' ERR
trap cleanup EXIT
main "$@"
fi