-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-configure-docker.sh
executable file
·177 lines (154 loc) · 6.52 KB
/
11-configure-docker.sh
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
#!/bin/sh
#-------------------------------------------------------------------------------
# Install and configure "docker" by:
#
# 1. installing dependencies;
# 2. adding the official GPG key for "docker";
# 3. adding the official docker repository; and
# 4. installing docker.
#
# N.B.
# This script needs to be run as "sudo".
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Imported shared variables.
#-------------------------------------------------------------------------------
. ./linshafun/linshafun.var
#-------------------------------------------------------------------------------
# Imported project specific variables.
#-------------------------------------------------------------------------------
. ./linux-setup.var
#-------------------------------------------------------------------------------
# Imported shared functions.
#-------------------------------------------------------------------------------
. ./linshafun/comments.sh
# . ./linshafun/docker-env-variables.sh
# . ./linshafun/docker-images.sh
# . ./linshafun/docker-services.sh
# . ./linshafun/docker-volumes.sh
# . ./linshafun/files-directories.sh
# . ./linshafun/firewall.sh
# . ./linshafun/host-env-variables.sh
# . ./linshafun/host-information.sh
# . ./linshafun/network.sh
. ./linshafun/ownership-permissions.sh
. ./linshafun/packages.sh
# . ./linshafun/services.sh
. ./linshafun/setup-config.sh
. ./linshafun/setup.sh
# . ./linshafun/ssh-keys.sh
. ./linshafun/text.sh
. ./linshafun/user-input.sh
#-------------------------------------------------------------------------------
# Config key and service variables.
#-------------------------------------------------------------------------------
CONFIG_KEY='configuredDocker'
SERVICE="$(changeCase "${CONFIG_KEY#'configured'}" "lower")"
#-------------------------------------------------------------------------------
# Installs all components of docker, after updating and upgrading packages to
# ensure use of the added docker repository.
#-------------------------------------------------------------------------------
installDocker () {
echoComment "Installing $SERVICE."
echoSeparator
updateUpgrade
installRemovePackages "install" "docker-ce" "docker-ce-cli" "containerd.io" "docker-buildx-plugin" "docker-compose-plugin"
}
#-------------------------------------------------------------------------------
# Installs the dependencies necessary to run docker, after updating and
# upgrading packages.
#
# N.B.
# Some of the dependencies may already be installed.
#-------------------------------------------------------------------------------
installDockerDependencies () {
echoComment "Installing dependencies for $SERVICE."
echoSeparator
updateUpgrade
installRemovePackages "install" "ca-certificates" "curl" "gnupg"
}
#-------------------------------------------------------------------------------
# Adds the GPG key for docker.
#-------------------------------------------------------------------------------
installDockerGpgKey () {
echoComment "Adding official GPG key for $SERVICE."
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
chmod a+r /etc/apt/keyrings/docker.gpg
echoComment 'GPG key added.'
}
#-------------------------------------------------------------------------------
# Sets up the repository for docker to enable install via "apt install".
#-------------------------------------------------------------------------------
installDockerRepository () {
echoComment "Setting up the repository for $SERVICE."
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
echoComment 'Repository set up.'
}
#-------------------------------------------------------------------------------
# Removes any existing install of docker.
#
# N.B.
# There are two remove commands, one for older installs, and another for any
# install performed by this script.
#-------------------------------------------------------------------------------
removeExistingDocker () {
promptForUserInput "Do you want to remove the existing install of $SERVICE (y/n)?"
DOCKER_REMOVE_YN="$(getUserInputYN)"
if [ "$DOCKER_REMOVE_YN" = true ]; then
echoComment "Removing existing installation of $SERVICE."
installRemovePackages "remove" "docker.io" "docker-doc" "docker-compose" "podman-docker" "containerd" "runc"
installRemovePackages "remove" "docker-ce" "docker-ce-cli" "containerd.io" "docker-buildx-plugin" "docker-compose-plugin"
mainScript
else
echoComment "Leaving current $SERVICE installation intact."
fi
}
#-------------------------------------------------------------------------------
# Verifies that the docker install worked correctly by running a "Hello world"
# container, then removing it.
#
# N.B.
# Had to add a shell command to get docker to run from this function.
#-------------------------------------------------------------------------------
verifyDockerInstall() {
echoComment "Verifying $SERVICE install."
echoSeparator
$SERVICE run hello-world
echoSeparator
echoComment "If $SERVICE was installed correctly, Hello World will appear above."
echoComment "Removing verification data and container."
echoSeparator
$SERVICE system prune -af
echoSeparator
echoComment "Verification data and container removed."
}
#-------------------------------------------------------------------------------
# Runs the main functions of the script.
#-------------------------------------------------------------------------------
mainScript () {
echoComment "Starting setup of $SERVICE."
local SERVICE_CHECK="$(checkForPackage "$SERVICE")"
echoComment "Checking for $SERVICE."
echoComment "Check returned $SERVICE_CHECK."
if [ "$SERVICE_CHECK" = true ]; then
echoComment "You have already installed $SERVICE."
removeExistingDocker
elif [ "$SERVICE_CHECK" = false ]; then
echoComment "You need to install $SERVICE."
installDockerDependencies
installDockerGpgKey
installDockerRepository
installDocker
verifyDockerInstall
fi
}
#-------------------------------------------------------------------------------
# Run the script.
#-------------------------------------------------------------------------------
initialiseScript "$CONFIG_KEY"
mainScript
finaliseScript "$CONFIG_KEY"