SwiftD
SwiftD

Reputation: 6079

explain and remove useless bytes at the start of docker exec response

Im running the docker api function:

func (cli *Client) ContainerExecAttach(ctx context.Context, execID string, config types.ExecConfig) (types.HijackedResponse, error)

When I read the response using the built in bufio reader:

line, _, err := resp.Reader.ReadLine()

I get no error but if I try to convert the bytes to a string the app crashes - the app in question is a windows service, and i think because of this I'm struggling to get an error out of it.

I have now managed to view the output using (logs to event viewer):

logger.Infof("res: %q", line)

the output from this is:

res: "\x01\x00\x00\x00\x00\x00\x00X{\"updating\":false,\"scanning\":false,\"progress\":0,\"discovering\":false,\"registered\":false}"

Now, I was expecting the json but the funky prefix is most unwelcome, any idea what these bytes are are and why they are there? I'm hoping this will inform my next question; what is the cleanest way to remove them or not receive them in the first place?

I'm currently left trimming the bytes by a fixed length but I think I need to do something more sophisticated. I'm thinking left trim these bytes: \x01 \x00 but not sure if I should expect any others

Upvotes: 1

Views: 655

Answers (1)

lmars
lmars

Reputation: 2532

The HTTP response you are getting back is the Docker stream format as documented here.

You should consider using the github.com/docker/docker/pkg/stdcopy.StdCopy function to demultiplex the stream into stdout / stderr.

Upvotes: 5

Related Questions