MCPcopy
hub / github.com/gliderlabs/ssh / dockerRun

Function dockerRun

_examples/ssh-docker/docker.go:44–119  ·  view source on GitHub ↗
(cfg *container.Config, sess ssh.Session)

Source from the content-addressed store, hash-verified

42}
43
44func dockerRun(cfg *container.Config, sess ssh.Session) (status int64, cleanup func(), err error) {
45 docker, err := client.NewEnvClient()
46 if err != nil {
47 panic(err)
48 }
49 status = 255
50 cleanup = func() {}
51 ctx := context.Background()
52 res, err := docker.ContainerCreate(ctx, cfg, nil, nil, "")
53 if err != nil {
54 return
55 }
56 cleanup = func() {
57 docker.ContainerRemove(ctx, res.ID, types.ContainerRemoveOptions{})
58 }
59 opts := types.ContainerAttachOptions{
60 Stdin: cfg.AttachStdin,
61 Stdout: cfg.AttachStdout,
62 Stderr: cfg.AttachStderr,
63 Stream: true,
64 }
65 stream, err := docker.ContainerAttach(ctx, res.ID, opts)
66 if err != nil {
67 return
68 }
69 cleanup = func() {
70 docker.ContainerRemove(ctx, res.ID, types.ContainerRemoveOptions{})
71 stream.Close()
72 }
73
74 outputErr := make(chan error)
75
76 go func() {
77 var err error
78 if cfg.Tty {
79 _, err = io.Copy(sess, stream.Reader)
80 } else {
81 _, err = stdcopy.StdCopy(sess, sess.Stderr(), stream.Reader)
82 }
83 outputErr <- err
84 }()
85
86 go func() {
87 defer stream.CloseWrite()
88 io.Copy(stream.Conn, sess)
89 }()
90
91 err = docker.ContainerStart(ctx, res.ID, types.ContainerStartOptions{})
92 if err != nil {
93 return
94 }
95 if cfg.Tty {
96 _, winCh, _ := sess.Pty()
97 go func() {
98 for win := range winCh {
99 err := docker.ContainerResize(ctx, res.ID, types.ResizeOptions{
100 Height: uint(win.Height),
101 Width: uint(win.Width),

Callers 1

mainFunction · 0.85

Calls 2

PtyMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…