Skip to content

Commit 7cb664f

Browse files
committed
fix(docker): insert --cidfile after run for docker.exe shell commands
On Windows without DOCKER_HOST the docker spawn keeps the shell wrap, and the command string starts with the resolved docker.exe path. The literal "docker run" search missed it, so --cidfile was appended as extra shell argv where docker never saw it and the container could not be tracked for cleanup. Match the first token as docker or docker.exe (bare, path, or quoted) and insert --cidfile right after the run verb.
1 parent c82d515 commit 7cb664f

2 files changed

Lines changed: 141 additions & 8 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package core
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
// A Windows shell wrap names the CLI docker.exe, often as a quoted path
12+
// (Docker Desktop installs under "Program Files"). --cidfile has to land
13+
// immediately after "run". Appending it as extra shell argv leaves docker
14+
// without a cidfile, so the container cannot be tracked for cleanup.
15+
func TestInsertCidfile_DockerExe(t *testing.T) {
16+
c := newIsolatedTestClient()
17+
const cid = `C:\Temp\mcpproxy-cid.txt`
18+
19+
cases := []struct {
20+
name string
21+
cmd string
22+
want string
23+
}{
24+
{
25+
name: "bare docker.exe",
26+
cmd: `docker.exe run --rm -i ghcr.io/some/image python -m srv`,
27+
want: `docker.exe run --cidfile ` + cid + ` --rm -i ghcr.io/some/image python -m srv`,
28+
},
29+
{
30+
name: "quoted docker.exe path",
31+
cmd: `"C:\Program Files\Docker\resources\bin\docker.exe" run --rm -i ghcr.io/some/image`,
32+
want: `"C:\Program Files\Docker\resources\bin\docker.exe" run --cidfile ` + cid + ` --rm -i ghcr.io/some/image`,
33+
},
34+
{
35+
name: "unix docker still inserts after run",
36+
cmd: `docker run --rm -i ghcr.io/some/image`,
37+
want: `docker run --cidfile ` + cid + ` --rm -i ghcr.io/some/image`,
38+
},
39+
}
40+
for _, tc := range cases {
41+
t.Run(tc.name, func(t *testing.T) {
42+
in := []string{"/c", tc.cmd}
43+
out := c.insertCidfileIntoShellDockerCommand(in, cid)
44+
require.Len(t, out, len(in), "cidfile must be inside the command string, not extra shell argv: %v", out)
45+
assert.Equal(t, tc.want, out[len(out)-1])
46+
})
47+
}
48+
49+
// A command that merely mentions docker must not be rewritten.
50+
out := c.insertCidfileIntoShellDockerCommand([]string{"/c", "echo docker run"}, cid)
51+
assert.False(t, strings.Contains(out[len(out)-1], "--cidfile"))
52+
}

‎internal/upstream/core/connection_docker.go‎

Lines changed: 89 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
397478
func (c *Client) extractContainerNameFromArgs(dockerArgs []string) string {
398479
// Look for --name flag in the arguments

0 commit comments

Comments
 (0)