Automate Docker, Compose & Portainer Updates

Saurab Thakur Saurab Thakur
⏳ 3 min read

If you're running a home server or NAS using OpenMediaVault or any Debian-based system, keeping Docker and Portainer up to date is essential for security and performance. In this guide, you'll learn how to create a shell script to automate updates and schedule it to run every Sunday at 3:30 AM using cron. You'll also set a daily shutdown at 4:00 AM to save electricity.

πŸ“¦ Step 1: Create the Update Script

  1. Create the script file using nano:
  2. nano /usr/local/bin/update-docker-stack.sh
    <li><strong>Copy and paste the following script</strong> into the file:</li>
    #!/bin/bash
    
    echo "πŸ”„ Updating Docker on Debian/OMV..."
    
    sudo apt update
    sudo apt install --only-upgrade -y docker-ce docker-ce-cli containerd.io
    sudo systemctl restart docker
    
    echo "βœ… Docker version:"
    docker --version
    
    echo "πŸ”„ Updating Docker Compose..."
    DOCKER_COMPOSE_PATH="/usr/local/bin/docker-compose"
    if [ -f "$DOCKER_COMPOSE_PATH" ]; then
        sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o $DOCKER_COMPOSE_PATH
        sudo chmod +x $DOCKER_COMPOSE_PATH
        docker-compose --version
    else
        echo "⚠️ Docker Compose binary not found at $DOCKER_COMPOSE_PATH"
    fi
    
    echo "πŸ”„ Updating Portainer..."
    docker stop portainer && docker rm portainer
    docker image rm portainer/portainer-ce
    docker pull portainer/portainer-ce:latest
    
    docker run -d \
      -p 8000:8000 -p 9443:9443 \
      --name portainer \
      --restart=always \
      -v /var/run/docker.sock:/var/run/docker.sock \
      -v portainer_data:/data \
      portainer/portainer-ce:latest
    
    echo "βœ… Portainer updated!"
    <li><strong>Save the script</strong> and exit <code>nano</code> by pressing:</li>
    CTRL + X
    <p>Then press <code>Y</code> to confirm saving, followed by <code>Enter</code> to confirm the file name.</p>
    
    <li><strong>Make the script executable</strong>:</li>
    sudo chmod +x /usr/local/bin/update-docker-stack.sh

🎯 Run the Script

To update Docker, Docker Compose, and Portainer:

sudo update-docker-stack.sh

πŸ—“οΈ Step 2: Schedule Weekly Auto Update Using Cron

Now schedule the script to run every Sunday at 3:30 AM using cron.

πŸ“Œ Edit Root Crontab

sudo crontab -e

πŸ•’ Add This Line

30 3 * * 0 /usr/local/bin/update-docker-stack.sh >> /var/log/docker-update.log 2>&1

This means:

  • 30 3: Run at 3:30 AM
  • * * 0: Every Sunday

The output will be saved to /var/log/docker-update.log for future reference.

⚑ Step 3: Daily Automatic Shutdown at 4:00 AM

To save energy, you might want your server to shut down daily at 4:00 AM.

🧾 Add Another Cron Entry

sudo crontab -e

And add this line:

0 4 * * * /sbin/shutdown -h now

This command will halt the system every day at 4:00 AM.

πŸ“‹ Final Thoughts

With this setup, your server will stay up-to-date with the latest Docker ecosystem updates and shut down gracefully each day to conserve power. This is perfect for home NAS users, Raspberry Pi setups, or OpenMediaVault users who don’t need 24/7 uptime.

Want to receive a Telegram alert after the update finishes? Stay tuned for our next guide on integrating Telegram bots into your scripts!

Comments

Loading comments...