docker container rmでDockerコンテナを削除する方法
Dockerコンテナを削除するにはコンテナIDかコンテナ名を指定してdocker container rmコマンドを実行します。
コマンドの説明
$ docker container rm --help Usage: docker container rm [OPTIONS] CONTAINER [CONTAINER...] Remove one or more containers Options: -f, --force Force the removal of a running container (uses SIGKILL) -l, --link Remove the specified link -v, --volumes Remove anonymous volumes associated with the container
実行例
$ docker image ls # Dockerイメージ一覧 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 container run -it --name test01 centos:7.7.1908 /bin/bash [root@e5cef61699ff /]# cat /etc/redhat-release CentOS Linux release 7.7.1908 (Core) [root@e5cef61699ff /]# exit exit $ docker container ls -a # Dockerコンテナ一覧 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e5cef61699ff centos:7.7.1908 "/bin/bash" About a minute ago Exited (0) 2 seconds ago test01 $ docker rm test01 # Dockerコンテナ削除 test01 $ docker container ls -a # Dockerコンテナ一覧 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES $ docker image ls # Dockerイメージ一覧はそのまま 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
稼働中のコンテナを削除するには--forceオプションを指定するか、コンテナを停止してから削除します。
$ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d05a20e07640 centos:7.7.1908 "/bin/bash" 9 seconds ago Up 7 seconds test01 $ docker container rm test01 Error response from daemon: You cannot remove a running container d05a20e07640d3d120a3f8c5ea6a5d78f1bdce0f6589605d071d974d686beaac. Stop the container before attempting removal or force remove $ docker container rm --force test01 # 強制削除する場合 test01 $ docker container stop test01 # コンテナを停止してから削除する場合 test01 $ docker container rm test01 test01