Created
June 19, 2024 03:52
-
-
Save loopyd/31850efd4c2b57ce718a1d11b1330192 to your computer and use it in GitHub Desktop.
[bash] Install Docker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
ARGS=($*) | |
BOLD=$(tput bold) | |
RED=$(tput setaf 1) | |
GREEN=$(tput setaf 2) | |
BLUE=$(tput setaf 4) | |
RESET=$(tput sgr0) | |
function __run_cmd() { | |
local cmd | |
local output | |
local exit_code | |
cmd="$@" | |
output=$(eval "$cmd" 2>&1; exit ${PIPESTATUS[0]}) | |
exit_code=$? | |
if [[ $exit_code -ne 0 ]]; then | |
echo -e "${RED}${output}\n${BOLD}Command: $cmd failed with exit status code: ${exit_code}\nPlease check the error message above and try again${RESET}" >&2 | |
exit $exit_code | |
fi | |
return 0 | |
} | |
function __get_os() { | |
local version | |
local id_like | |
version=$(. /etc/os-release && echo "$VERSION_CODENAME") | |
id_like=$(. /etc/os-release && echo "$ID_LIKE") | |
if echo "$id_like" | grep -qi "ubuntu"; then | |
if ! echo "$version" | grep -Eqi "jammy|bionic"; then | |
version=$(. /etc/os-release && echo "$UBUNTU_CODENAME") | |
if ! echo "$version" | grep -Eqi "jammy|bionic"; then | |
echo "Incompatible operating system found" >&2 | |
return 2 | |
fi | |
fi | |
echo "$version" | |
else | |
echo "Incompatible operating system found" >&2 | |
return 2 | |
fi | |
} | |
function __main() { | |
local BARGS | |
BARGS=($*) | |
local OS | |
OS=$(__get_os) | |
echo -e "${BLUE}Removing old Docker versions...${RESET}" | |
__run_cmd "for pkg in docker.io docker-doc docker-ce docker-ce-cli docker-compose docker-compose-v2 docker-compose-plugin podman-docker containerd containerd.io runc; do sudo apt-get -y remove \${pkg}; done && sudo apt-get autoclean" | |
echo -e "${BLUE}Installing keyrings...${RESET}" | |
__run_cmd "sudo apt-get -y update" | |
__run_cmd "sudo apt-get -y install ca-certificates curl" | |
__run_cmd "sudo install -m 0755 -d /etc/apt/keyrings" | |
__run_cmd "sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc" | |
__run_cmd "sudo chmod a+r /etc/apt/keyrings/docker.asc" | |
echo -e "${BLUE}Adding Docker repository...${RESET}" | |
__run_cmd "echo \"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $OS stable\" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null" | |
__run_cmd "sudo apt-get -y update" | |
echo -e "${BLUE}Installing Docker...${RESET}" | |
__run_cmd "sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin" | |
echo -e "${BLUE}Adding user to docker group...${RESET}" | |
__run_cmd "sudo usermod -aG docker $USER" | |
echo -e "${GREEN}Docker installed successfully${RESET}" | |
} | |
__main ${ARGS[@]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment