Skip to content
\n

Before specifying version, we were just reading json logs from buildOutput.Body, but now that only serves the Successfully tagged message at the end.

\n

Could someone help understand how to visualize the builderkit build logs like you would the v1 builder logs?

","upvoteCount":3,"answerCount":3,"acceptedAnswer":{"@type":"Answer","text":"

This is what I have figured out so far:

\n

You need to use the jsonmessage package (github.com/docker/docker/pkg/jsonmessage`) to read the response body like you did with V1.

\n

I think there is a helper function in that package you can use. I use a custom function that looks like this:

\n
func DockerMessages(in io.Reader, auxCallback func(jsonmessage.JSONMessage)) error {\n\tdec := json.NewDecoder(in)\n\n\tfor {\n\t\tvar jm jsonmessage.JSONMessage\n\t\tif err := dec.Decode(&jm); err != nil {\n\t\t\tif err == io.EOF {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\treturn fmt.Errorf(\"decode JSON message: %w\", err)\n\t\t}\n\n\t\t// If the message contains an error, return it to the caller\n\t\t// after writing. This is how Docker communicates things like\n\t\t// image builds failing.\n\t\tif jm.Error != nil {\n\t\t\treturn jm.Error\n\t\t}\n\n\t\t// Check for out of band data (like image IDs, digests, etc.)\n\t\tif jm.Aux != nil {\n\t\t\tif auxCallback != nil {\n\t\t\t\tauxCallback(jm)\n\t\t\t}\n\t\t\tcontinue // (docker ignores the rest if there is aux data)\n\t\t}\n\t\t\n\t\t// ...
\n

With Buildkit all of the data is on JSONMessage.Aux. You can switch on JSONMessage.ID to guess what the Aux type will be. For buildkit output from an image build it will be moby.buildkit.trace.

\n

The buildkit trace is a binary encoded protobuf message which is then JSON encoded, so it's a base64 JSON string. You can decode it like this:

\n
import controlapi \"github.com/moby/buildkit/api/services/control\"\n\nvar bytes []byte\nif err := json.Unmarshal(*jm.Aux, &bytes); err != nil {\n\tpanic(err)\n}\nresp := controlapi.StatusResponse{}\nif err := proto.Unmarshal(bytes, &resp); err != nil {\n\tpanic(err)\n}
\n

The proto is defined here.

\n

Edit: here's a more complete example that just prints the output to the terminal: https://gist.github.com/matt-allan/9d5419abd2c234011de175e891be48fb

\n

It uses the \"github.com/moby/buildkit/util/progress/progressui\" package to print the output.

","upvoteCount":2,"url":"https://github.com/moby/moby/discussions/43788#discussioncomment-13291612"}}}

How to Read BuildKit Build Logs #43788

Answered by matt-allan
dfoster42 asked this question in Q&A
Discussion options

You must be logged in to vote

This is what I have figured out so far:

You need to use the jsonmessage package (github.com/docker/docker/pkg/jsonmessage`) to read the response body like you did with V1.

I think there is a helper function in that package you can use. I use a custom function that looks like this:

func DockerMessages(in io.Reader, auxCallback func(jsonmessage.JSONMessage)) error {
	dec := json.NewDecoder(in)

	for {
		var jm jsonmessage.JSONMessage
		if err := dec.Decode(&jm); err != nil {
			if err == io.EOF {
				break
			}
			return fmt.Errorf("decode JSON message: %w", err)
		}

		// If the message contains an error, return it to the caller
		// after writing. This is how Docker communicates things like

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@dfoster42
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by dfoster42

This comment was marked as spam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants