4

我正在尝试使用打包器配置 docker:dind 映像,它将构建另一个 docker 映像并使用以下构建脚本在 docker 内运行它:

{
  "builders": [
    {
      "type": "docker",
      "image": "docker:dind",
      "privileged": true,
      "commit": "true"
    }
  ],
  "provisioners": [
    {
      "type": "file",
      "source": "../docker/",
      "destination": "/docker"
    },
    {
      "type": "shell",
      "inline": [
        "docker build -t my-service /docker"
      ]
    }
  ],
  "post-processors": [
    [
      {
        "type": "docker-tag",
        "repository": "my-service",
        "tag": "latest"
      }
    ]
  ]
}

当我运行此脚本时,它失败并出现以下错误:

$ packer build build.json
docker output will be in this color.

==> docker: Creating a temporary directory for sharing data...
==> docker: Pulling Docker image: docker:dind
    docker: dind: Pulling from library/docker
    docker: Digest: sha256:21fe82c6e54e2db31e64f79ec7ac75c6a8f7469cdc56687ced97d6fc3e75e6cd
    docker: Status: Image is up to date for docker:dind
==> docker: Starting docker container...
    docker: Run command: docker run --privileged -v C:\Users\user\packer.d\tmp:/packer-files -d -i -t --entrypoint=/bin/sh -- docker:dind
    docker: Container ID: 7fa2ae91eaf6111ce6d0567f1e523100c777311449454b0df8d6d49c84c477d6
==> docker: Using docker communicator to connect: 172.17.0.2
==> docker: Uploading ../docker/ => /docker
==> docker: Provisioning with shell script: C:\Users\user\AppData\Local\Temp\packer-shell164551847
==> docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
==> docker: Killing the container: 7fa2ae91eaf6111ce6d0567f1e523100c777311449454b0df8d6d49c84c477d6
Build 'docker' errored: Script exited with non-zero exit status: 1.Allowed exit codes are: [0]

==> Some builds didn't complete successfully and had errors:
--> docker: Script exited with non-zero exit status: 1.Allowed exit codes are: [0]

==> Builds finished but no artifacts were created.

问题是,当打包程序启动容器时,它会覆盖在 docker:dind 映像中配置的默认入口点 ["dockerd-entrypoint.sh"],并使用它自己的入口点 ["/bin/sh"]。这可以防止 docker 进程内的 docker 启动并且无法执行 shell 脚本。

我尝试使用以下选项更改入口点和 run_command,但在第一种情况下它没有效果(我假设它会在提交图像时在最后更新)并且在后者中它不能执行任何脚本。

"changes": [
  "ENTRYPOINT [dockerd-entrypoint.sh]"
],
"run_command": ["-d", "-i", "-t", "--entrypoint=dockerd-entrypoint.sh", "--", "{{.Image}}"]

另一种选择是将“[dockerd-entrypoint.sh]”作为内联 shell 脚本运行(首先),但这会阻止进程并且没有执行后续步骤。

我的选择已经不多了,我需要一些帮助来了解它是如何工作的以及是否有一些解决方案。

4

1 回答 1

1

在您的inline部分中,首先在后台运行 docker 守护程序,例如dockerd-entrypoint.sh & sleep 5.

于 2019-04-17T17:49:53.290 回答