docker image prune

docker image prune

사용하지 않는 이미지를 제거하는 명령이에요. 댕글링(dangling) 이미지를 정리하거나, -a로 어떤 컨테이너에도 참조되지 않은 모든 이미지를 지울 수 있어요.

출처: 문서

본문

docker image prune은 모든 댕글링 이미지를 제거하는 명령이에요. -a를 지정하면 어떤 컨테이너에도 참조되지 않은 모든 이미지도 제거해요.

docker image prune [OPTIONS]

지원하는 옵션은 다음과 같아요.

옵션 기본값 설명
-a, --all 댕글링 이미지만이 아니라 사용하지 않는 모든 이미지 제거
--filter 필터 값 제공 (예: until=<timestamp>)
-f, --force 확인 프롬프트를 표시하지 않아요

예시 출력:

$ docker image prune -a

WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: alpine:latest
untagged: alpine@sha256:3dcdb92d7432d56604d4545cbd324b14e647b313626d99b889d0626de158f73a
deleted: sha256:4e38e38c8ce0b8d9041a9c4fefe786631d1416225e13b0bfe8cfa2321aec4bba
deleted: sha256:4fe15f8d0ae69e169824f25f1d4da3015a48feeeeebb265cd2e328e15c6a869f
untagged: alpine:3.3
untagged: alpine@sha256:4fa633f4feff6a8f02acfc7424efd5cb3e76686ed3218abf4ca0fa4a2a358423
untagged: my-jq:latest
deleted: sha256:ae67841be6d008a374eff7c2a974cde3934ffe9536a7dc7ce589585eddd83aff
deleted: sha256:34f6f1261650bc341eb122313372adc4512b4fceddc2a7ecbb84f0958ce5ad65
deleted: sha256:cf4194e8d8db1cb2d117df33f2c75c0369c3a26d96725efb978cc69e046b87e7
untagged: my-curl:latest
deleted: sha256:b2789dd875bf427de7f9f6ae001940073b3201409b14aba7e5db71f408b8569e
deleted: sha256:96daac0cb203226438989926fc34dd024f365a9a8616b93e168d303cfe4cb5e9
deleted: sha256:5cbd97a14241c9cd83250d6b6fc0649833c4a3e84099b968dd4ba403e609945e
deleted: sha256:a0971c4015c1e898c60bf95781c6730a05b5d8a2ae6827f53837e6c9d38efdec
deleted: sha256:d8359ca3b681cc5396a4e790088441673ed3ce90ebc04de388bfcd31a0716b06
deleted: sha256:83fc9ba8fb70e1da31dfcc3c88d093831dbd4be38b34af9
...

-f 플래그를 쓰고 이미지의 짧거나 긴 ID를 지정하면, 이 명령은 지정한 ID와 일치하는 모든 이미지를 언태그하고 제거해요.

$ 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 -f fd484f19954f

Untagged: test1:latest
Untagged: test:latest
Untagged: test2:latest
Deleted: fd484f19954f4920da7ff372b5067f5b7ddb2fd3830cecd17b96ea9e286ba5b8

다이제스트로 가져온 이미지에는 태그가 없어요.

$ docker images --digests

REPOSITORY                     TAG       DIGEST                                                                    IMAGE ID        CREATED         SIZE
localhost:5000/test/busybox    <none>    sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf   4986bf8c1536    9 weeks ago     2.43 MB

다이제스트로 이미지를 제거하려면:

$ docker rmi localhost:5000/test/busybox@sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf
Untagged: localhost:5000/test/busybox@sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf
Deleted: 4986bf8c15363d1c5d15512d5266f8777bfba4974ac56e3270e7760f6f0a8125
Deleted: ea13149945cb6b1e746bf28032f02e9b5a793523481a0a18645fc77ad53c4ea2
Deleted: df7546f9f060a2268024c8a230d8639878585defcc1bc6f79d2728a13957871b

특정 플랫폼 제거 (--platform)

--platform 옵션은 이미지의 어떤 플랫폼 변형을 제거할지 지정할 수 있어요. 기본적으로 docker image remove는 존재하는 모든 플랫폼 변형을 제거해요. --platform 옵션으로 이미지의 어떤 플랫폼 변형을 제거할지 정할 수 있어요.

특정 플랫폼을 제거하면 같은 내용을 참조하는 모든 이미지에서 그 플랫폼이 제거되고, --force 옵션을 사용해야 해요. --force를 빼면 경고가 발생하고 제거가 취소돼요.

$ docker image rm --platform=linux/amd64 alpine
Error response from daemon: Content will be removed from all images referencing this variant. Use —-force to force delete.

플랫폼 옵션은 os[/arch[/variant]] 형식을 취해요. 예를 들어 linux/amd64 또는 linux/arm64/v8이에요. 아키텍처와 변형은 선택 사항이고, 생략하면 데몬 네이티브 아키텍처로 기본값을 정해요.

여러 플랫폼을 전달하려면 --platform 플래그를 여러 번 넘기거나, 제거할 플랫폼을 쉼표로 구분한 목록을 넘기면 돼요. 다음 두 사용법은 동일해요.

$ docker image rm --platform linux/amd64 --platform linux/ppc64le myimage
$ docker image rm --platform linux/amd64,linux/ppc64le myimage

다음 예제는 이미지 캐시에 여러 플랫폼 변형이 있는 alpine 이미지의 linux/amd64linux/ppc64le 변형을 제거해요.

$ docker image ls --tree

IMAGE                   ID             DISK USAGE   CONTENT SIZE   EXTRA
alpine:latest           a8560b36e8b8       37.8MB         11.2MB    U
├─ linux/amd64          1c4eef651f65       12.1MB         3.64MB    U
├─ linux/arm/v6         903bfe2ae994           0B             0B
├─ linux/arm/v7         9c2d245b3c01           0B             0B
├─ linux/arm64/v8       757d680068d7       12.8MB         3.99MB
├─ linux/386            2436f2b3b7d2           0B             0B
├─ linux/ppc64le        9ed53fd3b831       12.8MB         3.58MB
├─ linux/riscv64        1de5eb4a9a67           0B             0B
└─ linux/s390x          fe0dcdd1f783           0B             0B

$ docker image --platform=linux/amd64,linux/ppc64le --force alpine
Deleted: sha256:1c4eef651f65e2f7daee7ee785882ac164b02b78fb74503052a26dc061c90474
Deleted: sha256:9ed53fd3b83120f78b33685d930ce9bf5aa481f6e2d165c42cbbddbeaa196f6f

명령이 끝나면 alpine 이미지의 주어진 변형들은 이미지 캐시에서 제거돼요.

$ docker image ls --tree

IMAGE                   ID             DISK USAGE   CONTENT SIZE   EXTRA
alpine:latest           a8560b36e8b8       12.8MB         3.99MB
├─ linux/amd64          1c4eef651f65           0B             0B
├─ linux/arm/v6         903bfe2ae994           0B             0B
├─ linux/arm/v7         9c2d245b3c01           0B             0B
├─ linux/arm64/v8       757d680068d7       12.8MB         3.99MB
├─ linux/386            2436f2b3b7d2           0B             0B
├─ linux/ppc64le        9ed53fd3b831           0B             0B
├─ linux/riscv64        1de5eb4a9a67           0B             0B
└─ linux/s390x          fe0dcdd1f783           0B             0B

더 알아보기