-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-docker.sh
More file actions
97 lines (86 loc) · 2.84 KB
/
install-docker.sh
File metadata and controls
97 lines (86 loc) · 2.84 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
#!/usr/bin/env bash
# Docker Installation Script
#
# Automates the installation of Docker Engine on Ubuntu-based systems
# by removing conflicting packages and setting up official Docker repositories.
#
# Features:
# - Removes existing Docker-related packages to prevent conflicts
# - Adds Docker's official GPG key and APT repository
# - Installs Docker Engine, containerd, and Docker Compose
# - Verifies installation by running hello-world container
#
# Prerequisites:
# - Must be run with sudo/root privileges
# - Target system must be Ubuntu or Ubuntu-based distribution
# - Internet connection required
#
# Usage:
# sudo ./install-docker.sh
# or
# curl -fsSL https://raw.githubusercontent.com/etkeys/scripts/refs/heads/master/install-docker.sh | sudo bash
#
# Post-Installation:
# - User can be added to docker group using:
# sudo usermod -aG docker $USER
#
# Exit Codes:
# - 2: Script not run with root privileges
#
# Note: Requires manual logout/login to apply group changes
set -eu
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root. Please use sudo."
exit 2
fi
# Remove any docker related packages from distribution that may have been installed
# These packages may conflict with packages to be installed from docker
for pkg in \
docker.io \
docker-doc \
docker-compose \
docker-compose-v2 \
containerd \
runc; do
if dpkg -l | grep -q "^ii $pkg "; then
echo "Removing $pkg..."
apt-get remove --purge -y "$pkg"
fi
done
# Add Docker's official GPG key
apt-get update
apt-get install -y \
ca-certificates \
curl \
gnupg
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | tee /etc/apt/keyrings/docker.asc > /dev/null
chmod a+r /etc/apt/keyrings/docker.asc
# Add Docker's official APT repository
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update
# Install Docker Engine, containerd, and Docker Compose
apt-get install -y \
docker-ce \
docker-ce-cli \
containerd.io \
docker-compose-plugin
# Run hello-world to verify installation
docker run hello-world
if whiptail --title "Add User to Docker Group" \
--yesno "Would you like to add user '$USER' to the docker group? This allows running docker commands without sudo. Be warned, this can have security implications." \
10 72; then
if [ -z "$USER" ]; then
echo "No user found."
else
usermod -aG docker "$USER"
echo "User $USER has been added to the docker group."
fi
else
echo "You can add your user to the docker group later using:"
echo " sudo usermod -aG docker \$USER"
fi
echo ""
echo "Docker installation complete."