Skip to content

Replace docker compose v2 and docker compose switch to compose v5 #39

@AlexElizard

Description

@AlexElizard

Summary

Drop compose switch and use compose v5

Detailed description

Drop compose switch and use compose v5

Alternatives considered

I installed compose via UserScripts (generated by DeepSeek).
This script works correctly and is scheduled to run "At Startup on Array"

#!/bin/bash

# Script for installing or updating Docker Compose (the Docker CLI plugin) to the latest stable version
# Used via the UserScripts plugin on Unraid

set -e

GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

info() { echo -e "${GREEN}[INFO]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1" && exit 1; }

# Defining the architecture
ARCH=$(uname -m)
case $ARCH in
    x86_64) ARCH_SUFFIX="linux-x86_64" ;;
    aarch64) ARCH_SUFFIX="linux-aarch64" ;;
    armv7l) ARCH_SUFFIX="linux-armv7" ;;
    *) error "Unsupported architecture: $ARCH" ;;
esac

# Directory for Docker plugins (system)
PLUGIN_DIR="/usr/local/lib/docker/cli-plugins"
BIN_PATH="$PLUGIN_DIR/docker-compose"

# Check for Docker
if ! command -v docker >/dev/null 2>&1; then
    error "Docker is not installed. Install Docker before using this script."
fi

# Checking the required utilities
command -v curl >/dev/null 2>&1 || error "curl is not installed."
command -v sed >/dev/null 2>&1 || error "sed is not installed."
command -v sort >/dev/null 2>&1 || error "sort is not installed."
command -v sha256sum >/dev/null 2>&1 || error "sha256sum is not installed."

# Function for getting the current version of the plugin (docker compose)
get_current_version() {
    if docker compose version >/dev/null 2>&1; then
        docker compose version 2>/dev/null | sed -nE 's/.*version v?([0-9.]+).*/\1/p'
    else
        echo ""
    fi
}

# Function to get the latest stable version from GitHub tags
get_latest_version() {
    curl -s "https://api.github.com/repos/docker/compose/tags?per_page=100" | \
        grep '"name":' | \
        sed -E 's/.*"([^"]+)".*/\1/' | \
        grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | \
        sed 's/^v//' | \
        sort -V | \
        tail -n1
}

# Function to install the specified version
install_version() {
    local version="$1"
    local download_url="https://github.com/docker/compose/releases/download/v${version}/docker-compose-${ARCH_SUFFIX}"
    local checksum_url="${download_url}.sha256"

    info "Downloading Docker Compose version ${version} for architecture ${ARCH}..."
    local tmp_file=$(mktemp)
    if ! curl -L -s --fail "$download_url" -o "$tmp_file"; then
        rm -f "$tmp_file"
        error "Error downloading binary file. Check your connection."
    fi

    info "Downloading SHA256 checksum file..."
    local checksum_file=$(mktemp)
    if ! curl -L -s --fail "$checksum_url" -o "$checksum_file"; then
        rm -f "$tmp_file" "$checksum_file"
        error "Error downloading checksum file. Check your connection."
    fi

    info "Verifying SHA256 checksum..."
    local expected_hash=$(cat "$checksum_file" | awk '{print $1}')
    local actual_hash=$(sha256sum "$tmp_file" | awk '{print $1}')

    if [ "$expected_hash" != "$actual_hash" ]; then
        rm -f "$tmp_file" "$checksum_file"
        error "SHA256 checksum mismatch! Expected: $expected_hash, Got: $actual_hash"
    fi
    info "Checksum verification passed."

    # Creating the plugin directory if it doesn't exist
    if [ ! -d "$PLUGIN_DIR" ]; then
        info "Creating $PLUGIN_DIR..."
        mkdir -p "$PLUGIN_DIR" || { rm -f "$checksum_file"; error "Failed to create $PLUGIN_DIR. Root privileges may be required."; }
    fi

    info "Installing plugin to $BIN_PATH..."
    chmod +x "$tmp_file"
    if ! mv "$tmp_file" "$BIN_PATH"; then
        rm -f "$checksum_file"
        error "Failed to move binary to $BIN_PATH. Possibly insufficient permissions."
    fi

    # Clean up checksum file
    rm -f "$checksum_file"
    info "Plugin installed successfully."
}

# Main logic
info "Checking for the presence of the Docker Compose plugin (docker compose)..."
current_version=$(get_current_version)

if [ -z "$current_version" ]; then
    info "The docker compose plugin was not found. The latest stable version will be installed."
    latest_version=$(get_latest_version)
    if [ -z "$latest_version" ]; then
        error "Could not determine the latest stable version from GitHub."
    fi
    info "Latest stable version: $latest_version"
    install_version "$latest_version"
else
    info "The docker compose plugin exists. Current version: $current_version"
    info "Checking for updates..."
    latest_version=$(get_latest_version)
    if [ -z "$latest_version" ]; then
        error "Could not determine the latest stable version from GitHub."
    fi
    info "Latest stable version: $latest_version"

    if [ "$current_version" != "$latest_version" ]; then
        info "Update found: New version $latest_version, installed version $current_version"
        install_version "$latest_version"
    else
        info "The latest stable version is installed. No update required."
    fi
fi

# Final check
final_version=$(get_current_version)
info "Done. Current version: $final_version"

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions