因本地倉庫中Docker鏡像(image)已經很多,每次更新鏡像都需要逐個操作,耗時耗力。便想通過Shell Script實現image的自動更新及<none>的自動刪除。

Analysis

  1. Docker daemon啓動後,通過docker images獲取本地images列表;
  2. 通過awk將列表中的REPOSITORYTAG拼接成REPOSITORY:TAG形式;
  3. 通過docker pull命令從registry中拉取最新的image;
  4. image更新後會產生名爲<none>的image,通過awk提取其IMAGE ID;
  5. 通過docker rmi命令,移除名爲<none>的image;

Script

腳本下載 GitHub

#!/bin/bash
#update docker images and delete <none>
#writer: lempstacker
#create_time: 2016.07.25 08:58 Thur Asia/Shanghai
#update_time: 2016.12.21 15:50 Wed Asia/Shanghai

#reload or restart docker daemon
sudo systemctl start docker &> /dev/null

# Check Internet Connection
! ping -q -w 1 -c 1 `ip route | sed -r -n '/default/s@.*via (.*) dev.*@\1@p'` &> /dev/null && printf "%s\n" "Error: No Internet Connection, Please Check It!" && exit 1

#update docker images exclude which name is heREPOSITORY,hexo,<none>
for i in `docker images | awk '$1~/[^hexo|REPOSITORY|<none>]/&&$2~/[^<none>]/{printf("%s:%s\n",$1,$2)}'`;do
    docker pull $i;
done

#remove images which name is <none>
for j in `docker images | awk '$2~/<none>/{print $3}'`;do
    docker rmi -f $j;
done

#show docker image lists
docker images

#Script End

Experiment

操作過程

lemp@LempStacker:~$ bash ~/Desktop/dockerImageUpdate.sh
latest: Pulling from library/percona
Digest: sha256:e0e49a0782502e23bb1a1819cbe167deea646772e61d143aa0533adf265bed92
Status: Image is up to date for percona:latest
10.1: Pulling from library/mariadb
Digest: sha256:8f880226fb713d1ef3c320743a37dd2ed156cae07a6b0d0b8f32c247eaa06838
Status: Image is up to date for mariadb:10.1
latest: Pulling from prom/prometheus
385e281300cc: Already exists
a3ed95caeb02: Already exists
e418e02f5f37: Already exists
b8a25de1d9dd: Already exists
833f33bab667: Already exists
462231e47595: Already exists
e95984c52371: Already exists
2f24814359ea: Already exists
Digest: sha256:a64323c501bcbe02cac64195a3d33deb9207f3cc0152364a0452bec3ea0a8dca
Status: Image is up to date for prom/prometheus:latest
latest: Pulling from library/django
Digest: sha256:212196c2c7854e10b29aa417246690fbb029011131358fd1f3949d9489062b9e
Status: Image is up to date for django:latest
python3-onbuild: Pulling from library/django
Digest: sha256:3e4012544aea9048c2d470b2ae2a6d27d12aa0fda0b2d4fc82648051d7e797c5
Status: Image is up to date for django:python3-onbuild
3.5: Pulling from library/python
Digest: sha256:27cae89ba088e51a5c169f019ca6760d24a4e01c8453052631803a4785eda781
Status: Image is up to date for python:3.5
7: Pulling from library/centos
Digest: sha256:7b754086d2c7d74ac39dc0a2545d7b06d4266f873d502feb5b3e8bfca27c5dd8
Status: Image is up to date for centos:7
2.7: Pulling from library/python
Digest: sha256:3e367b131e6f56799698b8fb4b1396c838c42e313ad79165bc0f7823f83aa5a4
Status: Image is up to date for python:2.7
5.6-fpm: Pulling from library/php
Digest: sha256:5b25e479ea9427d302ebcf9eb6841e0fd3aa71e31383fe3cc9b1f51b58192c02
Status: Image is up to date for php:5.6-fpm
7-fpm: Pulling from library/php
Digest: sha256:44be948b8778d8446717257f7b45583a5a579c01cf6067ab3b92e7ee43f0ef62
Status: Image is up to date for php:7-fpm
latest: Pulling from library/mongo
Digest: sha256:fc89af57055910959cd94c3f852150fc0dafc95e2b081b57d109711dbcf0d506
Status: Image is up to date for mongo:latest
latest: Pulling from library/node
Digest: sha256:a1fb78aa6540650cc5856f2a9ad0f52458cfa9595cf49d874ac9eea9d1f7fb1e
Status: Image is up to date for node:latest
6.8: Pulling from library/centos
Digest: sha256:fd9058a6149809b2f4725bb4461294ceebb59e587435f3509c2c8dcc9ee1d5b4
Status: Image is up to date for centos:6.8
latest: Pulling from library/hello-world
Digest: sha256:0256e8a36e2070f7bf2d0b0763dbabdd67798512411de4cdcf9431a1feb60fd9
Status: Image is up to date for hello-world:latest
latest: Pulling from library/alpine
Digest: sha256:3dcdb92d7432d56604d4545cbd324b14e647b313626d99b889d0626de158f73a
Status: Image is up to date for alpine:latest
latest: Pulling from zabbix/zabbix-3.0
Digest: sha256:4cf1ced2307d545c3441c21d1ee8b450361e2f0f9e23900d73916b61fd031873
Status: Image is up to date for zabbix/zabbix-3.0:latest
latest: Pulling from library/mysql
Digest: sha256:a9a5b559f8821fe73d58c3606c812d1c044868d42c63817fa5125fd9d8b7b539
Status: Image is up to date for mysql:latest
jessie: Pulling from library/debian
Digest: sha256:8b1fc3a7a55c42e3445155b2f8f40c55de5f8bc8012992b26b570530c4bded9e
Status: Image is up to date for debian:jessie
latest: Pulling from zabbix/zabbix-db-mariadb
a3ed95caeb02: Already exists
da71393503ec: Already exists
7a1aca2aaa22: Already exists
b06b0c3fba74: Already exists
Digest: sha256:4e306267f5e9b37efc48ae7cefcbd0befc366943b125596f2d46c8511160f02b
Status: Image is up to date for zabbix/zabbix-db-mariadb:latest
latest: Pulling from library/nginx
Digest: sha256:0fe6413f3e30fcc5920bc8fa769280975b10b1c26721de956e1428b9e2f29d04
Status: Image is up to date for nginx:latest
latest: Pulling from google/cadvisor
09d0220f4043: Already exists
151807d34af9: Already exists
14cd28dce332: Already exists
Digest: sha256:8364c7ab7f56a087b757a304f9376c3527c8c60c848f82b66dd728980222bd2f
Status: Image is up to date for google/cadvisor:latest
latest: Pulling from seqvence/static-site
fdd5d7827f33: Already exists
a3ed95caeb02: Already exists
716f7a5f3082: Already exists
7b10f03a0309: Already exists
aff3ab7e9c39: Already exists
Digest: sha256:41b286105f913fb7a5fbdce28d48bc80f1c77e3c4ce1b8280f28129ae0e94e9e
Status: Image is up to date for seqvence/static-site:latest
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
percona                    latest              2cbc44dc3722        4 days ago          338.5 MB
mariadb                    10.1                3b5159f610ed        4 days ago          391.9 MB
prom/prometheus            latest              62b473b89d8d        4 days ago          43.25 MB
django                     latest              7c1a3ee4db99        7 days ago          454.4 MB
django                     python3-onbuild     b31dd7ed2f29        7 days ago          752.4 MB
python                     3.5                 9152ad50a7f9        7 days ago          694.3 MB
centos                     7                   50dae1ee8677        7 days ago          196.7 MB
python                     2.7                 b5c7fb15c9cb        7 days ago          691.6 MB
php                        5.6-fpm             441bf246c4d5        12 days ago         362.6 MB
php                        7-fpm               75b880f3a420        13 days ago         375 MB
mongo                      latest              7f09d45df511        13 days ago         336.1 MB
node                       latest              e79fe5711c94        2 weeks ago         660.7 MB
centos                     6.8                 a374fd62f97f        3 weeks ago         194.5 MB
hello-world                latest              c54a2cc56cbb        3 weeks ago         1.848 kB
alpine                     latest              4e38e38c8ce0        4 weeks ago         4.799 MB
hexo                       latest              10b3639c75ea        5 weeks ago         732.2 MB
zabbix/zabbix-3.0          latest              6cd09bc47ea2        5 weeks ago         719.7 MB
mysql                      latest              1195b21c3a45        6 weeks ago         380.2 MB
debian                     jessie              1b088884749b        6 weeks ago         125.1 MB
zabbix/zabbix-db-mariadb   latest              aecef6724205        7 weeks ago         554.6 MB
nginx                      latest              0d409d33b27e        7 weeks ago         182.8 MB
google/cadvisor            latest              4bc3588563b1        10 weeks ago        48.23 MB
seqvence/static-site       latest              f589ccde7957        4 months ago        190.5 MB
lemp@LempStacker:~$

Change Log

  • 2016.07.27 09:19 Wed Asia/Shanghai
    • 初稿完成
  • 2016.08.01 16:18 Mon Asia/Shanghai
    • 代碼更改
  • 2016.09.19 09:14 Mon Asia/Shanghai
    • 代碼優化,修復bug
  • 2016.12.21 15:51 Wed Asia/Shanghai
    • 代碼優化,添加網路狀態檢測

  • Note Time: 2016.07.27 09:19 Wed
  • Note Location: Asia/Shanghai
  • Writer: lempstacker