Skip to content

Instantly share code, notes, and snippets.

@nsticco
Last active September 6, 2022 23:29
Show Gist options
  • Save nsticco/10abb16a9a708788406c65e0b71cb170 to your computer and use it in GitHub Desktop.
Save nsticco/10abb16a9a708788406c65e0b71cb170 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This script will bootstrap Ubuntu with Docker
###############################################################################
# Customize the script by passing values to the named parameters below, e.g.
# ./bootstrap-ubuntu-docker.sh --user nate
use_current_user=${use_current_user:-true}
user=${user:-ubuntu}
wsl=${wsl:-false}
keygen=${keygen:-true}
docker_compose=${docker_compose:-2.3.3}
while [ $# -gt 0 ]; do
if [[ $1 == *"--"* ]]; then
param="${1/--/}"
declare $param="$2"
# echo $1 $2 # Optional to see the parameter:value result
fi
shift
done
###############################################################################
echo "Removing Docker conflicts if present..."
sudo apt-get -y remove docker docker-engine docker.io containerd runc
echo "Setting up the Docker repo..."
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg --yes
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] \
https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Note that you may get warned about a containerd version issue
# This is why we have to pass the --nobest flag
echo "Installing Docker..."
sudo apt-get -y update
sudo apt-get -y install docker-ce docker-ce-cli containerd.io
echo "Installing Docker Compose..."
sudo curl -L "https://github.com/docker/compose/releases/download/v${docker_compose}/docker-compose-$(uname -s)-$(uname -m)" \
-o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
echo "Adjusting user permissions..."
sudo groupadd docker
if [ $use_current_user = true ] ; then
sudo usermod -aG docker "$(whoami)"
else
sudo usermod -aG docker $user
fi
echo "Updating and cleaning up after bootstrapping..."
sudo apt-get -y update
sudo apt-get -y autoremove
sudo apt-get -y clean
if ! [ $wsl = true ] ; then
echo "Setting Docker to run at boot..."
sudo systemctl enable docker
fi
if [ $keygen = true ] ; then
echo "Checking if ssh key already exists..."
if [ ! -f ~/.ssh/id_rsa ] ; then
echo "Generating a new SSH key..."
ssh-keygen -q -t rsa -b 4096 -N "" -C "$(whoami)@$(hostname) on $(cat /etc/os-release)" \
-f ~/.ssh/id_rsa <<<y 2>&1 >/dev/null
else
echo "Key already exists"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment