※当サイトの記事には、広告・プロモーションが含まれます。

docker-compose imagesで確認できたDockerイメージの削除ってどうすべきなのか

gigazine.net

近年はAI開発企業によるウェブサイトのクローリングや、AIトレーニングに著作権で保護されたコンテンツを用いることに対する懸念が高まっており、企業やクリエイターによる訴訟にも発展しています。そんな中、Twitter(現X)の共同創業者である実業家のジャック・ドーシー氏が、「すべての知的財産法を撤廃せよ」とXに投稿して物議を醸しています。

ジャック・ドーシーが「すべての知的財産法を撤廃せよ」と発言してイーロン・マスクが同意 - GIGAZINE

ドーシー氏が2025年4月12日に投稿した「すべての知的財産法を撤廃せよ」という主張は、多くの人々からさまざまな反応を呼びました。Xやテスラのオーナーであるイーロン・マスク氏は、これに「同意します」とリプライしています。

ジャック・ドーシーが「すべての知的財産法を撤廃せよ」と発言してイーロン・マスクが同意 - GIGAZINE

テクノロジー業界に身を置いていたブロック氏は、オープンソースソフトウェアとクリエイティブコモンズのアプローチを支持しており、これらのモデルがイノベーションとコラボレーションを促進するのを目の当たりにしてきたとのこと。しかし、これらのアプローチが効果を発揮するのは、知的財産権の枠組みに反するのではなく枠組みの中で機能する時であり、クリエイターが特定の条件で権利を共有するかどうかを選択できる状態に依存していると主張。

ジャック・ドーシーが「すべての知的財産法を撤廃せよ」と発言してイーロン・マスクが同意 - GIGAZINE

ブロック氏が特に問題としているのが、ドーシー氏が「現在の欠陥がある知的財産法を維持するか、知的財産法を撤廃して不特定の『より良いモデル』を通じて『クリエイターに適正な報酬が支払われる』という曖昧な未来を支持するか」という誤った二元論を提示している点です。そもそも、クリエイターが何らかの形で知的財産権を保有していなければ、個々のクリエイターがプラットフォームに働きかけて報酬を得ることは不可能です。

ジャック・ドーシーが「すべての知的財産法を撤廃せよ」と発言してイーロン・マスクが同意 - GIGAZINE

⇧ 結局のところ、行き着く先は、「お金」の問題よね...

docker-compose imagesで確認できたDockerイメージの削除ってどうすべきなのか

Docker Composeなどで稼働しているアプリケーションをデプロイしている時に、「切り戻し」が必要になって来ること、あるあるだと思うのですが、「Dockerイメージ」の扱いが悩ましい...

とりあえず、「Dockerイメージ」を読み込む方法としては大きく分けて、

  1. docker pull
  2. docker load

の2パターンあると思うのだが、このあたりは、

qiita.com

⇧ 上記サイト様の図解が分かり易いかと。

で、「2. docker load」のパターンは、「Dockerイメージ」を「tarball」といった圧縮ファイルの状態にしておいて読み込む形になるので、事前に、「Dockerfile」などを元に、

  1. docker build
  2. docker image build

などで「Dockerイメージ」を作成後、

  1. docker save
  2. docker image save

などで「tarball」の形にしておく必要があると。

つまり、デプロイの最後で、「Dockerイメージ」の「tarball」を削除しないのであれば、1世代前の「Dockerイメージ」はデプロイ前の「バックアップ」で退避されていることになるので、

  1. docker compose images

などで確認できる「Dockerイメージ」は全て削除で良いはず...

満を持して、公式のドキュメントを見た感じでは、

docs.docker.jp

⇧ とあり、「[サービス...]」の説明が無いのも酷いのだが、実行した結果の出力の見本が無いのが残念過ぎる...

とりあえず、GitHubで公開されているのが「Go」だったので結局のところ、良く分からないのだが、

github.com

■https://github.com/docker/compose/blob/main/cmd/compose/images.go

/*
   Copyright 2020 Docker Compose CLI authors

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package compose

import (
	"context"
	"fmt"
	"io"
	"sort"
	"strings"

	"github.com/docker/cli/cli/command"
	"github.com/docker/docker/pkg/stringid"
	"github.com/docker/go-units"
	"github.com/spf13/cobra"

	"github.com/docker/compose/v2/cmd/formatter"
	"github.com/docker/compose/v2/pkg/api"
	"github.com/docker/compose/v2/pkg/utils"
)

type imageOptions struct {
	*ProjectOptions
	Quiet  bool
	Format string
}

func imagesCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
	opts := imageOptions{
		ProjectOptions: p,
	}
	imgCmd := &cobra.Command{
		Use:   "images [OPTIONS] [SERVICE...]",
		Short: "List images used by the created containers",
		RunE: Adapt(func(ctx context.Context, args []string) error {
			return runImages(ctx, dockerCli, backend, opts, args)
		}),
		ValidArgsFunction: completeServiceNames(dockerCli, p),
	}
	imgCmd.Flags().StringVar(&opts.Format, "format", "table", "Format the output. Values: [table | json]")
	imgCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
	return imgCmd
}

func runImages(ctx context.Context, dockerCli command.Cli, backend api.Service, opts imageOptions, services []string) error {
	projectName, err := opts.toProjectName(ctx, dockerCli)
	if err != nil {
		return err
	}

	images, err := backend.Images(ctx, projectName, api.ImagesOptions{
		Services: services,
	})
	if err != nil {
		return err
	}

	if opts.Quiet {
		ids := []string{}
		for _, img := range images {
			id := img.ID
			if i := strings.IndexRune(img.ID, ':'); i >= 0 {
				id = id[i+1:]
			}
			if !utils.StringContains(ids, id) {
				ids = append(ids, id)
			}
		}
		for _, img := range ids {
			_, _ = fmt.Fprintln(dockerCli.Out(), img)
		}
		return nil
	}

	sort.Slice(images, func(i, j int) bool {
		return images[i].ContainerName < images[j].ContainerName
	})

	return formatter.Print(images, opts.Format, dockerCli.Out(),
		func(w io.Writer) {
			for _, img := range images {
				id := stringid.TruncateID(img.ID)
				size := units.HumanSizeWithPrecision(float64(img.Size), 3)
				repo := img.Repository
				if repo == "" {
					repo = "<none>"
				}
				tag := img.Tag
				if tag == "" {
					tag = "<none>"
				}
				_, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", img.ContainerName, repo, tag, id, size)
			}
		},
		"CONTAINER", "REPOSITORY", "TAG", "IMAGE ID", "SIZE")
}

⇧ デフォルトだと、

  1. CONTAINER
  2. REPOSITORY
  3. TAG
  4. IMAGE ID
  5. SIZE

の5項目が出力されるっぽい。

なのだが、「1. CONTAINER」と「2. REPOSITORY」については、どの情報を元に決定している値なのか皆目見当が付かない...

で、「Dockerイメージ」の削除については、

  1. docker rmi
  2. docker image rm

などを利用する形になるのだが、

docs.docker.jp

⇧ インプットが非常に分り辛い...

「使用例」の説明を見た感じでは、

⇧ 上記の説明にあるインプットが全量とするならば、インプットとしては、

  1. ショート ID
  2. ロング ID
  3. タグ
  4. digest

の4つが全量となるらしい。

なのだが、「ロングID」については、取得する方法の説明が無いという...

まぁ、「Docker Compose」を利用しているのであれば、「docker-compose.yaml」の「service」に紐付く「Dockerイメージ」で絞り込みたいところなのだが、残念ながら、ネットの情報を漁っていた感じでは、見たらなかった...

ChatGPT氏に質問した感じでは、

■DockerイメージをREPOSITORYで絞り込んで削除する

docker-compose -f [Composeファイルの配置されているディレクトリ]/docker-compose.yaml images | awk '$2 == "<REPOSITORY_NAME>" {print $1}' | xargs docker rmi

⇧ のような回答が返ってきた。

無茶苦茶、微妙...

とりあえず、公式のドキュメントでコマンドの実行結果のアウトプットぐらいは掲載して欲しい気はする...

毎度モヤモヤ感が半端ない…

今回はこのへんで。