20.6. Buildah を使用したゼロからのイメージの作成
ベースイメージから開始する代わりに、最低限のコンテナーメタデータのみを保持する新しいコンテナーを作成できます。
スクラッチコンテナーからイメージを作成するときは、次のことを考慮してください。
- scratch イメージに依存関係のない実行ファイルをコピーして、minimal コンテナーを機能させるため、いくつかの設定を行うことができます。
-
RPM データベースを初期化し、
dnfやrpmなどのツールを使用するために、コンテナーにリリースパッケージを追加する必要があります。 - パッケージを多数追加する場合は、scratch イメージではなく、標準の UBI イメージまたは UBI minimal イメージの使用を検討してください。
前提条件
-
container-toolsメタパッケージがインストールされている。
手順
Web サービス httpd をコンテナーに追加し、実行するように設定できます。
空のコンテナーを作成します。
# buildah from scratch working-containerworking-containerコンテナーをマウントし、マウントポイントパスをscratchmnt変数に保存します。# scratchmnt=$(buildah mount working-container) # echo $scratchmnt /var/lib/containers/storage/overlay/be2eaecf9f74b6acfe4d0017dd5534fde06b2fa8de9ed875691f6ccc791c1836/mergedscratch イメージで RPM データベースを初期化し、
redhat-releaseパッケージを追加します。# dnf install -y --releasever=8 --installroot=$scratchmnt redhat-releasehttpdサービスをscratchディレクトリーにインストールします。# dnf install -y --setopt=reposdir=/etc/yum.repos.d \ --installroot=$scratchmnt \ --setopt=cachedir=/var/cache/dnf httpd$scratchmnt/var/www/html/index.htmlファイルを作成します。# mkdir -p $scratchmnt/var/www/html # echo "Your httpd container from scratch works!" > $scratchmnt/var/www/html/index.htmlコンテナーから直接
httpdデーモンを実行するようにworking-containerを設定します。# buildah config --cmd "/usr/sbin/httpd -DFOREGROUND" working-container # buildah config --port 80/tcp working-container # buildah commit working-container localhost/myhttpd:latest
検証
ローカルストレージ内のイメージをリスト表示します。
# podman images REPOSITORY TAG IMAGE ID CREATED SIZE localhost/myhttpd latest 08da72792f60 2 minutes ago 121 MBlocalhost/myhttpdイメージを実行し、コンテナーとホストシステム間のポートマッピングを設定します。# podman run -p 8080:80 -d --name myhttpd 08da72792f60Web サーバーをテストします。
# curl localhost:8080 Your httpd container from scratch works!