fix(docker-exec-via-sock): 增强命令执行错误处理逻辑

This commit is contained in:
严浩
2025-09-23 11:11:16 +08:00
parent 05cfe833da
commit 020859ff16

View File

@@ -84,14 +84,16 @@ docker_exec_start() {
docker_exec_start_id=$1 docker_exec_start_id=$1
docker_exec_start_desc=$2 docker_exec_start_desc=$2
docker_exec_start_endpoint="${docker_api_base}/exec/${docker_exec_start_id}/start" docker_exec_start_endpoint="${docker_api_base}/exec/${docker_exec_start_id}/start"
DOCKER_LAST_RESPONSE=$(curl --fail --show-error --silent --unix-socket "$DOCKER_SOCKET" \ if ! DOCKER_LAST_RESPONSE=$(curl --fail --show-error --silent --unix-socket "$DOCKER_SOCKET" \
-X POST \ -X POST \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-d '{"Detach": false, "Tty": true}' \ -d '{"Detach": false, "Tty": true}' \
"$docker_exec_start_endpoint") "$docker_exec_start_endpoint"); then
log "command execution failed. output from container: $DOCKER_LAST_RESPONSE"
return 1
fi
log_stream "$docker_exec_start_desc output" "$DOCKER_LAST_RESPONSE" log_stream "$docker_exec_start_desc output" "$DOCKER_LAST_RESPONSE"
printf '%s' "$DOCKER_LAST_RESPONSE" printf '%s' "$DOCKER_LAST_RESPONSE"
} }
@@ -189,13 +191,14 @@ fi
log "starting exec $exec_id" log "starting exec $exec_id"
start_output=$(docker_exec_start "$exec_id" "exec") || true if ! start_output=$(docker_exec_start "$exec_id" "exec"); then
# The error is already logged inside docker_exec_start
exit 1
fi
if ! exit_code=$(docker_exec_exit_code "$exec_id"); then if ! exit_code=$(docker_exec_exit_code "$exec_id"); then
error_message=$(printf '%s' "$DOCKER_LAST_RESPONSE" | jq -r '.message // "unknown error"') error_message=$(printf '%s' "$DOCKER_LAST_RESPONSE" | jq -r '.message // "unknown error"')
log "could not determine exec exit code: $error_message" log "could not determine exec exit code: $error_message"
log "this often happens if the command inside the container fails immediately (e.g. command not found)."
log "initial command output from container: $start_output"
exit 1 exit 1
fi fi