Skip to content

Commit cee4ff1

Browse files
committed
Merge pull request moby#20647 from coolljt0725/fix_20638
Fix exec start api with detach and AttachStdin at same time. fixes #2
2 parents 0f01b21 + fb0ac1a commit cee4ff1

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

daemon/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (d *Daemon) ContainerExecStart(name string, stdin io.ReadCloser, stdout io.
157157
logrus.Debugf("starting exec command %s in container %s", ec.ID, c.ID)
158158
d.LogContainerEvent(c, "exec_start: "+ec.ProcessConfig.Entrypoint+" "+strings.Join(ec.ProcessConfig.Arguments, " "))
159159

160-
if ec.OpenStdin {
160+
if ec.OpenStdin && stdin != nil {
161161
r, w := io.Pipe()
162162
go func() {
163163
defer w.Close()

integration-cli/docker_api_exec_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,36 @@ func (s *DockerSuite) TestExecApiStartMultipleTimesError(c *check.C) {
122122
startExec(c, execID, http.StatusConflict)
123123
}
124124

125+
// #20638
126+
func (s *DockerSuite) TestExecApiStartWithDetach(c *check.C) {
127+
name := "foo"
128+
dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "top")
129+
data := map[string]interface{}{
130+
"cmd": []string{"true"},
131+
"AttachStdin": true,
132+
}
133+
_, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), data)
134+
c.Assert(err, checker.IsNil, check.Commentf(string(b)))
135+
136+
createResp := struct {
137+
ID string `json:"Id"`
138+
}{}
139+
c.Assert(json.Unmarshal(b, &createResp), checker.IsNil, check.Commentf(string(b)))
140+
141+
_, body, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", createResp.ID), strings.NewReader(`{"Detach": true}`), "application/json")
142+
c.Assert(err, checker.IsNil)
143+
144+
b, err = readBody(body)
145+
comment := check.Commentf("response body: %s", b)
146+
c.Assert(err, checker.IsNil, comment)
147+
148+
resp, _, err := sockRequestRaw("GET", "/_ping", nil, "")
149+
c.Assert(err, checker.IsNil)
150+
if resp.StatusCode != http.StatusOK {
151+
c.Fatal("daemon is down, it should alive")
152+
}
153+
}
154+
125155
func createExec(c *check.C, name string) string {
126156
_, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}})
127157
c.Assert(err, checker.IsNil, check.Commentf(string(b)))

0 commit comments

Comments
 (0)