LoginSignup
2
2

More than 1 year has passed since last update.

Dockerで任意のイメージやコンテナをまとめて消す

Last updated at Posted at 2021-11-09

任意のイメージやコンテナをまとめて消したい

Dockerfileを構築したりしてるとコンテナやイメージがどんどん増えていく.しかし,pruneで全て消す訳にもいかない...

-f(--filter), --formatを使う

docker ps, docker imagesではフィルタリングとフォーマッティングができる.

フィルタリング-f

任意のキーでフィルタリングできる.-fは複数回使える.

$ docker ps -a
CONTAINER ID   IMAGE                      COMMAND                  CREATED             
1395759a7d8d   foo                        "bash"                   8 seconds ago       
c5d49f37df0e   foo                        "bash"                   16 minutes ago    
0d772e3c9df1   bar                        "docker-entrypoint.s…"   About an hour ago   
bb43a3ad3354   bar                        "docker-entrypoint.s…"   About an hour ago        
d4116724f683   baz                        "forever --killTree …"   About an hour ago      

$ docker ps -a -f ancestor=foo
CONTAINER ID   IMAGE                      COMMAND                  CREATED             
1395759a7d8d   foo                        "bash"                   8 seconds ago       
c5d49f37df0e   foo                        "bash"                   16 minutes ago

コンテナ

キー 説明
id (コンテナの ID)
label ( label= か label== )
name (コンテナの名前)
exited (整数値 - コンテナの終了コード。実用的なのは --all)
status (created
ancestor ( <イメージ名>[:<タグ>] 、 <イメージID> 、 <イメージ@digest> ) - 特定のイメージから作られた子コンテナを作成します。
before (コンテナ ID か名前) - 指定した ID か名前よりも前に作成したコンテナでフィルタ
since (コンテナ ID か名前) - 指定した ID か名前よりも後に作成したコンテナでフィルタ
isolation (default
volume (ボリューム名かマウントポイント) - コンテナがマウントしているボリュームでフィルタ
network (ネットワーク ID か名前)- コンテナが接続しているネットワークでフィルタ

イメージ

キー 説明
dangling (ダングリング;宙ぶらりんな状態)なイメージ (ブール値: true か false )
label ( label= か label== )
before ( <イメージ名>[:<タグ>], <イメージ ID> または image@digest - 指定した ID もしくはリファレンスよりも前に作成したイメージでフィルタ
since ( <イメージ名>[:<タグ>], <イメージ ID> または image@digest - 指定した ID もしくはリファレンスよりも後に作成したイメージでフィルタ
  • danglingはイメージのタグ付がされていない<none>をフィルタリングする.
  • リポジトリ,タグでフィルタリングするにはそのままオプションとして入力する.
$ docker images
REPOSITORY                 TAG       IMAGE ID       CREATED             SIZE
foo                        latest    e4bab8119042   About an hour ago   5.09GB
<none>                     <none>    e4b2cac62ee8   About an hour ago   5.09GB
<none>                     <none>    cd34e378bf28   About an hour ago   5.09GB

$ docker images -f dnagling=true
REPOSITORY                 TAG       IMAGE ID       CREATED             SIZE
<none>                     <none>    e4b2cac62ee8   About an hour ago   5.09GB
<none>                     <none>    cd34e378bf28   About an hour ago   5.09GB

$ docker images foo
REPOSITORY                 TAG       IMAGE ID       CREATED             SIZE
foo                        latest    e4bab8119042   About an hour ago   5.09GB

フォーマット--format

任意のプレースホルダで出力をフォーマッティングできる.

$ docker ps -a
CONTAINER ID   IMAGE                      COMMAND                  CREATED             
1395759a7d8d   foo                        "bash"                   8 seconds ago       
c5d49f37df0e   foo                        "bash"                   16 minutes ago    
0d772e3c9df1   bar                        "docker-entrypoint.s…"   About an hour ago   
bb43a3ad3354   bar                        "docker-entrypoint.s…"   About an hour ago        
d4116724f683   baz                        "forever --killTree …"   About an hour ago      

$ docker ps -a --format {{.ID}}
1395759a7d8d
c5d49f37df0e
0d772e3c9df1
bb43a3ad3354
d4116724f683

コンテナ

プレースホルダ 説明
.ID コンテナ ID
.Image イメージ ID
.Command クォートされたコマンド
.CreatedAt コンテナが作成された時間
.RunningFor コンテナが起動してからの時間
.Ports 公開しているポート
.Status コンテナのステータス
.Size コンテナのディスク容量
.Names コンテナ名
.Labels コンテナに割り当てられている全てのラベル
.Label コンテナに割り当てられた特定のラベル。例: {{.Label "com.docker.swarm.cpu"}}

イメージ

プレースホルダ 説明
.ID イメージ ID
.Repository リポジトリ
.Tag イメージのタグ
.Digest イメージのダイジェスト版
.CreatedSince イメージを作成してからの経過時間
.CreatedAt イメージの作成時間
.Size イメージ・ディスクの容量

Examples

同一イメージのコンテナをまとめて消す

$ docker ps -a
CONTAINER ID   IMAGE                      COMMAND                  CREATED             
1395759a7d8d   foo                        "bash"                   8 seconds ago       
c5d49f37df0e   foo                        "bash"                   16 minutes ago    
0d772e3c9df1   bar                        "docker-entrypoint.s…"   About an hour ago   
bb43a3ad3354   bar                        "docker-entrypoint.s…"   About an hour ago        
d4116724f683   baz                        "forever --killTree …"   About an hour ago 

$ docker ps -a -f ancestor=foo --format {{.ID}}
1395759a7d8d
c5d49f37df0e

$ docker rm $(!!)
1395759a7d8d
c5d49f37df0e

$ docker ps -a
CONTAINER ID   IMAGE                      COMMAND                  CREATED                
0d772e3c9df1   bar                        "docker-entrypoint.s…"   About an hour ago   
bb43a3ad3354   bar                        "docker-entrypoint.s…"   About an hour ago        
d4116724f683   baz                        "forever --killTree …"   About an hour ago 
  • !!は直前のコマンド

特定のコンテナ以外のコンテナをまとめて消す

$ docker ps -a
CONTAINER ID   IMAGE                      COMMAND                  CREATED             
1395759a7d8d   foo                        "bash"                   8 seconds ago       
c5d49f37df0e   foo                        "bash"                   16 minutes ago    
0d772e3c9df1   bar                        "docker-entrypoint.s…"   About an hour ago   
bb43a3ad3354   bar                        "docker-entrypoint.s…"   About an hour ago        
d4116724f683   baz                        "forever --killTree …"   About an hour ago 

$ docker ps -a --format {{.ID}} | grep -v -e bb4 -e b41
1395759a7d8d
c5d49f37df0e
0d772e3c9df1

$ docker rm $(!!)
1395759a7d8d
c5d49f37df0e
0d772e3c9df1

$ docker ps -a
CONTAINER ID   IMAGE                      COMMAND                  CREATED             
bb43a3ad3354   bar                        "docker-entrypoint.s…"   About an hour ago        
d4116724f683   baz                        "forever --killTree …"   About an hour ago 

タグ付けされていないイメージを全て消す

$ docker images
REPOSITORY                 TAG       IMAGE ID       CREATED             SIZE
foo                        latest    e4bab8119042   About an hour ago   5.09GB
<none>                     <none>    e4b2cac62ee8   About an hour ago   5.09GB
<none>                     <none>    cd34e378bf28   About an hour ago   5.09GB

$ docker images -f dnagling=true --format {{.ID}}
e4b2cac62ee8
cd34e378bf28

$ docker rmi -f $(!!)
Deleted: sha256:e4b2cac62ee8...
Deleted: sha256:cd34e378bf28...

$ docker images
REPOSITORY                 TAG       IMAGE ID       CREATED             SIZE
foo                        latest    e4bab8119042   About an hour ago   5.09GB
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2