Skip to content

Cannot export image config with environment variables #2362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
csweichel opened this issue Sep 13, 2021 · 2 comments
Closed

Cannot export image config with environment variables #2362

csweichel opened this issue Sep 13, 2021 · 2 comments

Comments

@csweichel
Copy link
Contributor

I'm trying to build an image using LLB directly, and fail to make it export the image config as well. Naturally this results in missing environment variables, and other bits of configuration. During the build the config is as expected - the env var is present.
This issue persists if I write the LLB to stdout and pipe it to buildctl build --output .... I reckon I'm misunderstanding how to use LLB.

Is there some bit of documentation that I'm missing?

The following code reproduces the issues:

package main

import (
	"context"
	"flag"
	"log"
	"os"

	"github.com/containerd/console"
	"github.com/moby/buildkit/client"
	"github.com/moby/buildkit/client/llb"
	"github.com/moby/buildkit/client/llb/imagemetaresolver"
	"github.com/moby/buildkit/util/progress/progressui"
	"golang.org/x/sync/errgroup"
)

var dest = flag.String("dest", "", "destination ref to push to")

func main() {
	flag.Parse()
	if *dest == "" {
		log.Fatal("missing --dest")
	}

	ctx := context.Background()
	state := llb.Image("docker.io/library/alpine:latest", llb.WithMetaResolver(imagemetaresolver.Default())).
		AddEnv("this-env-var", "is-not-exported").
		Dir("/some-directory").
		Run(llb.Shlex("env")).
		Root()

	def, err := state.Marshal(ctx, llb.LinuxAmd64)
	if err != nil {
		log.Fatal(err)
	}

	solveOpt := client.SolveOpt{
		Exports: []client.ExportEntry{
			{
				Type: "image",
				Attrs: map[string]string{
					"name": *dest,
					"push": "true",
				},
			},
		},
	}

	cl, err := client.New(ctx, "unix:///run/buildkit/buildkitd.sock", client.WithFailFast())
	if err != nil {
		log.Fatal(err)
	}
	eg, ctx := errgroup.WithContext(ctx)
	ch := make(chan *client.SolveStatus)
	eg.Go(func() error {
		_, err := cl.Solve(ctx, def, solveOpt, ch)
		if err != nil {
			return err
		}
		return nil
	})
	eg.Go(func() error {
		var c console.Console
		return progressui.DisplaySolveStatus(ctx, "", c, os.Stdout, ch)
	})
	err = eg.Wait()
	if err != nil {
		log.Fatal(err)
	}
}
@tonistiigi
Copy link
Member

@csweichel
Copy link
Contributor Author

Thank you very much for the pointer, that was very helpful indeed :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants