Docker Golang SDK-如何将容器标准输出重定向到文件

Using the docker golang sdk the following method can be used to create a container and bind it's output to stdout.

resp, err := cli.ContainerCreate(ctx, &container.Config{
        Image:        "alpine",
        Cmd:          []string{"echo", "Hello World"},
        AttachStdout: true,
    }, nil, nil, "")

How can I redirect this output to a file using the SDK ? I'm using the official SDK of docker - github.com/docker/docker/client

You can use something like below

  out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true})
  if err != nil {
    panic(err)
  }

  f, err := os.Create("/tmp/clogs")

  io.Copy(f, out)

But make sure to to do that after you have started the container, Create will only create the container and not start it