[docker]正確移除docker register的方式

[docker]正確移除docker register的方式

前言

安裝docker register非常的方便,但是要移除自行上傳的鏡像卻非常麻煩,所以筆者要來記錄一下到底如何正確的移除images的方式

linux的操作

首先我們必須先進到container容器裡面,因為我的container自行命名為registry,所以輸入如下指令進入container裡

docker exec -it registry sh

進到container裡面之後,我們可以輸入如下指令編輯文件,設定可刪除images,否則預設是不可刪除的,這邊你可以選擇使用volume對應出來,就不用進container裡面操作,視自身決策而定

vi /etc/docker/registry/config.yml

接著就是在storage:底下加入delete為true,我的yml完整內容如下

version: 0.1
log:
  fields:
    service: registry
storage:
  delete:
    enabled: true
  cache:
    blobdescriptor: inmemory
  filesystem:
    rootdirectory: /var/lib/registry
http:
  addr: :5000
  headers:
    X-Content-Type-Options: [nosniff]
health:
  storagedriver:
    enabled: true
    interval: 10s
    threshold: 3

修改完成之後,離開container並重啟此container

docker restart registry

接著重新進入container裡面,並輸入如下指令,查看裡面所有的images和sha的編碼

registry garbage-collect /etc/docker/registry/config.yml

此行指令執行後,會出現如下的資訊


沒錯,如果我們想要刪除一個項目的話,我們就必須把上述所有的sha256的資訊刪除乾淨,才算是真正的釋放空間,這邊可以透過postman的方式一筆一筆的刪除,刪除的url為下

10.20.101.20:5000/v2/testing-lottery-mgt-web/manifests/sha256:c66859f802d10e16e72a488f9645284e7722c49bea9d0043543946411619d6d7
(registry url)       (images name)                     (container裡面看到的所有sha256碼,都得刪乾淨)

當全部的sha256刪除乾淨之後,會只看到一個名稱,如圖示


此時我們確確實實的把空間釋放了,但你如果有安裝web registry(https://dotblogs.com.tw/kinanson/2019/10/02/101052 可參考最末段)的話,會發現名稱沒有被刪除,此時如果你沒有設定volume的話,你可以進container裡面,然後輸入如下指令強制刪除

rm -Rf /var/lib/registry/docker/registry/v2/repositories/rd-lottery-site-web

這樣子就算把此image清除得乾乾淨淨了,不過我們在刪除的時候也太辛苦了吧,每一個image有那麼多行sha256要刪,所以筆者自行就寫了一段簡單的程式碼,然後先把這些sha256編成一個字串,再用迴圈一次刪除,簡單的程式碼如下

	var sha = @"sha256:00b2ed483b24b9109fdc14e66b79674a476b703f1fa74e839ac386911affe104,sha256:399afe53bb12dd76d414526b84246c5fe15a84844977a2187c6a5a606601c322,sha256:89d9c30c1d48bac627e5c6cb0d1ed1eec28e7dbdfbcc04712e4c79c0f83faf17,sha256:fa1bcf7bbd3d7daa37cec96869ff19def74e5228dc908892df0755c9a02213bb,sha256:4f3cc74431c3a35ccdd23d5bb13d2f58ecb5698e534c8e5e24a8d0708f6c2e1b,sha256:1f4ab2dc430cb16f9b713a0ac78466c4b4b7782b5141abe67fa2142048806b44,sha256:7fe2ac66ebd7fb21d91cf5556f255b64c23588a38134b49ab277e805f082ee84,sha256:219e14c0cadbb833596f403df43394179f540793267c1cebf950c466ec9fe0c1,sha256:dc38aa313f93b65afdebde50531c2e78562a3bb7cecec90b6c0b16e4a825010d,sha256:89d9c30c1d48bac627e5c6cb0d1ed1eec28e7dbdfbcc04712e4c79c0f83faf17,sha256:fa1bcf7bbd3d7daa37cec96869ff19def74e5228dc908892df0755c9a02213bb,sha256:10679f88b6a087c440f3220d2b0dc87f5e6a983ace21612754cec889c89112ac,sha256:c8a862de4c84b5316358f282cfad6d248ae5019094c1e2f77316179420b7fea2,sha256:327719bc582c4e2547a078bc7cfc174765eaafad1882ae5f36587e0061abd514,sha256:5155ce35789e83bd6650ff2c8dfa98e61fab7e4d836ef742d004084a188a5dbb,sha256:f7ae7a4f9e89f01fb996eff07330cb99867181541a6410684eedd3216f08bd0e,sha256:89d9c30c1d48bac627e5c6cb0d1ed1eec28e7dbdfbcc04712e4c79c0f83faf17,sha256:fa1bcf7bbd3d7daa37cec96869ff19def74e5228dc908892df0755c9a02213bb,sha256:0b7245ae4c412af9a2a8beeab3db5c833fe214dd9130c4319100a76e7500b951,sha256:777c5e0f3bfc4db849a42889692c43a79e4e98697ab177651e387e673bfff775,sha256:8703be797dac46ce5ce5856cb09fb2b9164f452296abb5676bd9a38b1023f448,sha256:642859cad5922e8f37fa20362e08ed80394583d00bc5aa3f4ee3a86ea5e3e64e,sha256:c34b2cc4915e0bf4262eb64a54d8a773b7c5d2a21fceca72d398722f33f55ec1,sha256:89d9c30c1d48bac627e5c6cb0d1ed1eec28e7dbdfbcc04712e4c79c0f83faf17,sha256:fa1bcf7bbd3d7daa37cec96869ff19def74e5228dc908892df0755c9a02213bb,sha256:7219fdfa2e04e2a74bcceeae44e331428d58d0ca46f3ee2a6cc61001bb1b9016,sha256:c69a75d906928d794bc416ec7b9a1b96898f4d11f0f71bdf0d009915565a2587,sha256:52a404bf4c6e2d6dc8e20b083c24a5f33e818c30913fa0064b72b94ac15820c5,sha256:7f62c9633caf819339cf94411a0ed8f6587f5963e364e749520f9af112eb9d5d,sha256:b64c1fc54a3a83919997e557c30e56e50e40c4f54e0a3f7814331581e0f069ac,sha256:89d9c30c1d48bac627e5c6cb0d1ed1eec28e7dbdfbcc04712e4c79c0f83faf17,sha256:fa1bcf7bbd3d7daa37cec96869ff19def74e5228dc908892df0755c9a02213bb,sha256:ab88e8e20fca2f6427f12e7f33c247032796241c0b64b5e884c3e46453ef7687,sha256:121fcf9d82b6238fde7643c2c2b0de7f921ae65666ea26d9918df3361a3b332c,sha256:a04686b100fe14522f5f5918bbc817eb8a83bb9069a2f26b7308cdf935abfcf0,sha256:ddc9dd110221f2aaeb9f91c32e0853e08ae171df483b40654154fb52678e2f33,sha256:f8dbe5be75e5be4bd02714ae53248608f8ba96cbc7358f0877a56d4af9202222,sha256:9d48c3bd43c520dc2784e868a780e976b207cbf493eaff8c6596eb871cbd9609,sha256:7a56a3a1208e6e990931f408567b783890faa0ec6ceb69e4ea6dda6feb6c2808,sha256:cee521018a5fc2698cbbe4ae04e868c30befc69ea1aba941b76c009cfd2a70d1,sha256:4039a482a530b2fbf1169bea4131240262bf48242208aa84c7d8206005fd0095,sha256:cafd3a9678fc5d00a67ed982e44ea0ea43acbfc0039251655beed8501b640930,sha256:e0467cd5d69771b5e52392d7bb1fd6a66d264915cb52558482bfcec21097a355,sha256:f1219254a501a47c86df3695292aabaeea916b143104b1e303967ba709635467,sha256:89d9c30c1d48bac627e5c6cb0d1ed1eec28e7dbdfbcc04712e4c79c0f83faf17,sha256:fa1bcf7bbd3d7daa37cec96869ff19def74e5228dc908892df0755c9a02213bb,sha256:4f9b1eea72f416479be00344e359bf6431a6c39685a12a25b24d33b97ab6b9b8,sha256:18019836e37535b68d3c3331472bbe232851c550903bc44c32a2b096bbd5b84f,sha256:6b968b77a4fd3ec5fc95f9f10d7a5a42058661fe76e6638df20de4af825b18e3,sha256:fd04cb9790086da2776ef9bbb3edcec8587fe0a78288b169bbc282614c71f2cb,sha256:f1b1423bfb707fc19de8689445bc297c2623a6f51d5e6eff94819c5e8978d494,sha256:9d48c3bd43c520dc2784e868a780e976b207cbf493eaff8c6596eb871cbd9609,sha256:7a56a3a1208e6e990931f408567b783890faa0ec6ceb69e4ea6dda6feb6c2808,sha256:664506e9e6d593104870a356df4339557f74f8c7822e23bd0a8a700c2c408118,sha256:9100bcc03e204eb9d90d1c8e1b487d45239d77a3f83ff5425ae05622c7d67b87,sha256:8b2ddb017d6993068547950751157574ce8e90061e988b0eede9a166bcc8769b";
	foreach (var item in sha.Trim().Split(','))
	{
		using (HttpClient client = new HttpClient { BaseAddress = new Uri("http://10.20.101.20:5000")})
		{
			HttpResponseMessage response = client.DeleteAsync($"/v2/rd-lottery-site-web/manifests/{item}").GetAwaiter().GetResult();
			response.Content.ReadAsStringAsync().GetAwaiter().GetResult().Dump();
		}	
	}

至於如何把這整串的內容變成一個字串呢?其實只要善用vs code的話,這就不是件難事了。

結論

這是筆者自己研究後的做法,如覺得有任何不對的地方,再請指教