Update installer for Ubuntu22 and K8s 1.26.5
[osm/devops.git] / installers / install_docker_ce.sh
1 #!/bin/bash
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15
16 set +eux
17
18 function install_docker_ce() {
19 # installs and configures Docker CE
20 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
21 echo "Removing previous installation of docker ..."
22 for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
23 echo "Installing Docker CE ..."
24 sudo apt-get -y update
25 sudo apt-get install -y apt-transport-https ca-certificates software-properties-common gnupg
26 sudo install -m 0755 -d /etc/apt/keyrings
27 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
28 sudo chmod a+r /etc/apt/keyrings/docker.gpg
29 echo "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
30 "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
31 sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
32 sudo apt-get -y update
33 sudo apt-get install -y docker-ce
34
35 echo "Adding user to group 'docker'"
36 sudo groupadd -f docker
37 sudo usermod -aG docker $USER
38
39 if [ -n "${DOCKER_PROXY_URL}" ]; then
40 echo "Configuring docker proxy ..."
41 if [ -f /etc/docker/daemon.json ]; then
42 if grep -q registry-mirrors /etc/docker/daemon.json; then
43 sudo sed -i "s|registry-mirrors.*|registry-mirrors\": [\"${DOCKER_PROXY_URL}\"] |" /etc/docker/daemon.json
44 else
45 sudo sed -i "s|^{|{\n \"registry-mirrors\": [\"${DOCKER_PROXY_URL}\"],|" /etc/docker/daemon.json
46 fi
47 else
48 sudo bash -c "cat << EOF > /etc/docker/daemon.json
49 {
50 \"registry-mirrors\": [\"${DOCKER_PROXY_URL}\"]
51 }
52 EOF"
53 fi
54 fi
55 if [ -n "${OSM_BEHIND_PROXY}" ] ; then
56 if ! [ -f /etc/systemd/system/docker.service.d/http-proxy.conf ] ; then
57 sudo mkdir -p /etc/systemd/system/docker.service.d
58 cat << EOF | sudo tee -a /etc/systemd/system/docker.service.d/http-proxy.conf
59 [Service]
60 EOF
61 fi
62 [ -n "${HTTP_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf
63 Environment=\"HTTP_PROXY=${HTTP_PROXY}\"
64 EOF"
65 [ -n "${HTTPS_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf
66 Environment=\"HTTPS_PROXY=${HTTPS_PROXY}\"
67 EOF"
68 [ -n "${NO_PROXY}" ] && sudo bash -c "cat <<EOF >> /etc/systemd/system/docker.service.d/http-proxy.conf
69 Environment=\"NO_PROXY=${NO_PROXY}\"
70 EOF"
71 fi
72 if [ -n "${DOCKER_PROXY_URL}" ] || [ -n "${OSM_BEHIND_PROXY}" ] ; then
73 sudo systemctl daemon-reload
74 sudo systemctl restart docker
75 echo "... restarted Docker service"
76 fi
77
78 echo "Configuring containerd to expose CRI, use systemd cgroup and use DOCKER_PROXY_URL as registry mirror"
79 sudo mv /etc/containerd/config.toml /etc/containerd/config.toml.orig 2>/dev/null
80 sudo bash -c "containerd config default > /etc/containerd/config.toml"
81 sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
82 if [ -n "${DOCKER_PROXY_URL}" ]; then
83 sudo sed -i "s#\[plugins.\"io.containerd.grpc.v1.cri\".registry.mirrors\]#\[plugins.\"io.containerd.grpc.v1.cri\".registry.mirrors\]\n \[plugins.\"io.containerd.grpc.v1.cri\".registry.mirrors.\"docker.io\"\]\n endpoint = \[\"${DOCKER_PROXY_URL}\"\]\n \[plugins.\"io.containerd.grpc.v1.cri\".registry.mirrors.\"registry.hub.docker.com\"]\n endpoint = \[\"${DOCKER_PROXY_URL}\"]#" /etc/containerd/config.toml
84 fi
85 sudo systemctl restart containerd
86
87 [ -z "${DEBUG_INSTALL}" ] || ! echo "File: /etc/docker/daemon.json" || cat /etc/docker/daemon.json
88 echo "Testing Docker CE installation ..."
89 sg docker -c "docker version" || FATAL_TRACK docker_ce "Docker installation failed. Cannot run docker version"
90 sg docker -c "docker run --rm hello-world" || FATAL_TRACK docker_ce "Docker installation failed. Cannot run hello-world"
91 echo "... Docker CE installation done"
92 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
93 return 0
94 }
95
96 OSM_BEHIND_PROXY=""
97 DOCKER_PROXY_URL=""
98
99 while getopts ":D:p:-: P" o; do
100 case "${o}" in
101 D)
102 OSM_DEVOPS="${OPTARG}"
103 ;;
104 p)
105 DOCKER_PROXY_URL="${OPTARG}"
106 ;;
107 P)
108 OSM_BEHIND_PROXY="y"
109 ;;
110 -)
111 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
112 echo -e "Invalid option: '--$OPTARG'\n" >&2
113 exit 1
114 ;;
115 :)
116 echo "Option -$OPTARG requires an argument" >&2
117 exit 1
118 ;;
119 \?)
120 echo -e "Invalid option: '-$OPTARG'\n" >&2
121 exit 1
122 ;;
123 *)
124 exit 1
125 ;;
126 esac
127 done
128
129 source $OSM_DEVOPS/common/logging
130
131 echo "DEBUG_INSTALL=$DEBUG_INSTALL"
132 echo "DOCKER_PROXY_URL=$DOCKER_PROXY_URL"
133 echo "OSM_BEHIND_PROXY=$OSM_BEHIND_PROXY"
134 echo "USER=$USER"
135
136 install_docker_ce
137