docker image rmでDockerイメージを削除する方法
Dockerイメージを削除するにはdocker image rmコマンドを実行します。
コマンドの説明
$ docker image rm --help Usage: docker image rm [OPTIONS] IMAGE [IMAGE...] Remove one or more images Aliases: rm, rmi, remove Options: -f, --force Force removal of the image --no-prune Do not delete untagged parents
実行例
$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 1d622ef86b13 3 days ago 73.9MB centos latest 470671670cac 3 months ago 237MB centos 7.7.1908 08d05d1d5859 5 months ago 204MB $ docker image rm -f centos:7.7.1908 Untagged: centos:7.7.1908 Untagged: centos@sha256:50752af5182c6cd5518e3e91d48f7ff0cba93d5d760a67ac140e2d63c4dd9efc Deleted: sha256:08d05d1d5859ebcfb3312d246e2082e46cb307f0e896c9ac097185f0b0b19e56 Deleted: sha256:034f282942cd6c3abf9384601a57f080f8f75cc7f58527db8e07573d9d14ab46 $ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 1d622ef86b13 3 days ago 73.9MB centos latest 470671670cac 3 months ago 237MB
削除したいDockerイメージに関連付けられたコンテナがある場合は、コンテナを削除してからイメージを削除します。
$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 1d622ef86b13 3 days ago 73.9MB centos latest 470671670cac 3 months ago 237MB centos 7.7.1908 08d05d1d5859 5 months ago 204MB $ docker image rm centos:7.7.1908 Error response from daemon: conflict: unable to remove repository reference "centos:7.7.1908" (must force) - container 1f5a5464ec12 is using its referenced image 08d05d1d5859 $ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1f5a5464ec12 centos:7.7.1908 "/bin/bash" 8 minutes ago Up 8 minutes test01 $ docker container rm --force test01 test01 $ docker image rm centos:7.7.1908 Untagged: centos:7.7.1908 Untagged: centos@sha256:50752af5182c6cd5518e3e91d48f7ff0cba93d5d760a67ac140e2d63c4dd9efc Deleted: sha256:08d05d1d5859ebcfb3312d246e2082e46cb307f0e896c9ac097185f0b0b19e56 Deleted: sha256:034f282942cd6c3abf9384601a57f080f8f75cc7f58527db8e07573d9d14ab46 $ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest 1d622ef86b13 3 days ago 73.9MB centos latest 470671670cac 3 months ago 237MB