LoginSignup
30
31

More than 5 years have passed since last update.

Packer で Docker イメージを作る

Posted at

Packer で Docker イメージを作る

Packerを使えばDockerのイメージをDockerfileを使わずビルドすることができる
つまり,Dockerfileの特有な記述を使わず,今まで慣れ親しんできたChefやPuppet,Ansibleのようなプロビジョニングツールを使ってDockerのイメージをビルドできる.

とのことなので Dockerfile 書かなくても良いじゃん?
という。

Packer サンプル試した

のサンプル試してみる
このサンプルはEC2

$ packer build \
-var 'aws_access_key=XXXXXXXXXX' \
-var 'aws_secret_key=XXXXXXXXXX' \
example.json
amazon-ebs output will be in this color.

==> amazon-ebs: Creating temporary keypair: packer 538fe89a-d16c-59c9-7b95-9422d008f01e
==> amazon-ebs: Creating temporary security group for this instance...
==> amazon-ebs: Authorizing SSH access on the temporary security group...
==> amazon-ebs: Launching a source AWS instance...
amazon-ebs: Instance ID: i-609c5332
==> amazon-ebs: Waiting for instance (i-609c5332) to become ready...
==> amazon-ebs: Waiting for SSH to become available...
==> amazon-ebs: Connected to SSH!
==> amazon-ebs: Stopping the source instance...
==> amazon-ebs: Waiting for the instance to stop...
==> amazon-ebs: Creating the AMI: packer-example 1401940122
amazon-ebs: AMI: ami-fa0eff92
==> amazon-ebs: Waiting for AMI to become ready...
==> amazon-ebs: Terminating the source AWS instance...
==> amazon-ebs: Deleting temporary security group...
==> amazon-ebs: Deleting temporary keypair...
Build 'amazon-ebs' finished.

==> Builds finished. The artifacts of successful builds are:
--> amazon-ebs: AMIs were created:

us-east-1: ami-fa0eff92

Packer の起動動作まとめ

EC2に適用した場合、作られるのは AMI イメージ。
EC2は出来るけど、イメージ作り終わったら terminate される。

不要な AMI イメージは削除する感じ。
EC2の起動はAMIイメージを元に自分で行う感じ。

流れとして、

  1. Chef 用意する
  2. Packer で AMI イメージ作る / Docker イメージ作る
  3. AMI/Dockerイメージを起動する

って感じにできるぽい。

Chef on Docker

# docker-chef.json
{
  "builders":[{
    "type": "docker",
    "image": "phusion/baseimage",
    "export_path": "docker-image.tar"
  }],
  "provisioners":[
    {
      inline: [
        "apt-get -y install curl"
      ],
      "type": "shell"
    },
    {
      "type": "chef-solo",
      "cookbook_paths": ["cookbooks"],
      "run_list": []
    }
  ],
  "post-processors": [
    {
      "type": "docker-import",
      "repository": "skyriser/docker-test",
      "tag": "0.1"
    }
  ]
}
$ sudo -E packer build docker-chef.json
docker output will be in this color.

これだけで docker イメージできる。
packer っていう別のバイナリがいることもあるようなので注意。
フルパスで指定すると確実かもしれない。

$ sudo docker run -t skyriser/docker-test
30
31
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
30
31