Remove docker images referenced in multiple repos

Page content

docker image를 GHCR(GitHub Container Registry)에 업로드한 다음 같은 image ID를 갖는 여러 항목이 나타났다. 그동안 local machine에 있던 docker image를 GHCR에 업로드 했으니 더 이상 local host에 이미지가 없어도 되지 않을까 하는 생각에 원래 있던 image를 삭제하려고 하는데 이 경우 에러가 발생한다.

cychong@mini1:~/work/slackbot$ docker images |grep slackbot
my-slackbot                          latest              16cdaacd672e        5 days ago          133MB
ghcr.io/cychong47/my-slackbot        0.1                 16cdaacd672e        5 days ago          133MB
ghcr.io/cychong47/slackbot           0.1                 16cdaacd672e        5 days ago          133MB
cychong@mini1:~/work/slackbot$ docker images |grep pocket
pocket-retagger                      latest              942ef4cc7a60        2 days ago          285MB
ghcr.io/cychong47/pocket-retagger    latest              942ef4cc7a60        2 days ago          285MB
cychong@mini1:~/work/slackbot$ docker images |grep ghost
ghost                                3.8.0               a6ab8e0a010a        6 months ago        394MB
ghcr.io/cychong47/ghost              3.8.0               a6ab8e0a010a        6 months ago        394MB

그래서인지 docker image로 삭제하려고 하면 에러가 난다.

cychong@mini1:~/work/slackbot$ docker rmi a6ab8e0a010a
Error response from daemon: conflict: unable to delete a6ab8e0a010a (must be forced) - image is referenced in multiple repositories

음… 일단 한번 더 이상 사용하지 않는 docker image를 강제로 삭제해 봤다.

cychong@mini1:~/work/slackbot$ docker images |grep ghost
ghost                                3.8.0               a6ab8e0a010a        6 months ago        394MB
ghcr.io/cychong47/ghost              3.8.0               a6ab8e0a010a        6 months ago        394MB
cychong@mini1:~/work/slackbot$ docker rmi -f a6ab8e0a010a
Untagged: ghost:3.8.0
Untagged: ghost@sha256:821b1f33bc9868b39eea7ecd42a4b1320c7d5a8a5d3ed597c6d1c88fd55b0e52
Untagged: ghcr.io/cychong47/ghost:3.8.0
Untagged: ghcr.io/cychong47/ghost@sha256:d7305e5e7f9b24b69b2b883cc1b84988746c6ae8e88e862f2bfdc54d023ecfdc
Deleted: sha256:a6ab8e0a010ae92f22619928aa62ffeb0370768bebe9e5a83b2676774a6af37e
Deleted: sha256:7206e644229eafbb451a72727b84e36f65f8d5b98a8527837efb6c1057ecaedb
Deleted: sha256:6c0b480a0f3ae04bc1ffd05175883781a6eff80b2ed6cd47234165cd8247722e
Deleted: sha256:45b39ee767f6e9c2622980f3936fdb7d36f53d384a36a705f76e108cc8b049d2
Deleted: sha256:83b6ac7cf54af9c0ab9c4fc6e778ff58ad48867939f983aba7047d470bdacc68
Deleted: sha256:3dec8d1f9cbdc89ad4ea29e2f38d57b72e42baa4fe52595676582ee792989b99
Deleted: sha256:374cf1e1966728e5306fbeb61c7080535f3c0a078f7dcf65b0aabde1355d2c96
Deleted: sha256:b603cee30096ded2772fbb34552aa78a2e48e750a13a56eedc50c33b45b8353b
Deleted: sha256:bf996bc2e6349602474e920ef3343776d26a9575d944967832f8e34c63a236e2
Deleted: sha256:f2cb0ecef392f2a630fa1205b874ab2e2aedf96de04d0b8838e4e728e28142da
cychong@mini1:~/work/slackbot$ docker images |grep ghost

뭔가 찜찜(?)하게 Untagged 어쩌구 하는 메시지가 나온다.

다행히 GitHub Container Registry(ghcr.io)에 등록된 docker image는 영향이 없네. 흠… 문제(?)는 지금 사용하고 있는 image는 어떻게 하지? 일단 현재 실행되고 있는 container를 우선 내린 후에 이미지를 삭제해야 하나?

해결책은 의외로 간단하네.

docker rmi | Docker Documentation

> $ docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
test1                     latest              fd484f19954f        23 seconds ago      7 B (virtual 4.964 MB)
test                      latest              fd484f19954f        23 seconds ago      7 B (virtual 4.964 MB)
test2                     latest              fd484f19954f        23 seconds ago      7 B (virtual 4.964 MB)

$ docker rmi fd484f19954f

Error: Conflict, cannot delete image fd484f19954f because it is tagged in multiple repositories, use -f to force
2013/12/11 05:47:16 Error: failed to remove one or more images

$ docker rmi test1:latest

Untagged: test1:latest

$ docker rmi test2:latest

Untagged: test2:latest

$ docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
test                      latest              fd484f19954f        23 seconds ago      7 B (virtual 4.964 MB)

$ docker rmi test:latest
Untagged: test:latest
Deleted: fd484f19954f4920da7ff372b5067f5b7ddb2fd3830cecd17b96ea9e286ba5b8

같은 방법으로 REPOSITORY:TAG의 형태로 image를 삭제하면

cychong@mini1:~/work/slackbot$ docker rmi pocket-retagger:latest
Untagged: pocket-retagger:latest
cychong@mini1:~/work/slackbot$ docker images |grep pocket
ghcr.io/cychong47/pocket-retagger    latest              942ef4cc7a60        2 days ago          285MB

RTFM

#troubleshooting #TIL/docker