docker-compose,如何检查容器是否正在运行最后一个映像?然后重新启动

时间:2018-09-11 21:35:20

标签: docker docker-compose

完成拉取之后,我正在尝试检查容器是否正在运行最后可用的图像

带有这样的内容

for container in $(docker-compose ps -q)
do
    oldImage=$(docker inspect $container -f "{{.Image}}" | sed 's/sha256://')
    imageName=$(docker inspect $container -f "{{.Config.Image}}")
    newImage=$(docker image inspect $imageName -f "{{.Id}}" | sed 's/sha256://')
    if [[ "$newImage" != "$oldImage" ]]; then
        echo "new image found $imageName"
        echo "new image $newImage"
        echo "old image $oldImage"
        echo "restaring ... "
    fi
done

我可以检查容器的映像是否与可用的新映像不同,但是我没有重新启动它的服务名称。

一些想法?

1 个答案:

答案 0 :(得分:2)

我找到了这个解决方案

#!/bin/bash
set -e

for serviceName in $(docker-compose config --services)
do
    container=$(docker-compose ps -q $serviceName)
    if [[ "$container" == "" ]]; then
        continue
    fi
    oldImage=$(docker inspect $container -f "{{.Image}}" | sed 's/sha256://')
    imageName=$(docker inspect $container -f "{{.Config.Image}}")
    newImage=$(docker image inspect $imageName -f "{{.Id}}" | sed 's/sha256://')
    if [[ "$newImage" != "$oldImage" ]]; then
        echo "new image found $imageName"
        echo "new image $newImage"
        echo "old image $oldImage"
        echo "restaring ... $serviceName"
        set -v
        docker-compose stop $serviceName
        docker-compose up -d $serviceName
        set -nv
    fi
done