I am trying to print Bash commands themselves from inside Docker using Go SDK. The following works:
docker run ubuntu /bin/bash -xc "echo test
echo hello
"
and prints the following results:
+ echo test
test
+ echo hello
hello
However, when perform the same operation in a Go application using Docker SDK, I get results of commands but not the commands themselves (this is the default Docker SDK example modified to take multi-line commands):
package main
import (
"context"
"io"
"os"
"log"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
)
func main() {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
script := `
echo hello
echo test
`
reader, err := cli.ImagePull(ctx, "docker.io/library/ubuntu", types.ImagePullOptions{})
if err != nil {
panic(err)
}
io.Copy(os.Stdout, reader)
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: "ubuntu",
Cmd: []string{"/bin/bash", "-c", script},
}, nil, nil, nil, "")
if err != nil {
panic(err)
}
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
panic(err)
}
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
select {
case err := <-errCh:
if err != nil {
panic(err)
}
case <-statusCh:
}
out, err := cli.ContainerLogs(ctx, resp.ID, types.ContainerLogsOptions{ShowStdout: true})
if err != nil {
panic(err)
}
stdcopy.StdCopy(os.Stdout, os.Stderr, out)
}
Is it possible to get these values from Docker logs?