Last active
March 28, 2025 22:11
-
-
Save dcdeve/b7e31260d8811128ee10a1eef78a8de3 to your computer and use it in GitHub Desktop.
A script to set up a Docker wrapper that ensures Colima is activated automatically when running Docker commands.
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 | |
# Comprobar si colima está instalado | |
if ! command -v colima &> /dev/null; then | |
echo "Colima no está instalado. Por favor, instálalo y vuelve a intentar." | |
exit 1 | |
fi | |
# Determinar el archivo de configuración basado en el shell | |
if [[ "$SHELL" == *zsh ]]; then | |
config_file=~/.zshrc | |
elif [[ "$SHELL" == *bash ]]; then | |
config_file=~/.bash_profile # Usar .bash_profile como predeterminado | |
# Verificar si .bashrc existe y dar prioridad si es necesario | |
if [ -f ~/.bashrc ]; then | |
config_file=~/.bashrc | |
fi | |
else | |
echo "Error: Shell no soportado. Este script solo es compatible con Zsh y Bash." | |
exit 1 | |
fi | |
# Preguntar por la ruta de destino | |
read -p "Ingrese la carpeta donde desea crear el script (dejar en blanco para usar ~/bin): " target_dir | |
# Usar ~/bin si no se proporciona un directorio | |
if [ -z "$target_dir" ]; then | |
target_dir=~/bin | |
fi | |
# Crear la carpeta si no existe y verificar el éxito | |
mkdir -p "$target_dir" || { echo "Error al crear la carpeta."; exit 1; } | |
# Crear el script docker_wrapper.sh | |
cat << 'EOF' > "$target_dir/docker_wrapper.sh" | |
#!/bin/bash | |
# Comprobar si Colima está iniciado | |
if ! colima status 2>&1 | grep -q "is running"; then | |
echo "Colima no está iniciado. Iniciando Colima..." | |
colima start | |
# Esperar a que Colima se inicie correctamente | |
while true; do | |
if colima status 2>&1 | grep -q "is running"; then | |
echo "Colima ha sido iniciado." | |
break | |
fi | |
sleep 1 | |
done | |
fi | |
# Ejecutar el comando de docker que se pasa como argumento al script | |
command docker "$@" | |
EOF | |
# Hacer el script ejecutable | |
chmod +x "$target_dir/docker_wrapper.sh" || { echo "Error al hacer el script ejecutable."; exit 1; } | |
# Agregar el alias al archivo de configuración | |
if grep -q "alias docker=" "$config_file"; then | |
read -p "El alias 'docker' ya existe en $config_file. ¿Desea reemplazarlo? (s/n): " replace | |
if [[ "$replace" =~ ^[sS]$ ]]; then | |
sed -i '' "s|alias docker=.*|alias docker='$target_dir/docker_wrapper.sh'|" "$config_file" | |
else | |
echo "No se modificó el alias." | |
exit 0 | |
fi | |
else | |
echo "# This alias runs the docker_wrapper script that ensures Colima is activated" >> "$config_file" | |
echo "alias docker='$target_dir/docker_wrapper.sh'" >> "$config_file" | |
fi | |
echo "El alias para Docker ha sido agregado a $config_file." | |
# Recargar el archivo de configuración | |
echo "Recargando tu archivo de configuración..." | |
source "$config_file" | |
# Mensaje final | |
echo "El script docker_wrapper.sh ha sido creado en $target_dir." | |
echo "El archivo de configuración ha sido recargado. Ahora puedes usar el comando 'docker'!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to install?
/bin/bash -c "$(curl -fsSL https://gist.githubusercontent.com/dcdeve/b7e31260d8811128ee10a1eef78a8de3/raw/)"