Docker - check private registry image version

2019-03-25 14:30发布

What CLI commands do I need to use in order to check if the image in my private docker registry is a newer version than the one currently running on my server?

E.g. I have a container that I ran using docker run -d my.domain.com:5000/project1

and I would like to know if it is out-of-date.

6条回答
三岁会撩人
2楼-- · 2019-03-25 14:50

You can use a bash script running in a cron scheduled task:

#!/bin/bash

docker_instance='YOUR_RUNNING_INSTANCE'

instance_id=$(docker ps -qa --filter name=$docker_instance)
image_name_tag=$(docker inspect $instance_id | jq -r [] |.Config.Image')

if [ "-${image_name_tag}-" != "--" ]; then

    status=$(docker pull $image_name_tag | grep "Downloaded newer image")
    if [ "-${status}-" != "--" ]; then

        echo ">>> There is one update for this image ... "

        # stop the docker instance
        docker stop $docker_instance

        # remove the docker instance
        docker rm $docker_instance

        # restart the docker using the last command, using the new image from the remote repository
        run-my-docker-instance.sh

    fi
fi
查看更多
该账号已被封号
3楼-- · 2019-03-25 14:57

I don't know if this works as advertised. Just a quick hack I just put together. But this will at least give you a little push on how this might be done.

#!/bin/bash

container=$1
imageid=$(docker inspect --format '{{.Config.Image}}' ${container})

echo "Running version from: $(docker inspect --format '{{.Created}}' ${container})"
echo "Image version from: $(docker inspect --format '{{.Created}}' ${imageid})"

Example output:

[root@server ~]# sh version_check.sh 9e500019b9d4
Running version from: 2014-05-30T08:24:08.761178656Z
Image version from: 2014-05-01T16:48:24.163628504Z
查看更多
相关推荐>>
4楼-- · 2019-03-25 15:05

AFAIK, this is not possible right now.

The only thing I see would be to pull the registry to check if there is a new version of your image (would then have a different ID than your locally stored image):

docker pull your/image:tag

But yes, that would mean fetching the new images (if any).

If you have a look at the registry API documentation, you'll see that if you don't mind scripting a bit, you could get this information without actually downloading the image, by fetching the image tags and check if the returned ID for the tag matches the ID of the local image you have with the same tag.

That being said, having something to "check for updates" integrated into the docker CLI would be a nice addition.

查看更多
女痞
5楼-- · 2019-03-25 15:09

Even when there is no command, you can use the API to check for tags on the registry and compare against what you are running.

$ curl --silent my.domain.com:5000/v1/repositories//project1/tags | grep latest
{"latest": "116f283e4f19716a07bbf48a562588d58ec107fe6e9af979a5b1ceac299c4370"}

$ docker images --no-trunc my.domain.com:5000/project1
REPOSITORY           TAG                 IMAGE ID                                                           CREATED             VIRTUAL SIZE
my.domain.com:5000   latest              64d935ffade6ed1cca3de1b484549d4d278a5ca62b31165e36e72c3e6ab8a30f   4 days ago          583.2 MB

By comparing the ids, you can know that you are not running the latest version.

查看更多
时光不老,我们不散
6楼-- · 2019-03-25 15:13

Brownie points to @mbarthelemy and @amuino who put me on track. From that I was able to come up with the following bash script that others may find useful. It just checks if the tag on the registry is different from the currently executing container.

#!/bin/bash
# ensure running bash
if ! [ -n "$BASH_VERSION" ];then
    echo "this is not bash, calling self with bash....";
    SCRIPT=$(readlink -f "$0")
    /bin/bash $SCRIPT
    exit;
fi


REGISTRY="my.registry.com:5000"
REPOSITORY="awesome-project-of-awesomeness"


LATEST="`wget -qO- http://$REGISTRY/v1/repositories/$REPOSITORY/tags`"
LATEST=`echo $LATEST | sed "s/{//g" | sed "s/}//g" | sed "s/\"//g" | cut -d ' ' -f2`

RUNNING=`docker inspect "$REGISTRY/$REPOSITORY" | grep Id | sed "s/\"//g" | sed "s/,//g" |  tr -s ' ' | cut -d ' ' -f3`

if [ "$RUNNING" == "$LATEST" ];then
    echo "same, do nothing"
else
    echo "update!"
    echo "$RUNNING != $LATEST"
fi
查看更多
7楼-- · 2019-03-25 15:14

Not sure about the version but if you mean the tag of image, it can be easily checked through the registry v2 api . Note that in context of docker images tag has nothing to do with the version of software.

Use curl command in CLI

 curl <docker_host_ip>:<docker_host_port>/v2/<repository_name>/tags/list

To get a list of repositories pushed on the private registry, use

curl <docker_host_ip>:<docker_host_port>/v2/_catalog
查看更多
登录 后发表回答