我有一个脚本,我运行它来更新图像,然后再次stop
-> rm
->run
它们。不幸的是,即使pull
没有更新图像(即它已经是最新的),最后 3 个步骤也会发生。我如何更改我的 bash 脚本,以便它仅在 pull 更新图像时运行最后 3 个命令?
#!/usr/bin/env bash
set -e
docker pull linuxserver/sonarr
docker stop sonarr
docker rm sonarr
./run-sonarr.sh
docker pull linuxserver/radarr
docker stop radarr
docker rm radarr
./run-radarr.sh
docker pull linuxserver/nzbget
docker stop nzbget
docker rm nzbget
./run-nzbget.sh
docker pull plexinc/pms-docker
docker stop plex
docker rm plex
./run-plex.sh
答案1
你可以检查字符串“Image is up to date”是否不是展示:
...
export LANG=C
if ! docker pull linuxserver/sonarr | grep "Image is up to date"; then
docker stop sonarr
docker rm sonarr
./run-sonarr.sh
fi
...
答案2
您可以获取拉取前后的镜像 ID 并进行比较:
docker images --format '{{.ID}}' <your image>
例如:
# ID of current image
~/ >docker images --format '{{.ID}}' tomcat:latest
d636936d0d85
# Pull it (updated this time)
~/ >docker pull tomcat:latest
latest: Pulling from library/tomcat
05d1a5232b46: Pull complete
5cee356eda6b: Pull complete
89d3385f0fd3: Pull complete
65dd87f6620b: Pull complete
78a183a01190: Pull complete
1a4499c85f97: Pull complete
2c9d39b4bfc1: Pull complete
1b1cec2222c9: Pull complete
fc95b85a81f3: Pull complete
0f3868647539: Pull complete
9b6a6eddb2d9: Pull complete
8787183cb077: Pull complete
Digest: sha256:d789f1dc613ef36a24c29f36bdef4891907cd854a5723c73acf6ba78dfc9ac78
Status: Downloaded newer image for tomcat:latest
# ID of new image (different)
~/ >docker images --format '{{.ID}}' tomcat:latest
41a54fe1f79d
# Pull again
~/ >docker pull tomcat:latest
latest: Pulling from library/tomcat
Digest: sha256:d789f1dc613ef36a24c29f36bdef4891907cd854a5723c73acf6ba78dfc9ac78
Status: Image is up to date for tomcat:latest
# Same ID
~/ >docker images --format '{{.ID}}' tomcat:latest
41a54fe1f79d