← scripts 5 min read
Contents

How to Automatically Update Portainer to the Latest Version Using a Bash Script

Portainer is a powerful container management platform that simplifies working with Docker environments. However, as with any software, it's essential to keep it up-to-date to ensure security, performance improvements, and new features. In this article, we'll walk through the process of creating a Bash script to automatically update Portainer to the latest version whenever you run it.

Why Automate Portainer Updates?

Manually updating Portainer can be a time-consuming process. It involves stopping the current container, removing it, pulling the latest image, and recreating the container. Automating this process ensures you always have the most up-to-date version of Portainer with minimal effort.

Benefits of automation:

Step-by-Step Guide to Automating Portainer Updates

1. Prerequisites

Before we dive into creating the script, ensure that you have the following:

2. The Bash Script for Portainer Updates

Here’s the script that will automatically stop your existing Portainer container, pull the latest version of the image, and recreate the container with the same configuration:

#!/bin/bash

# Check if the Portainer container is running
if [ $(docker ps -a -q -f name=portainer) ]; then
    echo "Portainer container exists. Stopping and removing it for an update..."
    docker stop portainer
    docker rm portainer
fi

# Pull the latest Portainer image
echo "Pulling the latest Portainer image..."
docker pull portainer/portainer-ce:latest

# Run the latest version of Portainer
echo "Creating and starting the updated Portainer container..."
docker run -d -p 8000:8000 -p 9000:9000 \
    --name portainer \
    --restart=always \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v portainer_data_fix:/data \
    portainer/portainer-ce:latest

echo "Portainer has been updated and started successfully."

3. How the Script Works

a. Checking for an Existing Container

The script first checks if a Portainer container already exists on your system by using the docker ps -a -q -f name=portainer command. If the container is found, the script proceeds to stop it using docker stop and remove it with docker rm.

b. Pulling the Latest Portainer Image

Once the existing Portainer container has been stopped and removed, the script pulls the latest version of the Portainer image from Docker Hub using docker pull portainer/portainer-ce:latest. This ensures that you're always running the most up-to-date version of Portainer.

c. Running the Updated Portainer Container

After pulling the latest image, the script recreates the Portainer container using the docker run command. The command specifies the following:

d. Completion

Finally, the script outputs that Portainer has been updated and started successfully.

4. How to Implement the Script Yourself

Now that you understand how the script works, you can follow these steps to implement it on your system.

a. Create the Script

  1. Open your terminal and create a new script file using a text editor like nano:

    nano update_portainer.sh
  2. Copy and paste the following script into the file:

    #!/bin/bash
    
    # Check if the Portainer container is running
    if [ $(docker ps -a -q -f name=portainer) ]; then
        echo "Portainer container exists. Stopping and removing it for an update..."
        docker stop portainer
        docker rm portainer
    fi
    
    # Pull the latest Portainer image
    echo "Pulling the latest Portainer image..."
    docker pull portainer/portainer-ce:latest
    
    # Run the latest version of Portainer
    echo "Creating and starting the updated Portainer container..."
    docker run -d -p 8000:8000 -p 9000:9000 \
        --name portainer \
        --restart=always \
        -v /var/run/docker.sock:/var/run/docker.sock \
        -v portainer_data_fix:/data \
        portainer/portainer-ce:latest
    
    echo "Portainer has been updated and started successfully."
  3. Save and exit the file by pressing CTRL + X, then Y, and Enter.

b. Make the Script Executable

You need to give the script execute permissions:

chmod +x update_portainer.sh

c. Run the Script

Now you can run the script whenever you want to update Portainer:

./update_portainer.sh

The script will check for the existing Portainer container, update it, and restart it with the latest version.

5. Scheduling Automatic Updates (With a Warning!)

If you'd like this script to run automatically at regular intervals, you can schedule it using cron. However, be cautious when setting up automatic updates for Portainer.

⚠️ Warning:

Automating updates through cron can cause issues if an unstable or problematic version of Portainer is installed. It's essential to only use this automation for stable releases and to verify the stability of the latest version before upgrading. Automatically upgrading without checking could cause Portainer to fail or misbehave, leading to service interruptions or loss of functionality.

To schedule automatic updates safely, consider checking Portainer’s release notes or community feedback before running the upgrade script.

a. Setting Up a Cron Job

  1. Open the cron job configuration:

    crontab -e
  2. Add the following line to run the script every day at 2 AM (adjust the time as needed):

    0 2 * * * /path/to/update_portainer.sh
  3. Save and exit the crontab file. The script will now run at the specified time every day, ensuring that your Portainer installation is always up-to-date.

Conclusion

Automating Portainer updates with a simple Bash script ensures that you’re always running the latest version with minimal downtime and effort. However, it’s essential to stay cautious and only update to stable releases, as automatically installing a problematic version could result in service disruption. Always verify the latest version before upgrading to avoid issues!.