Created
September 15, 2025 13:20
-
-
Save loftux/fc245e32c35ba70434894e493b26880a to your computer and use it in GitHub Desktop.
Script to create and manage mutliple colima profiles
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 | |
| # Settings used when creating a new profile | |
| # Set the default values for memory, CPU, and disk | |
| # These values can be adjusted as needed | |
| MEMORY=10 | |
| CPU=6 | |
| DISK=50 | |
| # Execute the command 'colima list' and store its output in a variable | |
| data=$(colima list) | |
| profile=$(echo "$data" | awk '$2 == "Running" {print $1}') | |
| # Get the startprofile from parameter | |
| startprofile=$1 | |
| # if startprofile is empty, echo error message and exit | |
| if [ -z "$startprofile" ]; then | |
| echo "No profile name given, set as first parameter" | |
| exit 1 | |
| fi | |
| # If profile is not empty and equals startprofile, ask if it should stop profile | |
| if [ -n "$profile" ] && [ "$profile" = "$startprofile" ]; then | |
| echo | |
| echo "Profile '$profile' is already running." | |
| echo -n "Do you want to stop '$profile'? [y/N] " | |
| read -r answer | |
| if [[ "$answer" =~ ^[Yy]$ ]]; then | |
| echo | |
| echo "Stopping profile: $profile" | |
| echo | |
| colima stop "$profile" | |
| else | |
| echo | |
| echo "Proceeding without stopping '$profile'." | |
| echo | |
| fi | |
| else | |
| # always stop running profile, we just need one running | |
| echo | |
| echo "Stopping any running profile..." | |
| echo | |
| colima stop "$profile" | |
| fi | |
| # Set the DOCKER_HOST environment variable to point to the Colima socket, needs to be set here for docker commands to work§ | |
| export DOCKER_HOST="unix://${HOME}/.colima/${startprofile}/docker.sock" | |
| exists=$(colima list | awk '$1 == "'"$startprofile"'" {print $1}') | |
| if [ -n "$exists" ]; then | |
| echo | |
| echo "Starting $startprofile..." | |
| echo | |
| # Will pick upp settings from ~/.colima/startprofile/colima.yml, can be adjusted there if needed | |
| colima start --profile=$startprofile | |
| else | |
| echo | |
| echo "Setting up new profuile $startprofile..." | |
| echo | |
| colima start --profile=$startprofile --cpu $CPU --memory $MEMORY --disk $DISK --vm-type=vz --mount $PWD:w --mount-type=virtiofs --network-address | |
| echo | |
| echo '************' | |
| echo | |
| # Run portainer | |
| echo "Adding portiner to instance" | |
| echo | |
| docker volume create portainer_data | |
| docker run -d -p 9000:9000 -p 9443:9443 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:lts | |
| fi | |
| # Check if a second parameter is provided and matches true/upgrade/u/U | |
| if [[ "$2" =~ ^(true|upgrade|u|U)$ ]]; then | |
| # Check if portainer container exists | |
| portainer=$(docker ps -a --format "{{.Names}}" --filter name=portainer) | |
| # if portainer exists, if running, stop it | |
| if [ -n "$portainer" ]; then | |
| echo "Stopping portainer container..." | |
| docker stop portainer | |
| fi | |
| # Update colima while portainer is stopped | |
| echo | |
| echo '************' | |
| echo | |
| echo "Update host container" | |
| echo | |
| colima update "$startprofile" | |
| echo | |
| echo "Update Portainer" | |
| echo | |
| # remove portainer container | |
| echo "Removing portainer container..." | |
| docker rm -f portainer | |
| # pull latest portainer image | |
| echo "Pulling latest portainer image..." | |
| docker pull portainer/portainer:lts | |
| # run latest portainer image | |
| echo "Running latest portainer image..." | |
| docker run -d -p 9000:9000 -p 9443:9443 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:lts | |
| fi | |
| echo | |
| echo '************' | |
| echo | |
| echo "You currently have all the following profiles:" | |
| colima list | |
| echo | |
| echo "Docker context list:" | |
| docker context ls | |
| echo | |
| echo "If you have issues with docker, check that the correct context is set." | |
| echo | |
| echo "If needed, set the DOCKER_HOST env variable to the following:" | |
| echo "export DOCKER_HOST=\"unix://${HOME}/.colima/${startprofile}/docker.sock\"" | |
| echo | |
| colima status -p "$startprofile" -e | |
| echo | |
| echo "Disk usage in $startprofile VM:" | |
| colima ssh -p "$startprofile" -- df -h / | |
| echo | |
| echo | |
| docker ps -a -s | |
| echo | |
| echo | |
| echo "Done starting up $startprofile" | |
| echo "Thanks for your patience!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment