Last active
March 16, 2023 09:45
-
-
Save fernandoescolar/882e21f3e8d0ea3ffb092306fff92b95 to your computer and use it in GitHub Desktop.
Install docker using canonical multipass VM in MacOS (an alternative to docker desktop)
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
NM=$1 | |
function create_setup() { | |
cat << EOF > setup-vm.sh | |
#!/bin/bash | |
function get_architecture() { | |
arch=\$(uname -m) | |
if [ \$arch == "x86_64" ]; then | |
echo "amd64" | |
elif [ \$arch == "aarch64" ]; then | |
echo "arm64" | |
else | |
echo \$arch | |
fi | |
} | |
sudo apt-get update | |
sudo apt-get upgrade -y | |
sudo apt-get -y install apt-transport-https ca-certificates curl gnupg-agent software-properties-common | |
sudo apt-key fingerprint 0EBFCD88 | |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - | |
sudo add-apt-repository "deb [arch=\$(get_architecture)] https://download.docker.com/linux/ubuntu \$(lsb_release -cs) stable" | |
sudo apt-get update | |
sudo apt-get upgrade -y | |
sudo apt-get install -y docker-ce docker-ce-cli containerd.io | |
sudo groupadd docker | |
sudo usermod -aG docker ubuntu | |
sudo systemctl enable docker | |
EOF | |
} | |
function clean_setup() { | |
rm -f setup-vm.sh | |
} | |
function create_vm() { | |
echo "Creating VM $NM" | |
multipass launch --name ${NM} -d 32G focal | |
} | |
function configure_vm() { | |
create_setup | |
multipass transfer setup-vm.sh ${NM}:/home/ubuntu/setup-vm.sh | |
pk=$(cat ~/.ssh/id_rsa.pub) | |
command="echo "$pk" >> /home/ubuntu/.ssh/authorized_keys" | |
multipass exec ${NM} -- sh -c ${command} | |
clean_setup | |
} | |
if [ -z $NM ]; then | |
echo "You should specify a name for the VM (e.g. docker)" | |
exit 1 | |
fi | |
if [ -z "$(docker context ls)" ]; then | |
echo "You should install docker in your machine" | |
echo "e.g. brew install docker" | |
exit 1 | |
fi | |
if [ -z "$(multipass version)" ]; then | |
echo "You should install multipass in your machine" | |
echo "e.g. brew install --cask multipass" | |
exit 1 | |
fi | |
filter=$(echo "^${NM}") | |
already_exists=$(multipass list --format csv | tail -n +2 | cut -d ',' -f 1 | grep $filter) | |
if [ -z $already_exists ]; then | |
create_vm | |
fi | |
state=$(multipass list --format csv | tail -n +2 | grep $filter | cut -d ',' -f 2) | |
if [ $state == "Deleted" ]; then | |
create_vm | |
fi | |
if [ $state == "Stopped" ]; then | |
multipass start ${NM} | |
fi | |
state=$(multipass list --format csv | tail -n +2 | grep $filter | cut -d ',' -f 2) | |
if [ $state != "Running" ]; then | |
echo "VM is not running" | |
exit 1 | |
fi | |
configure_vm | |
multipass exec ${NM} -- bash -x /home/ubuntu/setup-vm.sh | |
already_exists=$(docker context ls | tail -n +2 | cut -d ' ' -f 1 | grep multipass) | |
if [ -z $already_exists ]; then | |
ip=$(multipass list --format csv | tail -n +2 | grep $filter | cut -d ',' -f 3) | |
echo "test ssh (accept using id_rsa)" | |
ssh ubuntu@${ip} 'echo "ssh ok"' | |
echo "Setting up docker context" | |
docker context create multipass --docker "host=ssh://ubuntu@${ip}" | |
echo "Mounting home in vm" | |
multipass mount ~ ${NM} | |
fi | |
docker context use multipass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment