来自Golang API的Docker exec命令

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"}

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
"
StdErr: "\x01\x00\x00\x00\x00\x00\x00fError: Exec command has already run
"

I've cheched 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
}