@@ -8,8 +8,11 @@ import (
88 "log/slog"
99 "os"
1010 "os/exec"
11+ "path/filepath"
1112
1213 "github.com/docker/cli/cli"
14+ "github.com/spf13/cobra"
15+ "github.com/spf13/pflag"
1316
1417 "github.com/docker/docker-agent/pkg/config"
1518 "github.com/docker/docker-agent/pkg/environment"
@@ -20,7 +23,7 @@ import (
2023// runInSandbox delegates the current command to a Docker sandbox.
2124// It ensures a sandbox exists (creating or recreating as needed), then
2225// executes docker agent inside it via `docker sandbox exec`.
23- func runInSandbox (ctx context.Context , args []string , runConfig * config.RuntimeConfig , template string ) error {
26+ func runInSandbox (ctx context.Context , cmd * cobra. Command , args []string , runConfig * config.RuntimeConfig , template string ) error {
2427 if environment .InSandbox () {
2528 return fmt .Errorf ("already running inside a Docker sandbox (VM %s)" , os .Getenv ("SANDBOX_VM_ID" ))
2629 }
@@ -34,19 +37,21 @@ func runInSandbox(ctx context.Context, args []string, runConfig *config.RuntimeC
3437 agentRef = args [0 ]
3538 }
3639
37- dockerAgentArgs := sandbox .BuildCagentArgs (os .Args )
3840 configDir := paths .GetConfigDir ()
39-
40- // Always forward config directory paths so the sandbox-side
41- // docker agent resolves it to the same host directories
42- // (which is mounted read-write by ensureSandbox).
43- dockerAgentArgs = sandbox .AppendFlagIfMissing (dockerAgentArgs , "--config-dir" , configDir )
41+ dockerAgentArgs := dockerAgentArgs (cmd , args , configDir )
42+ dockerAgentArgs = append (dockerAgentArgs , args ... )
43+ dockerAgentArgs = append (dockerAgentArgs , "--config-dir" , configDir )
4444
4545 stopTokenWriter := sandbox .StartTokenWriterIfNeeded (ctx , configDir , runConfig .ModelsGateway )
4646 defer stopTokenWriter ()
4747
48- // Ensure a sandbox with the right workspace mounts exists.
49- wd := cmp .Or (runConfig .WorkingDir , "." )
48+ // Resolve wd to an absolute path so that it matches the absolute
49+ // workspace paths returned by `docker sandbox ls --json`.
50+ wd , err := filepath .Abs (cmp .Or (runConfig .WorkingDir , "." ))
51+ if err != nil {
52+ return fmt .Errorf ("resolving workspace path: %w" , err )
53+ }
54+
5055 name , err := sandbox .Ensure (ctx , wd , sandbox .ExtraWorkspace (wd , agentRef ), template , configDir )
5156 if err != nil {
5257 return err
@@ -63,7 +68,7 @@ func runInSandbox(ctx context.Context, args []string, runConfig *config.RuntimeC
6368 envFlags = append (envFlags , "-e" , envModelsGateway + "=" + gateway )
6469 }
6570
66- dockerCmd := sandbox .BuildExecCmd (ctx , name , dockerAgentArgs , envFlags , envVars )
71+ dockerCmd := sandbox .BuildExecCmd (ctx , name , wd , dockerAgentArgs , envFlags , envVars )
6772 slog .Debug ("Executing in sandbox" , "name" , name , "args" , dockerCmd .Args )
6873
6974 if err := dockerCmd .Run (); err != nil {
@@ -75,3 +80,31 @@ func runInSandbox(ctx context.Context, args []string, runConfig *config.RuntimeC
7580
7681 return nil
7782}
83+
84+ func dockerAgentArgs (cmd * cobra.Command , args []string , configDir string ) []string {
85+ var dockerAgentArgs []string
86+ hasYolo := false
87+ cmd .Flags ().Visit (func (f * pflag.Flag ) {
88+ if f .Name == "sandbox" || f .Name == "config-dir" {
89+ return
90+ }
91+
92+ if f .Name == "yolo" {
93+ hasYolo = true
94+ }
95+
96+ if f .Value .Type () == "bool" {
97+ dockerAgentArgs = append (dockerAgentArgs , "--" + f .Name )
98+ } else {
99+ dockerAgentArgs = append (dockerAgentArgs , "--" + f .Name , f .Value .String ())
100+ }
101+ })
102+ if ! hasYolo {
103+ dockerAgentArgs = append (dockerAgentArgs , "--yolo" )
104+ }
105+
106+ dockerAgentArgs = append (dockerAgentArgs , args ... )
107+ dockerAgentArgs = append (dockerAgentArgs , "--config-dir" , configDir )
108+
109+ return dockerAgentArgs
110+ }
0 commit comments