I'm trying to build a docker file with Pulumi. I have the following Pulumi code
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
}
}
}
However, when I run pulumi preview
I get the following error:
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
What's interesting is that it seems Pulumi is running docker build -f Dockerfile . -t gcr.io/gadic-310112/server:latest
under the hood. And when I run that from my terminal it succeeds without any error.
❯❯❯ 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
....
It seems that Pulumi might be using a different working directory than the place where it's getting invoked from. Is that possible? Is there something else that might be going on?
I've noticed that if I don't include a Dockerfile
in the DockerBuildArgs
then the command generated by pulumi is docker build -f . -t gcr.io/gadic-310112/server:latest
which is definitely incorrect. If I execute that command locally I get the following:
❮❮❮ 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
EDIT: I also tried passing the absolute path to the folder containing my Dockerfile as the Context
of the BuildArgs
Build: &docker.DockerBuildArgs{
Dockerfile: pulumi.String("Dockerfile"),
Context: pulumi.String("/Users/paymahn/gadic/server"),
},
and this still gets the same error as before, even though the underlying docker build
command has an absolute path:
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
EDIT 2: Here's the layout of my files
/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
Your Pulumi projects builds inside your infra
directory, not in the folder your Pulumi.yaml
is in.
The Pulumi provider needs to know the path of the Dockerfile
using the docker build context (more info about these here)
Adding the context should fix this:
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
}
}
}