Skip to content

Commit d433cf8

Browse files
committed
Allow running exec with no attach streams requested
This is a small Docker compat fix with the API exec endpoints. With Docker, sending a bare Exec Create (command only, nothing else set) and then an Exec Start without Detach set performs a detached exec. With Podman, we threw an error that at least one stream must be attached to. Fix is trivial; look up the session before start, check the config for attach streams, and force detach on if none are set. Signed-off-by: Matthew Heon <matthew.heon@pm.me>
1 parent f0af130 commit d433cf8

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

pkg/api/handlers/compat/exec.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@ func ExecStartHandler(w http.ResponseWriter, r *http.Request) {
166166
return
167167
}
168168

169+
// If no streams were requested for attach, detach is implied
170+
if !bodyParams.Detach {
171+
execSession, err := sessionCtr.ExecSession(sessionID)
172+
if err != nil {
173+
utils.InternalServerError(w, err)
174+
return
175+
}
176+
177+
if !execSession.Config.AttachStderr && !execSession.Config.AttachStdout && !execSession.Config.AttachStdin {
178+
bodyParams.Detach = true
179+
}
180+
}
181+
169182
if bodyParams.Detach {
170183
// If we are detaching, we do NOT want to hijack.
171184
// Instead, we perform a detached start, and return 200 if

test/apiv2/20-containers.at

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,27 @@ t POST libpod/containers/$cid/update \
916916

917917
t DELETE containers/$cid 204
918918

919+
# Verify that exec session with no attach streams specified can be started
920+
CTRNAME=exectest
921+
podman run -d --name $CTRNAME $IMAGE top
922+
echo '{ "Cmd":["sh", "-c", "echo hello > /tmp/testfile"]}' >${TMPD}/exec_empty.json
923+
t POST "containers/$CTRNAME/exec" ${TMPD}/exec_empty.json 201 .Id~[0-9a-f]\\{64\\}
924+
eid=$(jq -r '.Id' <<<"$output")
925+
t POST exec/$eid/start 200
926+
# Need to wait for the exec to actually run, given it's detached
927+
TRIES=5
928+
while [[ $TRIES -gt 0 ]]; do
929+
podman exec $CTRNAME ls /tmp/testfile
930+
if [[ "$output" = "/tmp/testfile" ]]; then
931+
break
932+
fi
933+
sleep 1
934+
TRIES=$((TRIES - 1))
935+
done
936+
podman exec $CTRNAME cat /tmp/testfile
937+
is "$output" "hello"
938+
podman rm -f $CTRNAME
939+
919940
rm -rf $TMPD
920941

921942
podman container rm -fa

0 commit comments

Comments
 (0)