Upgrade to Pro — share decks privately, control downloads, hide ads and more …

10分でDeep Dive: コンテナイメージのアノ問題を理解する/Understanding...

10分でDeep Dive: コンテナイメージのアノ問題を理解する/Understanding-the-Well-Known-Container-Image-Issue

2025年1月31日にNBS 新春ライトニングトーク会で発表した資料です。

コンテナは仮想マシンと違いOSを持たないという特徴があります。ですが実際には各Linuxディストリビューションのコンテナイメージがあり、起動したコンテナ内にもOSと思わしき情報があります。この点についてscratchからコンテナイメージをビルドして解説します。

Shion Tanaka

January 31, 2025
Tweet

More Decks by Shion Tanaka

Other Decks in Technology

Transcript

  1. • 田中 司恩(タナカ シオン) / @tnk4on ◦ https://tnk4on.github.io/ • Red

    Hatのソリューションアーキテクト。Red Hatへの転職と同時に福岡に移 住。 • 大阪芸術大学音楽工学コース卒。在学中よりメディア・アート、サウンド・アー ト作品の制作を行う。 • コンテナに興味を持ち、OpenShiftやPodmanに関する情報をブログ、雑誌/ 書籍に多数投稿。「Podmanイン・アクション」の著者の1人。 • 2020年頃よりPodmanコミュニティに参加、PR/Issue/翻訳などを行い、 Podmanの日本語情報発信を積極的に展開中。 • 最近はPodmanを推す人 • 音とテクノロジーが重なる領域が好物のデジタルクリエイター。 自己紹介
  2. よくあるコンテナの説明図 ハードウェア OS ハイパーバイザー ハードウェア OS コンテナランタイム 仮想マシン OS Bin/Library

    App App 仮想マシン OS Bin/Library App App コンテナ Bin/ Library App コンテナ Bin/ Library App コンテナ Bin/ Library App コンテナ 仮想マシン
  3. よくあるコンテナの説明図 ハードウェア OS ハイパーバイザー ハードウェア OS コンテナランタイム 仮想マシン OS Bin/Library

    App App 仮想マシン OS Bin/Library App App コンテナ Bin/ Library App コンテナ Bin/ Library App コンテナ Bin/ Library App コンテナ 仮想マシン コンテナには OSがない
  4. コンテナイメージ(ubi9)を実行してOS情報を参照してみる UBIイメージはRHELコンテンツのサブセット $ podman run ubi9 cat /etc/redhat-release Red Hat

    Enterprise Linux release 9.5 (Plow) ubi9イメージを実行し、/etc/redhat-releaseを参照する RHELが動いている?
  5. コンテナ Why distroless containers aren't the security solution you think

    they are https://www.redhat.com/ja/blog/why-distroless-containers-arent-security-solution-you-think-they-are Linuxカーネル コンテナランタイム パッケージマネージャーの入っていないコンテナイメージ ファイルシステム いわゆるディストロレス コンテナイメージ
  6. scratchコンテナイメージ 最小限のコンテナイメージ FROM scratch scratchイメージのContainerfile ▸ ベースイメージの中で最も小さく、何も含まれていない。 ▸ ゼロからカスタムイメージを作成するための空のベースイメージ。 ▸

    追加のライブラリやツールが一切含まれていないため、非常に軽量。 ▸ 必要なものだけを追加することで、非常にカスタマイズされたコンテナイメージ を作成できる。
  7. 通常のコンテナイメージとscratchイメージを比較する podman image treeコマンドを使用 $ podman pull registry.access.redhat.com/ubi9 $ podman

    image tree ubi9 Image ID: 3955a6e8054b Tags: [registry.access.redhat.com/ubi9:latest] Size: 251.5MB Image Layers ├── ID: 202739b2c1be Size: 251.5MB └── ID: a52bb7de32eb Size: 3.584kB Top Layer of: [registry.access.redhat.com/ubi9:latest] UBI9のコンテナイメージのレイヤー情報 $ podman build -t scratch . $ podman image tree scratch Image ID: c6d5a9a34a65 Tags: [localhost/scratch:latest] Size: 1.04kB No Image Layers scratchのコンテナイメージのレイヤー情報
  8. scratchイメージを実行する $ podman run -it scratch Error: preparing container fd0ec2d594d4ba20d0cc3150cab99499a34d7be2c4bf1f3c5d4bf6c407d1691e

    for attach: crun: executable file `` not found in $PATH: No such file or directory: OCI runtime attempted to invoke a command that was not found scratchイメージを実行してもエラーになる(シェルにすら入れない)
  9. scratchイメージにシェル(sh)を追加する シェルにbusyboxを使用 FROM scratch # Copy busybox static binary from

    busybox image COPY --from=busybox:1.31.0-uclibc /bin/busybox /bin/busybox # Install busybox RUN ["/bin/busybox", "--install", "/bin"] # Set the entrypoint to sh ENTRYPOINT ["/bin/sh"] busyboxを追加するようにカスタマイズしたContainerfile
  10. scratchイメージにシェル(sh)を追加する シェルにbusyboxを使用 $ podman build -t scratch . STEP 1/4:

    FROM scratch STEP 2/4: COPY --from=busybox:1.31.0-uclibc /bin/busybox /bin/busybox -→ 42078f70895a STEP 3/4: RUN ["/bin/busybox", "--install", "/bin"] -→ 9a721967bba7 STEP 4/4: ENTRYPOINT ["/bin/sh"] COMMIT scratch -→ 3a2d1dd912a3 Successfully tagged localhost/scratch:latest 3a2d1dd912a34d3ad4d000df73bc9f8352b0e9a05850002dde606ccdf324033d $ $ podman run -it scratch / # ls bin dev etc proc run sys カスタマイズしたContainerfileをビルドし、コンテナを実行する
  11. scratchイメージにシェル(sh)を追加する シェルにbusyboxを使用 $ podman image tree scratch:latest Image ID: 3a2d1dd912a3

    Tags: [localhost/scratch:latest] Size: 2.635MB Image Layers ├── ID: 5d9f8cd70000 Size: 1.213MB └── ID: a2e55300b051 Size: 1.42MB Top Layer of: シェルを追加したコンテナイメージのレイヤー情報
  12. scratchイメージにパッケージマネージャーでアプリを追加する Buildahを使ってコンテナイメージを作成する Buildah とは https://www.redhat.com/ja/topics/containers/what-is-buildah コンテナランタイム scratchコンテナ Bin/ Library dnfコマンド

    RHEL コンテナイメージの 操作 Buildah Podman は Buildah と同じコードを利用して作成されています。 Podman のpodman build コマンドは Buildah の機能のサブセットを使用しています。 Buildah は単独のビルドツールとして使うことができ、さらに Podman ではできない柔軟なコンテナイメージの作成ができる機能を搭載しています。
  13. Buildah を使用したゼロからのイメージの作成 19.6. Buildah を使用したゼロからのイメージの作成 | Red Hat Product Documentation

    https://docs.redhat.com/ja/documentation/red_hat_enterprise_linux/9/html/building_running_and_managing_containers/proc_creating-images-from-scratch-with-buildah_ass embly_building-container-images-with-buildah#proc_creating-images-from-scratch-with-buildah_assembly_building-container-images-with-buildah # buildah from scratch working-container # scratchmnt=$(buildah mount working-container) # dnf install -y --releasever=9 --installroot=$scratchmnt redhat-release # dnf install -y --setopt=reposdir=/etc/yum.repos.d \ --installroot=$scratchmnt \ --setopt=cachedir=/var/cache/dnf httpd # mkdir -p $scratchmnt/var/www/html echo "Your httpd container from scratch works!" > $scratchmnt/var/www/html/index.html # buildah config --cmd "/usr/sbin/httpd -DFOREGROUND" working-container # buildah config --port 80/tcp working-container # buildah commit working-container localhost/myhttpd:latest # podman run -it myhttpd cat /etc/redhat-release Red Hat Enterprise Linux release 9.5 (Plow) scratchイメージにhttpdをインストールし、HTMLコンテンツを追加して、自動的にサービス起動するようにする
  14. Ubuntuでもゼロからイメージの作成は可能 Ubuntu上でBuildahを実行する # buildah from scratch # scratchmnt=$(buildah mount working-container)

    # mmdebstrap --variant=apt noble $scratchmnt http://ports.ubuntu.com/ubuntu-ports/ # buildah commit working-container ubuntu-noble # podman run --rm -it ubuntu-noble:latest bash root@e872586bff19:/# cat /etc/os-release PRETTY_NAME="Ubuntu 24.04 LTS" NAME="Ubuntu" VERSION_ID="24.04" VERSION="24.04 LTS (Noble Numbat)" VERSION_CODENAME=noble ID=ubuntu ID_LIKE=debian HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/priv acy-policy" UBUNTU_CODENAME=noble LOGO=ubuntu-logo scratchイメージにubuntu-nobleをインストール