Container Ship

Synology: How do I update an existing Docker container with a new image?

By

in

As always: before you do such an update, make sure to create a backup of all your files. If something goes wrong, this may lead to data loss!

Manual update

To update an existing Docker container manually, the following steps are necessary;

  1. Go to Registry and download new image (mostly the “latest” version)
  2. Go to Container, select the container you need to update and stop it
  3. From Actions menu select “Clear”
    Edit: Under DSM7, the “Clear” command has been renamed “Reset”.
  4. Start the container again

This will clear the complete container and start with the newly downloaded Docker image. Since the data folders are mounted into the container, this will not erase the apllications data. Configurations are also not affected by this.

Note: when updating a major version of gitlab/gitlab-ce, make sure to follow the update paths! This requires updates in smaller steps (minor versions).
In this case, you should not immediatelly download the “latest” version of the image. Select a specific version that follows the update path and repeat this until you reach the “latest” version.

Automatic update of Docker images

Updating a Docker image manually might be fine for a small number of images. But there is a more elegant way by using another Docker container called Watchtower. This one can update Docker containers automatically. The image is called containrrr/watchtower. A simple setup can be performed with the following steps:

  1. Load image containrrr/watchtower in the Docker registry
  2. Sign in with root privilege via SSH
  3. Run the following code in the shell:
docker run -d --network host --name watchtower-once -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower:latest --cleanup --include-stopped --run-once

This will start the Watchtower image and update all container once. The container created for this runs once and can then be found switched off in the list of containers. Now you can start it manually again and again as needed or let it run at certain times via Synology Task Scheduler. The command for the task scheduler is then as follows:

docker start watchtower-once -a

Let Watchtower run permanently

Alternatively you can use the scheduler in Watchtower itself. If you want to start it every Monday at 4 a.m., then enter the following command on the shell:

docker run -e "TZ=Europe/Berlin" -d --restart unless-stopped --network host --name watchtower -v /var/run/docker.sock:/var/run/docker.sock containrrr/watchtower --cleanup --include-stopped --schedule "0 0 4 ? * MON"

It is important to set the time zone to yours because otherwise you will have an offset to UTC. In addition, the container is not terminated but always restarted. Even if it crashed or the NAS was restarted. The last parameter uses the cron syntax for scheduling the task.

Photo by Elias on Unsplash



Comments

4 responses to “Synology: How do I update an existing Docker container with a new image?”

  1. Even Solberg Avatar
    Even Solberg

    This worked beautifully. Thank you so much!

  2. Thank you for this ‘how to auto update’ information! You’re a docker wizard ๐Ÿ™‚

    I instead used Control Panel > Task Scheduler > Create, from the Web Admin GUI and used “Run as Root” and then the user-defined script of “docker start watchtower-once -a” as per your instructions. That way I can more easily manage the schedule and have visibility of the cronjobs running on my NAS without needing to SSH back in again.

    Thanks again

    1. Mathias Lipowski Avatar
      Mathias Lipowski

      Thank you for adding this detail. I didn’t use the Task Scheduler yet, but great to know that this is possible. Indeed, this makes it more easy by just using the Admin Web interface ๐Ÿ˜‰

      Thanks

  3. Thank you for the guide to ‘Manual update’.

    I have seen way more complicated guides to updating a Docker Run container, but yours worked flawlessly.

Leave a Reply

Your email address will not be published. Required fields are marked *