diff --git a/pkg/api/handlers/compat/containers_create.go b/pkg/api/handlers/compat/containers_create.go index 4855b56b66a..b6ad88104c1 100644 --- a/pkg/api/handlers/compat/containers_create.go +++ b/pkg/api/handlers/compat/containers_create.go @@ -604,14 +604,13 @@ func cliOpts(cc handlers.CreateContainerConfig, rtc *config.Config) (*entities.C cliOpts.OOMKillDisable = *cc.HostConfig.OomKillDisable } if cc.Config.Healthcheck != nil { - finCmd := "" - for _, str := range cc.Config.Healthcheck.Test { - finCmd = finCmd + str + " " - } - if len(finCmd) > 1 { - finCmd = finCmd[:len(finCmd)-1] + // Encode healthcheck test as JSON to preserve arguments with spaces. + // MakeHealthCheckFromCli will unmarshal this back to the original array. + cmdJSON, err := json.Marshal(cc.Config.Healthcheck.Test) + if err != nil { + return nil, nil, err } - cliOpts.HealthCmd = finCmd + cliOpts.HealthCmd = string(cmdJSON) if cc.Config.Healthcheck.Interval > 0 { cliOpts.HealthInterval = cc.Config.Healthcheck.Interval.String() } diff --git a/pkg/specgenutil/specgen.go b/pkg/specgenutil/specgen.go index 6ab83ce6d31..17004ac65a7 100644 --- a/pkg/specgenutil/specgen.go +++ b/pkg/specgenutil/specgen.go @@ -986,7 +986,10 @@ func MakeHealthCheckFromCli(inCmd, interval string, retries uint, timeout, start var concat string if strings.ToUpper(cmdArr[0]) == define.HealthConfigTestCmd || strings.ToUpper(cmdArr[0]) == define.HealthConfigTestNone { // this is for compat, we are already split properly for most compat cases - cmdArr = strings.Fields(inCmd) + // Only re-split if the input was not already a JSON array (isArr == false); otherwise preserve the unmarshaled array structure + if !isArr { + cmdArr = strings.Fields(inCmd) + } } else if strings.ToUpper(cmdArr[0]) != define.HealthConfigTestCmdShell { // this is for podman side of things, won't contain the keywords if isArr && len(cmdArr) > 1 { // an array of consecutive commands cmdArr = append([]string{define.HealthConfigTestCmd}, cmdArr...) diff --git a/test/apiv2/20-containers.at b/test/apiv2/20-containers.at index 654fa3b2cd7..fbd8543653f 100644 --- a/test/apiv2/20-containers.at +++ b/test/apiv2/20-containers.at @@ -593,6 +593,29 @@ t GET containers/$cid/json 200 \ .Config.Healthcheck.Timeout=30000000000 \ .Config.Healthcheck.Retries=3 +t DELETE containers/$cid?v=true 204 + +# Test Compat Create with healthcheck preserving arguments with spaces (issue #26519) +cat >$TMPD/healthcheck-spaces.json <