无法使用 Pulumi 构建 dockerfile

Can't build dockerfile with Pulumi

我正在尝试使用 Pulumi 构建一个 docker 文件。我有以下 Pulumi 代码

func main() {
    pulumi.Run(func(ctx *pulumi.Context) error {
        imageName := "server"
        _, err = docker.NewImage(ctx, imageName, &docker.ImageArgs{
            ImageName: pulumi.Sprintf("gcr.io/gadic-310112/%s:latest", imageName),
            SkipPush: pulumi.Bool(true),
            Build: &docker.DockerBuildArgs{
                Dockerfile: pulumi.String("Dockerfile"),
            },
        })
        if err != nil {
            return err
        }

    }
}

但是,当我 运行 pulumi preview 我得到以下错误:

Diagnostics:
  pulumi:pulumi:Stack (server-prod):
    error: program failed: docker build -f Dockerfile . -t gcr.io/gadic-310112/server:latest failed with error: exit status 1
    exit status 1

    error: an unhandled error occurred: program exited with non-zero exit code: 1

  docker:image:Image (server):
    error: #1 [internal] load build definition from Dockerfile
    #1 sha256:921a08a3c227abd8c3811effc689fa5319db237c32a4adf2b255007a51af9ef8
    #1 transferring dockerfile: 2B 0.0s done
    #1 DONE 0.0s
    failed to solve with frontend dockerfile.v0: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount052159980/Dockerfile: no such file or directory

有趣的是,Pulumi 似乎是 运行宁 docker build -f Dockerfile . -t gcr.io/gadic-310112/server:latest 幕后黑手。当我从我的终端 运行 它成功时没有任何错误。

 ❯❯❯ docker build -f Dockerfile . -t gcr.io/gadic-310112/server:latest
[+] Building 11.3s (16/18)
 => [internal] load build definition from Dockerfile                                                                                                                                                   0.0s
 => => transferring dockerfile: 1.87kB                                                                                                                                                                 0.0s
 => [internal] load .dockerignore                                                                                                                                                                      0.0s
 => => transferring context: 34B                                                                                                                                                                       0.0s
....

看来 Pulumi 使用的工作目录可能与调用它的地方不同。那可能吗?还有其他可能发生的事情吗?

我注意到,如果我不在 DockerBuildArgs 中包含 Dockerfile,那么 pulumi 生成的命令是 docker build -f . -t gcr.io/gadic-310112/server:latest,这绝对是不正确的。如果我在本地执行该命令,我会得到以下信息:

 ❮❮❮ docker build -f  . -t gcr.io/gadic-310112/server:latest
"docker build" requires exactly 1 argument.
See 'docker build --help'.

Usage:  docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile

编辑:我还尝试将绝对路径传递到包含我的 Dockerfile 的文件夹,作为 BuildArgs

Context
            Build: &docker.DockerBuildArgs{
                Dockerfile: pulumi.String("Dockerfile"),
                Context: pulumi.String("/Users/paymahn/gadic/server"),
            },

这仍然会出现与以前相同的错误,即使底层 docker build 命令具有绝对路径:

Diagnostics:
  docker:image:Image (server):
    error: #1 [internal] load build definition from Dockerfile
    #1 sha256:16f11ab26c775f06385c0fde07864ed70b428d13662aa2be42823751fb5143f4
    #1 transferring dockerfile: 2B 0.0s done
    #1 DONE 0.1s
    failed to solve with frontend dockerfile.v0: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount684750851/Dockerfile: no such file or directory

  pulumi:pulumi:Stack (server-prod):
    error: program failed: docker build -f Dockerfile /Users/paymahn/gadic/server -t gcr.io/gadic-310112/server:latest failed with error: exit status 1
    exit status 1

    error: an unhandled error occurred: program exited with non-zero exit code: 1

编辑 2:这是我的文件布局

/Users/paymahn/gadic/server/
                            infra/
                                  main.go # this is the go program that pulumi runs
                            src/ # this is where the source code for my server lives
                            Dockerfile
                            Pulumi.yaml
                            Pulumi.prod.yaml # the pulumi stack config
                            go.sum
                            go.mod
                            .git/ # this is the root of my git repository
                            

您的 Pulumi 项目构建在您的 infra 目录中,而不是您的 Pulumi.yaml 所在的文件夹中。

Pulumi 提供者需要使用 docker 构建上下文了解 Dockerfile 的路径(有关这些 here 的更多信息)

添加上下文应该可以解决这个问题:

func main() {
    pulumi.Run(func(ctx *pulumi.Context) error {
        imageName := "server"
        _, err = docker.NewImage(ctx, imageName, &docker.ImageArgs{
            ImageName: pulumi.Sprintf("gcr.io/gadic-310112/%s:latest", imageName),
            SkipPush: pulumi.Bool(true),
            Build: &docker.DockerBuildArgs{
                Dockerfile: pulumi.String("Dockerfile"),
                Context: "../", # note I'm adding the context here
            },
        })
        if err != nil {
            return err
        }

    }
}