Bash解析Docker状态以检查本地映像是否最新 [英] Bash parse docker status to check if local image is up to date

查看:101
本文介绍了Bash解析Docker状态以检查本地映像是否最新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有一个开始的docker脚本:

I have a starting docker script here:

#!/usr/bin/env bash
set -e

echo '>>> Get old container id'
CID=$(sudo docker ps --all | grep "web-client" | awk '{print $1}')
echo $CID


echo '>>> Stopping and deleting old container'
if [ "$CID" != "" ];
then
  sudo docker stop $CID
  sudo docker rm $CID
fi


echo '>>> Starting new container'
sudo docker pull my-example-registry.com:5050/web-client:latest
sudo docker run --name=web-client -p 8080:80 -d my-example-registry.com:5050/web-client:latest

事实是此脚本的结果不合适。每次运行脚本时,它都会删除旧容器。

The fact is this script has umproper result. It deletes the old container everytime the script is run.

启动新容器部分将提取最新的映像。如果本地镜像是最新的,这是docker pull的示例输出:

The "starting new container" section will pull the most recent image. Here is an example output of docker pull if the image locally is up to date:


状态:镜像是最新的,价格为
my-example-registry:5050 / web-client:latest

Status: Image is up to date for my-example-registry:5050/web-client:latest

是否可以通过添加条件来改善我的脚本:

Is there any way to improve my script by adding a condition:

在进行任何操作之前,请先通过docker pull检查本地映像是注册表上可用的最新版本。然后,如果它是最新版本,则继续执行停止操作并删除旧的容器操作,然后docker运行新的提取映像。

Before anything, check via docker pull the local image is the most recent version available on registry. Then if it's the most recent version, proceed the stop and delete old container action and docker run the new pulled image.

在此脚本中,如何解析检查本地映像的状态是否与注册表上可用的最新状态相对应?

也许docker命令可以解决问题,但我没有

Maybe a docker command can do the trick, but I didn't manage to find a useful one.

推荐答案

检查字符串图像是最新的以了解本地图像是否已更新:

Check the string "Image is up to date" to know whether the local image was updated:

sudo docker pull my-example-registry.com:5050/web-client:latest | 
   grep "Image is up to date" ||
   (echo Already up to date. Exiting... && exit 0)

因此将脚本更改为:

#!/usr/bin/env bash
set -e

sudo docker pull my-example-registry.com:5050/web-client:latest | 
   grep "Image is up to date" ||
   (echo Already up to date. Exiting... && exit 0)


echo '>>> Get old container id'
CID=$(sudo docker ps --all | grep "web-client" | awk '{print $1}')
echo $CID


echo '>>> Stopping and deleting old container'
if [ "$CID" != "" ];
then
  sudo docker stop $CID
  sudo docker rm $CID
fi


echo '>>> Starting new container'
sudo docker run --name=web-client -p 8080:80 -d my-example-registry.com:5050/web-client:latest

这篇关于Bash解析Docker状态以检查本地映像是否最新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆