@@ -367,13 +367,12 @@ func (c *Client) insertCidfileIntoShellDockerCommand(shellArgs []string, cidFile
367367 // Get the Docker command string (last argument)
368368 dockerCmd := shellArgs [len (shellArgs )- 1 ]
369369
370- // Insert --cidfile into the Docker command string
371- // Look for "docker run" and insert --cidfile right after
372- if strings .Contains (dockerCmd , "docker run" ) {
373- // Replace "docker run" with "docker run --cidfile /path/to/file"
374- dockerCmdWithCid := strings .Replace (dockerCmd , "docker run" , fmt .Sprintf ("docker run --cidfile %s" , cidFile ), 1 )
375-
376- // Create new args with the modified command
370+ // Insert --cidfile immediately after the run verb. The executable may be the
371+ // bare name "docker", "docker.exe" (Windows), or a quoted path to either.
372+ // A literal search for "docker run" misses docker.exe, and the fallback
373+ // below then appends --cidfile as extra shell argv, where docker never
374+ // sees it (it lands after the image and is passed into the container).
375+ if dockerCmdWithCid , ok := insertCidfileAfterDockerRun (dockerCmd , cidFile ); ok {
377376 newArgs := make ([]string , len (shellArgs ))
378377 copy (newArgs , shellArgs )
379378 newArgs [len (newArgs )- 1 ] = dockerCmdWithCid
@@ -386,13 +385,95 @@ func (c *Client) insertCidfileIntoShellDockerCommand(shellArgs []string, cidFile
386385 return newArgs
387386 }
388387
389- // If we can't find " docker run" , fall back to appending
388+ // If we can't find a docker run verb , fall back to appending
390389 c .logger .Warn ("Could not find 'docker run' in shell command for cidfile insertion" ,
391390 zap .String ("server" , c .config .Name ),
392391 zap .String ("docker_cmd" , logSafeCommand (dockerCmd )))
393392 return append (shellArgs , "--cidfile" , cidFile )
394393}
395394
395+ // insertCidfileAfterDockerRun returns the command with "--cidfile <file>"
396+ // placed immediately after the run verb of a docker or docker.exe invocation.
397+ // The executable may be bare or a quoted path. ok is false when the command
398+ // does not start with one.
399+ func insertCidfileAfterDockerRun (dockerCmd , cidFile string ) (string , bool ) {
400+ end , ok := dockerRunVerbEnd (dockerCmd )
401+ if ! ok {
402+ return "" , false
403+ }
404+ return dockerCmd [:end ] + " --cidfile " + cidFile + dockerCmd [end :], true
405+ }
406+
407+ // dockerRunVerbEnd reports the index just past the "run" verb when cmd's first
408+ // token is a docker or docker.exe executable (bare, a path, or quoted).
409+ func dockerRunVerbEnd (cmd string ) (int , bool ) {
410+ i := skipShellSpace (cmd , 0 )
411+ tok , next , ok := readShellToken (cmd , i )
412+ if ! ok || ! isDockerExecutableToken (tok ) {
413+ return 0 , false
414+ }
415+ i = skipShellSpace (cmd , next )
416+ const verb = "run"
417+ if i + len (verb ) > len (cmd ) || cmd [i :i + len (verb )] != verb {
418+ return 0 , false
419+ }
420+ end := i + len (verb )
421+ if end < len (cmd ) && cmd [end ] != ' ' && cmd [end ] != '\t' {
422+ return 0 , false
423+ }
424+ return end , true
425+ }
426+
427+ func skipShellSpace (s string , i int ) int {
428+ for i < len (s ) && (s [i ] == ' ' || s [i ] == '\t' ) {
429+ i ++
430+ }
431+ return i
432+ }
433+
434+ // readShellToken reads one shell word starting at i. A leading quote consumes
435+ // until the matching closer; the returned token is the word without quotes.
436+ func readShellToken (s string , i int ) (string , int , bool ) {
437+ if i >= len (s ) {
438+ return "" , i , false
439+ }
440+ if s [i ] == '"' || s [i ] == '\'' {
441+ q := s [i ]
442+ j := i + 1
443+ for j < len (s ) && s [j ] != q {
444+ j ++
445+ }
446+ if j >= len (s ) {
447+ return "" , i , false
448+ }
449+ return s [i + 1 : j ], j + 1 , true
450+ }
451+ j := i
452+ for j < len (s ) && s [j ] != ' ' && s [j ] != '\t' {
453+ j ++
454+ }
455+ if j == i {
456+ return "" , i , false
457+ }
458+ return s [i :j ], j , true
459+ }
460+
461+ // isDockerExecutableToken reports whether tok names the docker CLI, including
462+ // a path and the Windows docker.exe form. Comparison is case-insensitive
463+ // because Windows executable lookup is.
464+ func isDockerExecutableToken (tok string ) bool {
465+ base := tok
466+ if k := strings .LastIndexAny (base , `/\` ); k >= 0 {
467+ base = base [k + 1 :]
468+ }
469+ switch strings .ToLower (base ) {
470+ case "docker" , "docker.exe" :
471+ return true
472+ default :
473+ return false
474+ }
475+ }
476+
396477// extractContainerNameFromArgs extracts the container name from Docker run arguments
397478func (c * Client ) extractContainerNameFromArgs (dockerArgs []string ) string {
398479 // Look for --name flag in the arguments
0 commit comments