Pitel
Pitel

Reputation: 5393

How to check if docker pull actually pulled something

I've the following script to update my Nextcloud:

#!/bin/bash

set -ex

docker pull nextcloud
docker rm -f nextcloud
docker run -d -p 8080:80 -v nextcloud:/var/www/html --name nextcloud --restart=unless-stopped nextcloud
docker image prune -f

The problem is, that it creates new container even when there was noting new to pull.

How can I check, if the docker pull actually pulled something, or if my image is already up-to-date? The exit code of docker pull is 0 in both situations.

Upvotes: 6

Views: 11937

Answers (5)

frankhommers
frankhommers

Reputation: 1305

This is how I do it, by comparing the hash before and after pull.

#!/bin/bash
current_image_hash=$(docker image ls ghcr.io/home-assistant/home-assistant:stable --quiet)
docker pull ghcr.io/home-assistant/home-assistant:stable
after_pull_image_hash=$(docker image ls ghcr.io/home-assistant/home-assistant:stable --quiet)
if [[ $current_image_hash != $after_pull_image_hash ]]; then
  docker stop homeassistant
  docker rm homeassistant
  docker run --restart always -d --name homeassistant ghcr.io/home-assistant/home-assistant:stable
fi

Upvotes: 2

anubhava
anubhava

Reputation: 784868

You can check output of docker pull command:

#!/bin/bash    
set -ex

out=$(docker pull nextcloud)

if [[ $out != *"up to date"* ]]; then
   docker stop nextcloud
   docker rm -f nextcloud
   docker run -d -p 8080:80 -v nextcloud:/var/www/html --name nextcloud -- 
   restart=unless-stopped nextcloud
   docker image prune -f
fi

Upvotes: 7

vimal-k
vimal-k

Reputation: 293

You could get the image id of the locally available version, for ex:

> docker image ls nginx:latest | awk '/nginx/ {print $3}'
> 649dcb69b782

'latest' is the tag used by default.

When you do a docker pull, you could use the command again to check if the image id has changed, and then take action accordingly.

Upvotes: 2

tgogos
tgogos

Reputation: 25122

You might also find this information useful...

From the docs:

Update to a newer version

Updating the Nextcloud container is done by pulling the new image, throwing away the old container and starting the new one. Since all data is stored in volumes, nothing gets lost. The startup script will check for the version in your volume and the installed docker version. If it finds a mismatch, it automatically starts the upgrade process. Don't forget to add all the volumes to your new container, so it works as expected.

$ docker pull nextcloud
$ docker stop <your_nextcloud_container>
$ docker rm <your_nextcloud_container>
$ docker run <OPTIONS> -d nextcloud

Beware that you have to run the same command with the options that you used to initially start your Nextcloud. That includes volumes, port mapping.

Upvotes: 0

gcharbon
gcharbon

Reputation: 1701

First of all if not specifying tag you are implying that you want to pull the latest version. In order to be more clear you could do:

#!/bin/bash

set -ex

docker pull nextcloud:latest
docker rm -f nextcloud
docker run -d -p 8080:80 -v nextcloud:/var/www/html --name nextcloud --restart=unless-stopped nextcloud:latest
docker image prune -f

And if you want to know if you pulled an image or not, I think you're right saying that exit code is always 0, whether docker pull a new image or local image is already up-to-date. But you could capture the ouput of the docker pull command and grep for Status: Image is up to date... or Status: Downloaded newer image...

Something like:

docker pull nextcloud:latest | grep "Image is up to date" && echo "Didn't downloaded anything" || echo "Downloaded new image"

Putting everything together:

#!/bin/bash

set -ex

docker pull nextcloud:latest | grep "Image is up to date" && pull_status="already_pulled" || pull_status="newly_pulled"

if [ "$pull_status" = "newly_pulled" ]; then
docker rm -f nextcloud
docker run -d -p 8080:80 -v nextcloud:/var/www/html --name nextcloud --restart=unless-stopped nextcloud:latest
docker image prune -f
fi

Upvotes: 2

Related Questions