Diego Betto
Docker and Portainer

Diego Betto · June 27, 2022 · 2 min di lettura

Portainer: updating your Docker instance the easy way

How to safely update your Portainer instance with a simple bash script, without losing containers or volumes.

Condividi:XLinkedInFacebookWhatsApp

Portainer is a really handy tool for managing your Docker containers, volumes, and networks from a web interface.

One thing that can be scary at first is how to update Portainer. It’s actually very simple and you shouldn’t lose any data.

I use Linux Ubuntu/Debian, so things might be slightly different in your case, but it should be easy to adapt the script. You can create a simple script like this:

#!/bin/bash
sudo docker pull portainer/portainer-ce
sudo docker stop portainer
sudo docker rm portainer

sudo docker run --name portainer --restart=unless-stopped -d -p 8000:8000 -p 9000:9000 \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v portainer_data:/data portainer/portainer-ce

The script takes care of:

  • updating the local Portainer image
  • stopping the container
  • removing it (not the volume with the data)
  • starting the new container

You can save it in ~/.local/bin under the name update-portainer and then give it execute permissions (with chmod +x ~/.local/bin/update-portainer).

From then on, just run update-portainer from bash and your Portainer container will be updated.

Before updating: an extra volume backup

The script above never touches the portainer_data volume, so your data stays safe across regular updates. If you still want a safety backup before a significant update (e.g. a major bump), you can export the volume to a tarball:

docker run --rm -v portainer_data:/data -v $(pwd):/backup alpine \
    tar czf /backup/portainer_data_backup.tar.gz -C /data .

Check the release notes before updating

Before running the script, take a look at the Portainer release notes: a major version sometimes ships breaking configuration changes or requires a manual internal database migration.

If you’re already on Docker Compose

If your Portainer is already managed via a docker-compose.yml instead of a standalone docker run, updating it is just:

docker compose pull portainer
docker compose up -d portainer

Same result, even shorter command.

Easy. If you’re also running WordPress on Docker, I’ve written a guide on how to build the stack with wpdockerize too.

Have a good one!

Condividi:XLinkedInFacebookWhatsApp
Diego Betto

Written by

Diego Betto

Co-Founder & CTO at PAPION. Senior full-stack engineer specializing in React, TypeScript, Node.js, and application security.