9

Need help.I have code to exec command from docker container. Need corrently get stdout from exec command.

execConfig:= types.ExecConfig{Tty:false,AttachStdout:true,AttachStderr:false,Cmd:command}
    respIdExecCreate,err := cli.ContainerExecCreate(context.Background(),dockerName,execConfig)
    if err != nil {
        fmt.Println(err)
    }
    respId,err:=cli.ContainerExecAttach(context.Background(),respIdExecCreate.ID,types.ExecStartCheck{})
    if err != nil {
        fmt.Println(err)
    }
    scanner := bufio.NewScanner(respId.Reader)
    for scanner.Scan() {
       fmt.Println(output)
}

From output i see interesting situation: Screen from gyazo

How corrently remove bytes ?

I send simply command := []string{"echo","-n", "hello word"}

1 Answer 1

13

I've faced with same issue, this is how stderr and stdout looks for me:

StdOut: "\x01\x00\x00\x00\x00\x00\x00\thello world\n"
StdErr: "\x01\x00\x00\x00\x00\x00\x00fError: Exec command has already run\r\n"

I've checked docker source code and found answer here:

https://github.com/moby/moby/blob/8e610b2b55bfd1bfa9436ab110d311f5e8a74dcb/integration/internal/container/exec.go#L38

looks like this leading bytes used especially for marking stdout and stderr bytes.

And there is a library "github.com/docker/docker/pkg/stdcopy" which can split stdout and stderr from stream reader:

type ExecResult struct {
    StdOut string
    StdErr string
    ExitCode int
}

func Exec(ctx context.Context, containerID string, command []string) (types.IDResponse, error) {
    docker, err := client.NewEnvClient()
    if err != nil {
        return types.IDResponse{}, err
    }
    defer closer(docker)

    config :=  types.ExecConfig{
        AttachStderr: true,
        AttachStdout: true,
        Cmd: command,
    }

    return docker.ContainerExecCreate(ctx, containerID, config)
}

func InspectExecResp(ctx context.Context, id string) (ExecResult, error) {
    var execResult ExecResult
    docker, err := client.NewEnvClient()
    if err != nil {
        return execResult, err
    }
    defer closer(docker)

    resp, err := docker.ContainerExecAttach(ctx, id, types.ExecConfig{})
    if err != nil {
        return execResult, err
    }
    defer resp.Close()

    // read the output
    var outBuf, errBuf bytes.Buffer
    outputDone := make(chan error)

    go func() {
        // StdCopy demultiplexes the stream into two buffers
        _, err = stdcopy.StdCopy(&outBuf, &errBuf, resp.Reader)
        outputDone <- err
    }()

    select {
    case err := <-outputDone:
        if err != nil {
            return execResult, err
        }
        break

    case <-ctx.Done():
        return execResult, ctx.Err()
    }

    stdout, err := ioutil.ReadAll(&outBuf)
    if err != nil {
        return execResult, err
    }
    stderr, err := ioutil.ReadAll(&errBuf)
    if err != nil {
        return execResult, err
    }

    res, err := docker.ContainerExecInspect(ctx, id)
    if err != nil {
        return execResult, err
    }

    execResult.ExitCode = res.ExitCode
    execResult.StdOut = string(stdout)
    execResult.StdErr = string(stderr)
    return execResult, nil
}
Sign up to request clarification or add additional context in comments.

1 Comment

It works for me! Thanks for pointing integration tests directly on the moby/moby codebase! +1 for that. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.