/
attach.go
60 lines (52 loc) · 1.25 KB
/
attach.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
package docker
import (
"net/http/httputil"
"github.com/docker/docker/api/types"
"github.com/pkg/errors"
)
func (c *Container) Attach() error {
client := c.client
ctx := c.options.context
attachOpts := types.ContainerAttachOptions{
Stream: true,
Stdin: client.options.stdin != nil,
Stdout: client.options.stdout != nil,
Stderr: client.options.stderr != nil,
Logs: true,
}
resp, errAttach := client.ContainerAttach(
c.options.context,
c.ID,
attachOpts,
)
if errAttach == nil {
defer resp.Close()
}
if errAttach != nil && errAttach != httputil.ErrPersistEOF {
// ContainerAttach returns an ErrPersistEOF (connection closed)
// means server met an error and put it in Hijacked connection
// keep the error and read detailed error message from hijacked connection later
return errors.Wrap(errAttach, "cannot attach to container")
}
if errAttach != nil {
return errAttach
}
strm := &stream{
stdin: client.options.stdin,
stdout: client.options.stdout,
stderr: client.options.stderr,
}
errHijack := holdHijackedConnection(
ctx,
strm,
c.options.containerConfig.Tty,
client.options.stdin,
client.options.stdout,
client.options.stderr,
resp,
)
if errHijack == nil {
return errAttach
}
return errHijack
}