mito’s blog

IT技術メインの雑記。思い立ったが吉日。

[Packer] HCL2形式でDockerイメージを作成する

はじめに

amazoncorrettoのDockerイメージを作成します。
JSON形式ではなく、HCL2形式で書きます。HCL2形式で。


環境

  • OS : Amazon Linux 2(Cloud 9)
  • Packer : 1.8.0
  • Docker : 20.10.13


Packerのインストール

$ sudo yum install -y yum-utils
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core                                                                                                            | 3.7 kB  00:00:00     
234 packages excluded due to repository priority protections
Package yum-utils-1.1.31-46.amzn2.0.1.noarch already installed and latest version
Nothing to do
$ sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
adding repo from: https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
grabbing file https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo to /etc/yum.repos.d/hashicorp.repo
repo saved to /etc/yum.repos.d/hashicorp.repo
$ sudo yum -y install packer
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
234 packages excluded due to repository priority protections
Resolving Dependencies
--> Running transaction check
---> Package packer.x86_64 0:1.8.0-1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================
 Package                         Arch                            Version                            Repository                          Size
=============================================================================================================================================
Installing:
 packer                          x86_64                          1.8.0-1                            hashicorp                           20 M

Transaction Summary
=============================================================================================================================================
Install  1 Package

Total download size: 20 M
Installed size: 145 M
Downloading packages:
packer-1.8.0-1.x86_64.rpm                                                                                             |  20 MB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : packer-1.8.0-1.x86_64                                                                                                     1/1 
  Verifying  : packer-1.8.0-1.x86_64                                                                                                     1/1 

Installed:
  packer.x86_64 0:1.8.0-1                                                                                                                    

Complete!
$ packer -version
1.8.0


  • Dockerイメージを作成するため、dockerグループにユーザを追加します。
    • cloud 9は、ec2-userが追加済みでした。
$ sudo usermod -aG docker ec2-user
$ cat /etc/group | grep docker
docker:x:992:ec2-user
$ sudo systemctl enable docker
$ sudo shutdown -r now


Packer templateの作成

  • まずは適当なフォルダを作成し、移動します。
$ mkdir packer_test
$ cd packer_test/


  • テンプレートを作成します。
    • ファイル名は「*.pkr.hcl」とします。
$ cat docker-amazoncorretto.pkr.hcl 
packer {
  required_plugins {
    // dockerプラグインを追加します。バージョンについては後述します。
    docker = {
      version = ">= v1.0.0"
      source  = "github.com/hashicorp/docker"
    }
  }
}

// dockerを指定し、利用したいイメージを記載します。
// [https://www.packer.io/plugins/builders/docker:title]
source "docker" "amazoncorretto" {
  image  = "amazoncorretto:18.0.1"
  // コンテナをイメージにコミットします。
  commit = true
}

build {
  name = "build-amazoncorretto"
  sources = [
    "source.docker.amazoncorretto"
  ]
  
  // 期待するイメージに向け、コマンドを記載します。
  // パッケージのインストールで止まらないようyを忘れずに。
  provisioner "shell" {
    inline = [
      "yum install -y git"
    ]
  }
  
  // 付けたいrepositoryとtagの値を記載します。
  post-processor "docker-tag" {
    repository = "baseimage"
    tags       = ["1.0.0"]
  }
}


$ packer init .
Installed plugin github.com/hashicorp/docker v1.0.3 in "/home/ec2-user/.config/packer/plugins/github.com/hashicorp/docker/packer-plugin-docker_v1.0.3_x5.0_linux_amd64"


  • packer fmtコマンドでインデントのずれなどフォーマットを整えてくれます。
    • 整形したファイルがなければ、何も表示されません。
$ packer fmt .
$


  • packer validate コマンドでテンプレートの構文と構成をチェックします。
    • エラーがあると、該当箇所を表示します。
$ packer validate .
The configuration is valid.


実行結果

  • packer buildコマンドを実行します。
$ packer build docker-amazoncorretto.pkr.hcl 
build-amazoncorretto.docker.amazoncorretto: output will be in this color.

==> build-amazoncorretto.docker.amazoncorretto: Creating a temporary directory for sharing data...
==> build-amazoncorretto.docker.amazoncorretto: Pulling Docker image: amazoncorretto:18.0.1
    build-amazoncorretto.docker.amazoncorretto: 18.0.1: Pulling from library/amazoncorretto
    build-amazoncorretto.docker.amazoncorretto: 8de5b65bd171: Pulling fs layer
    build-amazoncorretto.docker.amazoncorretto: ea513dd92a36: Pulling fs layer
    build-amazoncorretto.docker.amazoncorretto: 8de5b65bd171: Verifying Checksum
    build-amazoncorretto.docker.amazoncorretto: 8de5b65bd171: Download complete
    build-amazoncorretto.docker.amazoncorretto: ea513dd92a36: Verifying Checksum
    build-amazoncorretto.docker.amazoncorretto: ea513dd92a36: Download complete
    build-amazoncorretto.docker.amazoncorretto: 8de5b65bd171: Pull complete
    build-amazoncorretto.docker.amazoncorretto: ea513dd92a36: Pull complete
    build-amazoncorretto.docker.amazoncorretto: Digest: sha256:bf51a65227c2fb599f4ef9709149f16694072cfef6e3d2b8933f64fb7f43ea8a
    build-amazoncorretto.docker.amazoncorretto: Status: Downloaded newer image for amazoncorretto:18.0.1
    build-amazoncorretto.docker.amazoncorretto: docker.io/library/amazoncorretto:18.0.1
==> build-amazoncorretto.docker.amazoncorretto: Starting docker container...
    build-amazoncorretto.docker.amazoncorretto: Run command: docker run -v /home/ec2-user/.config/packer/tmp2830417922:/packer-files -d -i -t --entrypoint=/bin/sh -- amazoncorretto:18.0.1
    build-amazoncorretto.docker.amazoncorretto: Container ID: 31aff84d224c99d58e4afaf6f1b6d65a13c3281b4e4ea168d9ee25d98629dff1
()
    build-amazoncorretto.docker.amazoncorretto: Installing:
    build-amazoncorretto.docker.amazoncorretto:  git                       x86_64 2.32.0-1.amzn2.0.1           amzn2-core 126 k
()
    build-amazoncorretto.docker.amazoncorretto:   ustr.x86_64 0:1.0.4-16.amzn2.0.3
    build-amazoncorretto.docker.amazoncorretto:   util-linux.x86_64 0:2.30.2-2.amzn2.0.7
    build-amazoncorretto.docker.amazoncorretto:
    build-amazoncorretto.docker.amazoncorretto: Complete!
==> build-amazoncorretto.docker.amazoncorretto: Committing the container
    build-amazoncorretto.docker.amazoncorretto: Image ID: sha256:c13fe64d1aa9212105d68226f12f4ede5660f1a3e2a3a3211b9f4642cffc1c6c
==> build-amazoncorretto.docker.amazoncorretto: Killing the container: 74bde7cb62627ca378965b2e347368cefb3f937933374284ea8f0bbc9b766a06
==> build-amazoncorretto.docker.amazoncorretto: Running post-processor:  (type docker-tag)
    build-amazoncorretto.docker.amazoncorretto (docker-tag): Tagging image: sha256:c13fe64d1aa9212105d68226f12f4ede5660f1a3e2a3a3211b9f4642cffc1c6c
    build-amazoncorretto.docker.amazoncorretto (docker-tag): Repository: baseimage:1.0.0
Build 'build-amazoncorretto.docker.amazoncorretto' finished after 38 seconds 192 milliseconds.

==> Wait completed after 38 seconds 197 milliseconds

==> Builds finished. The artifacts of successful builds are:
--> build-amazoncorretto.docker.amazoncorretto: Imported Docker image: sha256:c13fe64d1aa9212105d68226f12f4ede5660f1a3e2a3a3211b9f4642cffc1c6c
--> build-amazoncorretto.docker.amazoncorretto: Imported Docker image: baseimage:1.0.0 with tags baseimage:1.0.0


  • Dockerイメージを確認します。
$ docker images
REPOSITORY       TAG             IMAGE ID       CREATED              SIZE
baseimage        1.0.0           c13fe64d1aa9   About a minute ago   912MB
amazoncorretto   18.0.1          05d288c3a055   3 weeks ago          464MB


  • 作成したイメージが確認できました。


その他

HCLに慣れると、Terraformも扱いやすいかなと思います。コメントも簡単に書けますし。