Dockerのプライベートレジストリからimageを削除する方法

プライベートリポジトリ（プライベートレジストリ）からイメージを削除する方法を共有します。

コマンドがなくAPIのみの対応なので面倒です。

## 手順

### １：docker registryの準備

`環境変数REGISTRY_STORAGE_DELETE_ENABLED=true`にしてdocker registryを起動する

* 参考：https://github.com/docker/distribution/issues/1573

```
[root@ip-10-0-0-187 ec2-user]# docker run -e REGISTRY_STORAGE_DELETE_ENABLED=true -d -p 5000:5000 --res
try registry:2

8fa11495803de7c8ea6315da2adf407db4c7927d6de9ced098c540f9c176d6ac
```

すでにどこかで建てている方は、環境変数を有効化して再起動してください。

### ２：コンテナをregistryにpushする

あくまでおいている作業なのでスキップしてください。

```
[root@ip-10-0-0-187 ec2-user]# docker push localhost:5000/os:latest
The push refers to repository [localhost:5000/os]
03901b4a2ea8: Pushed 
latest: digest: sha256:acd3ca9941a85e8ed16515bfc5328e4e2f8c128caa72959a58a127b7801ee01f size: 528
[root@ip-10-0-0-187 ec2-user]# 
[root@ip-10-0-0-187 ec2-user]# 
```

確認する

```
[root@ip-10-0-0-187 ec2-user]# curl -X GET localhost:5000/v2/os/manifests/latest
{
   "schemaVersion": 1,
   "name": "os",
   "tag": "latest",
   "architecture": "amd64",
   "fsLayers": [
      {
         "blobSum": "sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4"
      },
      {
         "blobSum": "sha256:9d48c3bd43c520dc2784e868a780e976b207cbf493eaff8c6596eb871cbd9609"
      }
   ],
   "history": [
      {
         "v1Compatibility": "{\"architecture\":\"amd64\",\"config\":{\"Hostname\":\"\",\"Domainname\":\chStdin\":false,\"AttachStdout\":false,\"AttachStderr\":false,\"Tty\":false,\"OpenStdin\":false,\"StdinPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\"],\"Cmd\":[\"/bin/sh\"],\"ArgsEscape256:06f4121dff4d0123ce11bd2e44f48da9ba9ddcd23ae376ea1f363f63ea0849b5\",\"Volumes\":null,\"WorkingDir\":,\"OnBuild\":null,\"Labels\":null},\"container\":\"0a80155a31551fcc1a36fccbbda79fcd3f0b1c7d270653d00310er_config\":{\"Hostname\":\"0a80155a3155\",\"Domainname\":\"\",\"User\":\"\",\"AttachStdin\":false,\"AtachStderr\":false,\"Tty\":false,\"OpenStdin\":false,\"StdinOnce\":false,\"Env\":[\"PATH=/usr/local/sbinn:/usr/bin:/sbin:/bin\"],\"Cmd\":[\"/bin/sh\",\"-c\",\"#(nop) \",\"CMD [\\\"/bin/sh\\\"]\"],\"ArgsEscapa256:06f4121dff4d0123ce11bd2e44f48da9ba9ddcd23ae376ea1f363f63ea0849b5\",\"Volumes\":null,\"WorkingDir\"l,\"OnBuild\":null,\"Labels\":{}},\"created\":\"2019-08-20T20:19:55.211423266Z\",\"docker_version\":\"150dd72f8902b1b7d3128ad4b9f79e6b185216bcddd0fa930f13aff6b68764\",\"os\":\"linux\",\"parent\":\"0e440b6eff734b53c70d903ee0f129d67d450365\",\"throwaway\":true}"
      },
      {
         "v1Compatibility": "{\"id\":\"0e440b6ef9db5adec4ee95f637eed3440f734b53c70d903ee0f129d67d450365-20T20:19:55.062606894Z\",\"container_config\":{\"Cmd\":[\"/bin/sh -c #(nop) ADD file:fe64057fbb83dccb97fa8dbca0a8801c651bdf7c in / \"]}}"
      }
   ],
   "signatures": [
      {
         "header": {
            "jwk": {
               "crv": "P-256",
               "kid": "BZZG:ACYI:CQZW:TJEG:TILZ:KLV2:ONVK:HSLZ:5HM7:GK2X:PFUV:ZE2N",
               "kty": "EC",
               "x": "Dxj9qI1YlOPTxM9nrVXA9yjt38ZlHQ-AY_Gmb-sKRRo",
               "y": "9eeU57WRdH63-Qfw9O6Oqc8eadG1JWO-TfHnfTGH4j0"
            },
            "alg": "ES256"
         },
         "signature": "Kjl6JOKqxtWb5_lJP6-8B9P-6hEUwmI6R-KB_yD9qhzgWtQQXufMkM6opj0MdPQb9QLmsksff6XKinEe
         "protected": "eyJmb3JtYXRMZW5ndGgiOjIxMjksImZvcm1hdFRhaWwiOiJDbjAiLCJ0aW1lIjoiMjAxOS0wOS0xMlQw
      }
   ]
}
```

## ３：削除したいimageのdigestを取得する

参考：https://github.com/docker/distribution/issues/1579

```
[root@ip-10-0-0-187 ec2-user]# curl -i -H "Accept: application/vnd.docker.distribution.manifest.v2+json" localhost:5000/v2/os/m
anifests/latest

HTTP/1.1 200 OK
Content-Length: 528
Content-Type: application/vnd.docker.distribution.manifest.v2+json
Docker-Content-Digest: sha256:acd3ca9941a85e8ed16515bfc5328e4e2f8c128caa72959a58a127b7801ee01f
Docker-Distribution-Api-Version: registry/2.0
Etag: "sha256:acd3ca9941a85e8ed16515bfc5328e4e2f8c128caa72959a58a127b7801ee01f"
X-Content-Type-Options: nosniff
Date: Thu, 12 Sep 2019 08:46:07 GMT

{
   "schemaVersion": 2,
   "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
   "config": {
      "mediaType": "application/vnd.docker.container.image.v1+json",
      "size": 1512,
      "digest": "sha256:961769676411f082461f9ef46626dd7a2d1e2b2a38e6a44364bcbecf51e66dd4"
   },
   "layers": [
      {
         "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
         "size": 2789669,
         "digest": "sha256:9d48c3bd43c520dc2784e868a780e976b207cbf493eaff8c6596eb871cbd9609"
      }
   ]
```

これが大事です↓
`Docker-Content-Digest: sha256:acd3ca9941a85e8ed16515bfc5328e4e2f8c128caa72959a58a127b7801ee01f`

### ４：Docker-Content-Digestヘッダで取得した情報を付与してDELETEする

```
[root@ip-10-0-0-187 ec2-user]# curl -X DELETE localhost:5000/v2/os/manifests/sha256:acd3ca9941a85e8ed16
515bfc5328e4e2f8c128caa72959a58a127b7801ee01f
```

確認

```
[root@ip-10-0-0-187 ec2-user]# curl -X GET localhost:5000/v2/os/manifests/latest
{"errors":[{"code":"MANIFEST_UNKNOWN","message":"manifest unknown","detail":{"Tag":"latest"}}]}
```