Last active
April 29, 2022 10:17
-
-
Save Shardj/0e07f8bfc0222efddf913b713939357a to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env bash | |
ColorOff='\033[0m' # Text Reset | |
Yellow='\033[0;33m' # Yellow for important info | |
Red='\033[0;31m' # Red for errors | |
function infoMessage() { | |
echo -e ${Yellow} | |
echo $1 | |
echo -e ${ColorOff} | |
} | |
function errMessage() { | |
echo -e ${Red} | |
echo $1 | |
echo -e ${ColorOff} | |
} | |
errHandler() { | |
# Any steps that must be taken prior to exiting due to error | |
errMessage "Something went wrong. Exiting now." | |
exit 1 | |
} | |
set -eE # -e throw ERR for any non-zero exit codes, -E as well as in any child functions | |
trap 'errHandler' ERR INT # Call errHandler function on ERR (non-zero exit code) or INT (ctrl-c interupt execution) | |
# Usage info, literally just prints this text, to be called when -h is used | |
show_help() { | |
cat << EOF | |
Usage: [-h] | |
This script will install or uninstall Docker and Docker Compose for you | |
Parameter details: | |
-h display help and exit | |
EOF | |
} | |
# Initialise variables | |
OPTIND=1 # Reset for previous uses of getops in shell | |
OPTSTRING="h" # colon after character to tell getopts that we're expecting a value. No colon chars are just flags | |
# Location of this file | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
cd "$DIR/.." # Go to project root | |
while getopts "$OPTSTRING" opt; do | |
case "$opt" in | |
h) | |
show_help | |
exit 0 | |
;; | |
*) | |
show_help >&2 | |
exit 1 | |
;; | |
esac | |
done | |
if [[ $OPTIND == 1 ]] && [[ $@ != "" ]]; then | |
echo "Arguments were passed that getopts didn't detect" | |
exit 1 | |
fi | |
if [ -f /.dockerenv ]; then | |
errMessage "Why are you trying to install docker inside of docker? Computer says no." | |
exit 1 | |
else | |
# If not inside docker container | |
infoMessage "Please provide root privileges for the purpose of environment setup installations." | |
sudo echo "Thanks for root privileges." | |
if [[ "$(docker -v)" ]]; then | |
infoMessage "Would you like to uninstall docker? You will be given an install option next if you uninstall. [Y/N]" | |
read -n 1 -r | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
infoMessage "Removing Docker." | |
sudo apt-get -y remove docker docker-engine docker.io docker.ce | |
fi | |
fi | |
if [[ ! "$(docker -v)" ]]; then | |
infoMessage "Would you like to install docker? [Y/N]" | |
read -n 1 -r | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
infoMessage "Setting up Docker." | |
echo "Updating system packages." | |
sudo apt-get update | |
echo "Installing dependencies." | |
sudo apt -y install apt-transport-https ca-certificates curl software-properties-common | |
echo "Adding GPG key." | |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - | |
echo "Adding docker repository." | |
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | |
apt-cache policy docker-ce | |
echo "Installing docker engine." | |
sudo apt-get -y install docker.ce | |
echo "adding docker group if it doesn't exist." | |
getent group docker || sudo groupadd docker | |
if groups | grep -qw docker; then | |
echo "$USER already belongs to docker group." | |
else | |
if groups ${USER} | grep -qw docker; then | |
echo "$USER is not logged in to docker group." | |
else | |
sudo usermod -aG docker ${USER} | |
echo "$USER did not belong to docker group. Added." | |
fi | |
needsToLoginToDockerGroup=true | |
fi | |
fi | |
fi | |
if [[ "$(which docker-compose)" ]]; then | |
infoMessage "Would you like to uninstall docker-compose? You will be given an install option next if you uninstall. [Y/N]" | |
read -n 1 -r | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
sudo rm -f $(which docker-compose) | |
fi | |
fi | |
if [[ ! "$(which docker-compose)" ]]; then | |
infoMessage "Would you like to install docker-compose? [Y/N]" | |
read -n 1 -r | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
infoMessage "Installing docker-compose" | |
sudo curl -L "https://github.com/docker/compose/releases/download/v2.3.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose | |
sudo chmod +x /usr/local/bin/docker-compose | |
fi | |
fi | |
if [[ ! -f /etc/docker/daemon.json ]]; then | |
infoMessage "Setting docker storage driver." | |
cat <<EOF | sudo tee /etc/docker/daemon.json | |
{ | |
"storage-driver": "overlay2" | |
} | |
EOF | |
fi | |
if [[ ! -f $HOME/.docker/config.json ]]; then | |
infoMessage "Setting up Docker keybindings." | |
mkdir $HOME/.docker | |
cat <<EOF | sudo tee $HOME/.docker/config.json | |
{ | |
"detachKeys": "ctrl-d" | |
} | |
EOF | |
fi | |
infoMessage "Docker setup has finished" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment