-
I've been trying to programmaticaly I have scoured quite a bit of docs and built something which is able to pull an image and start a container. Where I am having issues is the Here is my code: ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
panic(err)
}
defer cli.Close()
token, err := authenticateGcr()
if err != nil {
panic(err)
}
authConfig := registry.AuthConfig{
Username: "oauth2accesstoken",
Password: token.AccessToken,
}
encodedJSON, err := json.Marshal(authConfig)
authStr := base64.URLEncoding.EncodeToString(encodedJSON)
if err != nil {
panic(err)
}
reader, err := cli.ImagePull(ctx, "myimage", types.ImagePullOptions{RegistryAuth: authStr})
if err != nil {
panic(err)
}
defer reader.Close()
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: "myimage",
AttachStderr: true,
AttachStdin: true,
Tty: true,
AttachStdout: true,
OpenStdin: true,
}, nil, nil, nil, "")
fmt.Println(resp.ID)
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:
}
// See the output on CLI
config := types.ExecConfig{
AttachStdin: true,
AttachStderr: true,
AttachStdout: true,
Cmd: []string{"bash"},
}
IDResp, err := cli.ContainerExecCreate(ctx, resp.ID, config)
if err != nil {
log.Panic(err)
}
resp2, err := cli.ContainerExecAttach(ctx, IDResp.ID, types.ExecStartCheck{})
if err != nil {
log.Panic(err)
}
text, _ := resp2.Reader.ReadString('\n')
log.Print(text)
io.Copy(os.Stdout, reader) My issue is that it never really I never see the shell nor bash prompt of my container. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Your |
Beta Was this translation helpful? Give feedback.
Your
ExecConfig
is equivalent todocker exec -i
. You would also need to setTty: true
. Bash does not write a prompt to stdout unless it is started attached to a TTY.