Skip to content

Commit 95c0fa7

Browse files
authored
Merge pull request #947 from areebahmeddd/fix/sync-docker-creds
sync docker credentials to container
2 parents 0644cc5 + e33c90b commit 95c0fa7

6 files changed

Lines changed: 86 additions & 6 deletions

File tree

cmd/cli/commands/compose.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func newUpCommand() *cobra.Command {
6363
return errors.New("unable to determine standalone runner endpoint")
6464
}
6565

66+
syncDockerConfigForRegistry(cmd.Context(), asPrinter(cmd))
6667
if err := downloadModelsOnlyIfNotFound(desktopClient, models); err != nil {
6768
return err
6869
}

cmd/cli/commands/pull.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func newPullCmd() *cobra.Command {
2222

2323
func pullModel(cmd *cobra.Command, desktopClient *desktop.Client, model string) error {
2424
printer := asPrinter(cmd)
25+
syncDockerConfigForRegistry(cmd.Context(), printer)
2526
response, _, err := desktopClient.Pull(model, printer)
2627

2728
if err != nil {

cmd/cli/commands/push.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func newPushCmd() *cobra.Command {
2121

2222
func pushModel(cmd *cobra.Command, desktopClient *desktop.Client, model string) error {
2323
printer := asPrinter(cmd)
24+
syncDockerConfigForRegistry(cmd.Context(), printer)
2425
response, _, err := desktopClient.Push(model, printer)
2526

2627
if err != nil {

cmd/cli/commands/utils.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package commands
22

33
import (
44
"bytes"
5+
"context"
56
"errors"
67
"fmt"
78
"io"
@@ -10,6 +11,7 @@ import (
1011

1112
"github.com/docker/model-runner/cmd/cli/desktop"
1213
"github.com/docker/model-runner/cmd/cli/pkg/standalone"
14+
"github.com/docker/model-runner/cmd/cli/pkg/types"
1315
"github.com/docker/model-runner/pkg/distribution/distribution"
1416
"github.com/docker/model-runner/pkg/distribution/oci/reference"
1517
"github.com/docker/model-runner/pkg/inference/backends/vllm"
@@ -247,6 +249,40 @@ func addRunnerFlags(cmd *cobra.Command, opts runnerFlagOptions) {
247249
}
248250
}
249251

252+
// syncDockerConfigForRegistry copies the host's Docker config into the running
253+
// container. Only applicable for Moby engine setups; a no-op otherwise.
254+
func syncDockerConfigForRegistry(ctx context.Context, printer standalone.StatusPrinter) {
255+
if modelRunner == nil {
256+
return
257+
}
258+
engineKind := modelRunner.EngineKind()
259+
if engineKind != types.ModelRunnerEngineKindMoby {
260+
return
261+
}
262+
if desktop.IsDesktopWSLContext(ctx, dockerCLI) {
263+
return
264+
}
265+
dockerClient, err := desktop.DockerClientForContext(dockerCLI, dockerCLI.CurrentContext())
266+
if err != nil {
267+
printer.Printf("Warning: failed to create Docker client for credential sync: %v\n", err)
268+
return
269+
}
270+
defer dockerClient.Close()
271+
272+
containerID, _, _, err := standalone.FindControllerContainer(ctx, dockerClient)
273+
if err != nil {
274+
printer.Printf("Warning: failed to find model runner container for credential sync: %v\n", err)
275+
return
276+
}
277+
if containerID == "" {
278+
return
279+
}
280+
281+
if err := standalone.SyncDockerConfigToContainer(ctx, dockerClient, containerID, engineKind); err != nil {
282+
printer.Printf("Warning: failed to sync Docker credentials to runner: %v\n", err)
283+
}
284+
}
285+
250286
// newTable creates a new table with Docker CLI-style formatting:
251287
// no borders, no column separators, no header line, left-aligned, and 2-space padding.
252288
func newTable(w io.Writer) *tablewriter.Table {

cmd/cli/pkg/standalone/containers.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,24 @@ import (
2828
// controllerContainerName is the name to use for the controller container.
2929
const controllerContainerName = "docker-model-runner"
3030

31-
// copyDockerConfigToContainer copies the Docker config file from the host to the container
32-
// and sets up proper ownership and permissions for the modelrunner user.
33-
// It does nothing for Desktop and Cloud engine kinds.
34-
func copyDockerConfigToContainer(ctx context.Context, dockerClient *client.Client, containerID string, engineKind types.ModelRunnerEngineKind) error {
31+
// SyncDockerConfigToContainer copies the host's ~/.docker/config.json into the
32+
// running container. It is a no-op for Desktop and Cloud engine kinds.
33+
func SyncDockerConfigToContainer(ctx context.Context, dockerClient *client.Client, containerID string, engineKind types.ModelRunnerEngineKind) error {
3534
// Do nothing for Desktop and Cloud engine kinds
3635
if engineKind == types.ModelRunnerEngineKindDesktop || engineKind == types.ModelRunnerEngineKindCloud ||
3736
os.Getenv("_MODEL_RUNNER_TREAT_DESKTOP_AS_MOBY") == "1" {
3837
return nil
3938
}
4039

41-
dockerConfigPath := os.ExpandEnv("$HOME/.docker/config.json")
40+
dockerConfigDir := os.Getenv("DOCKER_CONFIG")
41+
if dockerConfigDir == "" {
42+
homeDir, err := os.UserHomeDir()
43+
if err != nil {
44+
return fmt.Errorf("failed to get home directory: %w", err)
45+
}
46+
dockerConfigDir = filepath.Join(homeDir, ".docker")
47+
}
48+
dockerConfigPath := filepath.Join(dockerConfigDir, "config.json")
4249
if s, err := os.Stat(dockerConfigPath); err != nil || s.Mode()&os.ModeType != 0 {
4350
return nil
4451
}
@@ -622,7 +629,7 @@ func CreateControllerContainer(ctx context.Context, dockerClient *client.Client,
622629

623630
// Copy Docker config file if it exists and we're the container creator.
624631
if created && !vllmOnWSL {
625-
if err := copyDockerConfigToContainer(ctx, dockerClient, resp.ID, engineKind); err != nil {
632+
if err := SyncDockerConfigToContainer(ctx, dockerClient, resp.ID, engineKind); err != nil {
626633
// Log warning but continue - don't fail container creation
627634
printer.Printf("Warning: failed to copy Docker config: %v\n", err)
628635
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package standalone
2+
3+
import (
4+
"testing"
5+
6+
"github.com/docker/model-runner/cmd/cli/pkg/types"
7+
)
8+
9+
// TestSyncDockerConfigToContainer_NoopForDesktopAndCloud verifies that
10+
// SyncDockerConfigToContainer skips Desktop and Cloud engine kinds.
11+
func TestSyncDockerConfigToContainer_NoopForDesktopAndCloud(t *testing.T) {
12+
for _, engineKind := range []types.ModelRunnerEngineKind{
13+
types.ModelRunnerEngineKindDesktop,
14+
types.ModelRunnerEngineKindCloud,
15+
} {
16+
t.Run(engineKind.String(), func(t *testing.T) {
17+
err := SyncDockerConfigToContainer(t.Context(), nil, "container-id", engineKind)
18+
if err != nil {
19+
t.Fatalf("SyncDockerConfigToContainer(%v) returned unexpected error: %v", engineKind, err)
20+
}
21+
})
22+
}
23+
}
24+
25+
// TestSyncDockerConfigToContainer_NoopWhenConfigMissing verifies that
26+
// SyncDockerConfigToContainer skips when the host config file is absent.
27+
func TestSyncDockerConfigToContainer_NoopWhenConfigMissing(t *testing.T) {
28+
t.Setenv("DOCKER_CONFIG", t.TempDir())
29+
30+
err := SyncDockerConfigToContainer(t.Context(), nil, "container-id", types.ModelRunnerEngineKindMoby)
31+
if err != nil {
32+
t.Fatalf("SyncDockerConfigToContainer returned unexpected error for missing config: %v", err)
33+
}
34+
}

0 commit comments

Comments
 (0)