Using docker buildkit's go client, how do I add an entrypoint?

1k Views Asked by At

For reasons of precise control of our builds we are using the new buildkit (moby/buildkit) directly. So without a Dockerfile.

We are creating a script like this example: https://github.com/moby/buildkit/blob/master/examples/buildkit0/buildkit.go

While it works (great), documentation is lacking.

How do I add an entrypoint? (i.e. default command to run)

and

How do I set the default workdir for when the container starts?

and

How do I set which ports to expose?

2

There are 2 best solutions below

1
On BEST ANSWER

LLB layer in BuildKit does not deal with images. It's one specific exporter for the build result. If you use a frontend like Dockerfile, it will prepare an image config for the exporter as well as invoking the LLB build. If you are using LLB directly you need to create an image config yourself as well. If you use buildctl this would look something like buildctl build --output 'type=docker,name=test,"containerimage.config={""Config"":{""Cmd"":[""bash""]}}"'

In Go API you would pass this with ExportEntry https://godoc.org/github.com/moby/buildkit/client#ExportEntry attributes. The image format is documented at https://github.com/moby/moby/blob/master/image/spec/v1.2.md .

Note that you don't need to fill RootFS in the image config. BuildKit will fill this in automatically. More background info https://github.com/moby/buildkit/issues/1041

0
On

Tõnis answer actually helped me solve it. I'm also posting here for an example for how to do it.

config := Config{
    Cmd:        cmd,
    WorkingDir: "/opt/company/bin",
    ExposedPorts: map[string]struct{}{
        "80/tcp":   {},
        "8232/tcp": {},
    },
    Env: []string{"PATH=/opt/company/bin:" + system.DefaultPathEnv},
}
imgConfig := ImgConfig{
    Config: config,
}
configStr, _ := json.Marshal(imgConfig)

Exports: []client.ExportEntry{
    {
        Type: "image",
        Attrs: map[string]string{
            "name":                  manifest.Tag,
            "push":                  "true",
            "push-by-digest":        "false",
            "registry.insecure":     strconv.FormatBool(insecureRegistry),
            "containerimage.config": string(configStr),
        },
    },
},