-
Notifications
You must be signed in to change notification settings - Fork 0
/
attach_container.go
70 lines (53 loc) · 1.55 KB
/
attach_container.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package docker
import (
"context"
"fmt"
"io"
"log"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
)
func ShellConnect(containerID string) error {
dockerClient, err := DockerClient()
if err != nil {
return fmt.Errorf("error creating docker client: %w", err)
}
inspectedContainer, err := InspectContainer(containerID)
if err != nil {
return fmt.Errorf("error inspecting container: %w", err)
}
out, err := dockerClient.ContainerAttach(context.Background(), containerID, container.AttachOptions{
Stream: true,
Stdin: true,
Stdout: true,
Stderr: true,
Logs: true,
})
if err != nil {
return fmt.Errorf("error attaching to container: %w", err)
}
defer out.Close()
exec, err := dockerClient.ContainerExecCreate(context.Background(), inspectedContainer.ID, types.ExecConfig{
Cmd: []string{"sh", "-c", "TERM=xterm-256color; export TERM; exec /bin/sh -i"},
AttachStdin: true,
AttachStdout: true,
AttachStderr: true,
Tty: true,
})
if err != nil {
return fmt.Errorf("error creating exec in container: %w", err)
}
execObj, err := dockerClient.ContainerExecAttach(context.Background(), exec.ID, types.ExecStartCheck{})
if err != nil {
return fmt.Errorf("error attaching to exec in container: %w", err)
}
defer execObj.Close()
// this basically makes a connection between terminal and the shell session
go io.Copy(execObj.Conn, os.Stdin)
_, err = io.Copy(os.Stdout, execObj.Reader)
if err != nil {
log.Printf("error copying output: %v", err)
}
return nil
}