Skip to content

Commit 28cc369

Browse files
committed
fix(docker): recognise docker.exe and backslash paths as a direct docker run
The user-supplied docker run check matched only "docker" or a path ending in "/docker". A Windows config naming docker.exe, or the CLI by its backslash path, fell through to the plain shell wrap: config env was never injected as -e flags (the container did not see it), no cidfile was written, and the server was not treated as docker for container cleanup. Match the base name in either separator style, case-insensitively, with or without .exe.
1 parent c82d515 commit 28cc369

4 files changed

Lines changed: 52 additions & 4 deletions

File tree

‎internal/upstream/core/connection_docker.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,21 @@ func (c *Client) insertCidfileIntoShellDockerCommand(shellArgs []string, cidFile
393393
return append(shellArgs, "--cidfile", cidFile)
394394
}
395395

396+
// isDockerRunCommand reports whether a configured command is a user-supplied
397+
// `docker run`. The command may be bare or a path in either separator style,
398+
// and may carry the Windows .exe suffix (case-insensitive, as Windows lookup is).
399+
func isDockerRunCommand(command string, args []string) bool {
400+
if len(args) == 0 || args[0] != cmdRun {
401+
return false
402+
}
403+
base := command
404+
if i := strings.LastIndexAny(base, `/\`); i >= 0 {
405+
base = base[i+1:]
406+
}
407+
base = strings.ToLower(base)
408+
return base == cmdDocker || base == cmdDocker+".exe"
409+
}
410+
396411
// extractContainerNameFromArgs extracts the container name from Docker run arguments
397412
func (c *Client) extractContainerNameFromArgs(dockerArgs []string) string {
398413
// Look for --name flag in the arguments

‎internal/upstream/core/connection_launcher.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (c *Client) connectWithLauncher(ctx context.Context) error {
5858
// (matches connectStdio's behaviour at internal/upstream/core/
5959
// connection_stdio.go:97). Otherwise concurrent reconnects could
6060
// race to create overlapping containers for the same upstream.
61-
willUseDocker := (c.config.Command == cmdDocker || strings.HasSuffix(c.config.Command, "/"+cmdDocker)) && len(c.config.Args) > 0 && c.config.Args[0] == cmdRun
61+
willUseDocker := isDockerRunCommand(c.config.Command, c.config.Args)
6262
if !willUseDocker && c.isolationManager != nil {
6363
willUseDocker = c.isolationManager.ShouldIsolate(c.config)
6464
}
@@ -292,7 +292,7 @@ func (c *Client) buildLauncherCmd(_ context.Context, willUseDocker bool) (*exec.
292292
finalArgs = c.insertCidfileIntoDockerArgs(finalArgs, cidFile)
293293
}
294294
}
295-
} else if isDirectDockerRun := (c.config.Command == cmdDocker || strings.HasSuffix(c.config.Command, "/"+cmdDocker)) && len(args) > 0 && args[0] == cmdRun; isDirectDockerRun {
295+
} else if isDirectDockerRun := isDockerRunCommand(c.config.Command, args); isDirectDockerRun {
296296
// USER-SUPPLIED `docker run …` upstream launched via the launcher path
297297
// (config.Command IS `docker`). Reuse the SAME resolve→spawn decision as the
298298
// isolation path and the stdio path (resolveDockerSpawn) so this entrypoint

‎internal/upstream/core/connection_stdio.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (c *Client) connectStdio(ctx context.Context) error {
9797
var cidFile string
9898

9999
// Check if this will be a Docker command (either explicit or through isolation)
100-
willUseDocker := (c.config.Command == cmdDocker || strings.HasSuffix(c.config.Command, "/"+cmdDocker)) && len(args) > 0 && args[0] == cmdRun
100+
willUseDocker := isDockerRunCommand(c.config.Command, args)
101101
if !willUseDocker && c.isolationManager != nil {
102102
willUseDocker = c.isolationManager.ShouldIsolate(c.config)
103103
}
@@ -172,7 +172,7 @@ func (c *Client) connectStdio(ctx context.Context) error {
172172
finalArgs = c.insertCidfileIntoDockerArgs(finalArgs, cidFile)
173173
}
174174
}
175-
} else if isDirectDockerRun := (c.config.Command == cmdDocker || strings.HasSuffix(c.config.Command, "/"+cmdDocker)) && len(args) > 0 && args[0] == cmdRun; isDirectDockerRun {
175+
} else if isDirectDockerRun := isDockerRunCommand(c.config.Command, args); isDirectDockerRun {
176176
// USER-SUPPLIED `docker run …` upstream (config.Command IS `docker`).
177177
// Inject env vars as -e flags, then reuse the SAME resolve→spawn decision
178178
// as the isolation path (resolveDockerSpawn) so this path also direct-execs
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package core
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
// A Windows config names the CLI docker.exe, often by its full backslash path.
10+
// Missing it routes the server through the plain shell wrap, so config env is
11+
// never injected as -e flags and the container is not tracked for cleanup.
12+
func TestIsDockerRunCommand(t *testing.T) {
13+
run := []string{"run", "--rm", "-i", "ghcr.io/some/image"}
14+
cases := []struct {
15+
command string
16+
args []string
17+
want bool
18+
}{
19+
{"docker", run, true},
20+
{"/usr/local/bin/docker", run, true},
21+
{"docker.exe", run, true},
22+
{"DOCKER.EXE", run, true},
23+
{`C:\Program Files\Docker\Docker\resources\bin\docker.exe`, run, true},
24+
{`C:\Program Files\Docker\Docker\resources\bin\docker`, run, true},
25+
{"docker", []string{"ps"}, false},
26+
{"docker", nil, false},
27+
{"/usr/bin/docker-compose", run, false},
28+
{"dockerize", run, false},
29+
}
30+
for _, tc := range cases {
31+
assert.Equal(t, tc.want, isDockerRunCommand(tc.command, tc.args), "%q %v", tc.command, tc.args)
32+
}
33+
}

0 commit comments

Comments
 (0)