packer(4)integration-docker

目的

通过packer构建docker镜像

packer/integrations/hashicorp/docker

详细的plugin说明及支持配置

1.验证构建docker镜像过程

1.docker-ubunut.pkr.hcl

source "docker" "ubuntu" {
  image  = "ubuntu:latest"
  commit = true
}

build {
  sources = ["source.docker.ubuntu"]
  provisioner "shell" {
    inline = ["echo 'Hello from Docker!' >/gaga"]
  }
  post-processor "docker-tag" {
    repository = "test"
    tags = ["v1"]
  }
}

解释:

  1. builders:

    • "type": "docker": 使用 Docker 构建器。
    • "image": "ubuntu:latest": 从 ubuntu:latest 基础镜像开始。
    • "commit": true: 在构建完成后提交更改,生成新的镜像。
  2. provisioners:

    • "type": "shell": 使用 shell 脚本配置镜像。
    • "inline": 在容器内运行的命令,写一个文件
  3. post-processors:

    • "repository": "docker-tag": 将生成的镜像标记为 test:v1

2.packer 构建镜像

➜  hcl packer build 1.pkr.hcl 
docker.ubuntu: output will be in this color.

### 1.创建共享目录
==> docker.ubuntu: Creating a temporary directory for sharing data...

### 2.拉取镜像
==> docker.ubuntu: Pulling Docker image: ubuntu:latest 
    docker.ubuntu: latest: Pulling from library/ubuntu
    docker.ubuntu: Digest: sha256:72297848456d5d37d1262630108ab308d3e9ec7ed1c3286a32fe09856619a782
    docker.ubuntu: Status: Image is up to date for ubuntu:latest
    docker.ubuntu: docker.io/library/ubuntu:latest

### 3.运行容器
==> docker.ubuntu: Starting docker container...
    docker.ubuntu: Run command: docker run -v /Users/mvpbang/.config/packer/tmp3963359971:/packer-files -d -i -t --entrypoint=/bin/sh -- ubuntu:latest
    docker.ubuntu: Container ID: fa4de6de45e41d8d56645c17aef5e121e83e0b07640cb77ad019a9fcbc2cd849

### 4.连接进去执行脚本
==> docker.ubuntu: Using docker communicator to connect: 192.168.215.2
==> docker.ubuntu: Provisioning with shell script: /var/folders/rr/rgbwqqyn077fxsdfk_y3dq4c0000gn/T/packer-shell3824029857

### 5.提交变化
==> docker.ubuntu: Committing the container
    docker.ubuntu: Image ID: sha256:02ce03a9b02c04fa7110201ff42663d52312bd2ff532049dcd4a63c1df976e93
==> docker.ubuntu: Killing the container: fa4de6de45e41d8d56645c17aef5e121e83e0b07640cb77ad019a9fcbc2cd849

### 6.打标记
==> docker.ubuntu: Running post-processor:  (type docker-tag)
    docker.ubuntu (docker-tag): Tagging image: sha256:02ce03a9b02c04fa7110201ff42663d52312bd2ff532049dcd4a63c1df976e93
    docker.ubuntu (docker-tag): Repository: test:v1
Build 'docker.ubuntu' finished after 5 seconds 253 milliseconds.

==> Wait completed after 5 seconds 254 milliseconds

### 7.导入镜像到本地仓库
==> Builds finished. The artifacts of successful builds are:
--> docker.ubuntu: Imported Docker image: sha256:02ce03a9b02c04fa7110201ff42663d52312bd2ff532049dcd4a63c1df976e93
--> docker.ubuntu: Imported Docker image: test:v1 with tags test:v1

3.运行测试

➜  hcl docker images |grep test
test                                                    v1             02ce03a9b02c   7 seconds ago    78.1MB

➜  hcl docker run --rm -it test:v1 cat /gaga
Hello from Docker!

2.构建后导出镜像

source "docker" "example" {
  image = "ubuntu"
  export_path = "image.tar"
}

build {
  sources = ["source.docker.example"]
}


pakcer build x.pkr.hcl

3.安装nginx

3.1 ngx.pkr.hcl

source "docker" "ubuntu" {
  image  = "ubuntu:20.04"
  commit = true
}

build {
  sources = ["source.docker.ubuntu"]
  provisioner "shell" {
    inline = [
    "export DEBIAN_FRONTEND=noninteractive",
        "apt-get update",
        "apt-get install -y nginx",
        "echo 'Hello from Packer!' > /var/www/html/index.html"
      ]
  }
  post-processor "docker-tag" {
    repository = "test"
    tags = ["v2"]
  }
}

3.2测试

docker run --name ngx  --rm  test:v2 nginx -g  'daemon off;'

➜  ~ pod_ip=$(docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ngx)
➜  ~ curl $pod_ip
Hello from Packer!

3.3优化.pkr.hcl修改image metadata

source "docker" "ubuntu" {
  image  = "ubuntu:20.04"
  commit = true
    changes = [
       "CMD [\"nginx\", \"-g\", \"daemon off;\"]"
    ]
}

build {
  sources = ["source.docker.ubuntu"]
  provisioner "shell" {
    inline = [
  "export DEBIAN_FRONTEND=noninteractive",
        "apt-get update",
        "apt-get install -y nginx",
        "echo 'Hello from Packer!' > /var/www/html/index.html"
      ]
  }
  post-processor "docker-tag" {
    repository = "test"
    tags = ["v4"]
  }
}
docker run --name ngx --rm   test:v4

pod_ip=$(docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ngx) ; curl $pod_ip