diff --git a/pkg/api/handlers/compat/exec.go b/pkg/api/handlers/compat/exec.go index a3b26e362a..7ede6a846c 100644 --- a/pkg/api/handlers/compat/exec.go +++ b/pkg/api/handlers/compat/exec.go @@ -166,6 +166,19 @@ func ExecStartHandler(w http.ResponseWriter, r *http.Request) { return } + // If no streams were requested for attach, detach is implied + if !bodyParams.Detach { + execSession, err := sessionCtr.ExecSession(sessionID) + if err != nil { + utils.InternalServerError(w, err) + return + } + + if !execSession.Config.AttachStderr && !execSession.Config.AttachStdout && !execSession.Config.AttachStdin { + bodyParams.Detach = true + } + } + if bodyParams.Detach { // If we are detaching, we do NOT want to hijack. // Instead, we perform a detached start, and return 200 if diff --git a/test/apiv2/20-containers.at b/test/apiv2/20-containers.at index daedb4e02f..00d1d3d38d 100644 --- a/test/apiv2/20-containers.at +++ b/test/apiv2/20-containers.at @@ -916,6 +916,27 @@ t POST libpod/containers/$cid/update \ t DELETE containers/$cid 204 +# Verify that exec session with no attach streams specified can be started +CTRNAME=exectest +podman run -d --name $CTRNAME $IMAGE top +echo '{ "Cmd":["sh", "-c", "echo hello > /tmp/testfile"]}' >${TMPD}/exec_empty.json +t POST "containers/$CTRNAME/exec" ${TMPD}/exec_empty.json 201 .Id~[0-9a-f]\\{64\\} +eid=$(jq -r '.Id' <<<"$output") +t POST exec/$eid/start 200 +# Need to wait for the exec to actually run, given it's detached +TRIES=5 +while [[ $TRIES -gt 0 ]]; do + podman exec $CTRNAME ls /tmp/testfile + if [[ "$output" = "/tmp/testfile" ]]; then + break + fi + sleep 1 + TRIES=$((TRIES - 1)) +done +podman exec $CTRNAME cat /tmp/testfile +is "$output" "hello" +podman rm -f $CTRNAME + rm -rf $TMPD podman container rm -fa