# 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.

- Autore / Author: Diego Betto (https://diegobetto.com/about/)
- Pubblicato / Published: 2022-06-27
- URL: https://diegobetto.com/en/portainer-update/
- Tag: portainer, docker, management, tools

[Portainer](https://www.portainer.io/) 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:

```bash
#!/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:

```bash
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](https://github.com/portainer/portainer/releases): 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:

```bash
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](https://diegobetto.com/wpdockerize-wordpress-on-docker/) too.

Have a good one!
