From 7ac7d1723343631cbd57b42ca02e9e47a242b0d0 Mon Sep 17 00:00:00 2001 From: Christopher <1289128+dragonfire1119@users.noreply.github.com> Date: Thu, 5 Sep 2024 23:25:13 -0500 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(update-portainer-ce):=20Preser?= =?UTF-8?q?ve=20current=20Portainer=20ports=20(#5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change ensures that the new Portainer container is created with the same ports as the current configuration. It first checks the existing Portainer container to get the current port mappings, and then uses those values when starting the new container. This way, clients do not need to update any of their configurations when the Portainer version is updated. --- update-portainer-ce/run.sh | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/update-portainer-ce/run.sh b/update-portainer-ce/run.sh index 7f06a42..20a9a87 100644 --- a/update-portainer-ce/run.sh +++ b/update-portainer-ce/run.sh @@ -71,6 +71,13 @@ get_current_version() { fi } +# Function to get current Portainer ports +get_current_ports() { + local ports + ports=$(docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}}{{$p}}={{(index $conf 0).HostPort}} {{end}}' portainer 2>/dev/null) + echo "$ports" +} + # Function to get the last three Portainer versions get_last_three_versions() { curl -s https://api.github.com/repos/portainer/portainer/releases | grep -m 3 '"tag_name":' | awk -F'"' '{print $4}' @@ -92,6 +99,19 @@ remove_existing_images() { # Get current Portainer version current_version=$(get_current_version) +# Get current Portainer ports +current_ports=$(get_current_ports) + +# Set default ports if none are found +port1="8000:8000" +port2="9443:9443" + +if [[ ! -z "$current_ports" ]]; then + # Extract ports from the current configuration + port1=$(echo "$current_ports" | grep -o '8000/tcp=[0-9]*' | sed 's/tcp=/:/') + port2=$(echo "$current_ports" | grep -o '9443/tcp=[0-9]*' | sed 's/tcp=/:/') +fi + # Get the last three versions mapfile -t versions < <(get_last_three_versions) @@ -165,11 +185,11 @@ echo "Pulling Portainer image version $version_to_install..." docker pull portainer/portainer-ce:$version_to_install check_command "Failed to pull Portainer image" -# Create and start a new Portainer container +# Create and start a new Portainer container with the same ports echo "Creating and starting a new Portainer container..." docker run -d \ - -p 8000:8000 \ - -p 9443:9443 \ + -p $port1 \ + -p $port2 \ --name portainer \ --restart always \ -v /var/run/docker.sock:/var/run/docker.sock \