Skip to content

Commit 4d3a019

Browse files
committed
stripDefaultsFromModelName fixed to account for default registries
So we strip defaults fully. Signed-off-by: Eric Curtin <[email protected]>
1 parent 6f0be5b commit 4d3a019

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

cmd/cli/commands/utils.go

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/docker/model-runner/cmd/cli/desktop"
1212
"github.com/docker/model-runner/cmd/cli/pkg/standalone"
1313
"github.com/docker/model-runner/pkg/inference/backends/vllm"
14+
"github.com/google/go-containerregistry/pkg/name"
1415
"github.com/moby/term"
1516
"github.com/spf13/cobra"
1617
)
@@ -26,6 +27,16 @@ const (
2627
enableVLLM = "It looks like you're trying to use a model for vLLM → docker model install-runner --vllm"
2728
)
2829

30+
// getDefaultRegistry returns the default registry, checking for environment override
31+
// If DEFAULT_REGISTRY environment variable is set, it returns that value
32+
// Otherwise, it returns name.DefaultRegistry ("index.docker.io")
33+
func getDefaultRegistry() string {
34+
if defaultReg := os.Getenv("DEFAULT_REGISTRY"); defaultReg != "" {
35+
return defaultReg
36+
}
37+
return name.DefaultRegistry
38+
}
39+
2940
var notRunningErr = fmt.Errorf("Docker Model Runner is not running. Please start it and try again.\n")
3041

3142
func handleClientError(err error, message string) error {
@@ -87,15 +98,47 @@ func asPrinter(cmd *cobra.Command) standalone.StatusPrinter {
8798
return &commandPrinter{cmd: cmd}
8899
}
89100

90-
// stripDefaultsFromModelName removes the default "ai/" prefix and ":latest" tag for display.
101+
// stripDefaultsFromModelName removes the default "ai/" prefix, default registry, and ":latest" tag for display.
91102
// Examples:
92103
// - "ai/gemma3:latest" -> "gemma3"
93-
// - "ai/gemma3:v1" -> "ai/gemma3:v1"
104+
// - "ai/gemma3:v1" -> "gemma3:v1"
94105
// - "myorg/gemma3:latest" -> "myorg/gemma3"
95106
// - "gemma3:latest" -> "gemma3"
107+
// - "index.docker.io/ai/gemma3:latest" -> "gemma3"
108+
// - "docker.io/ai/gemma3:latest" -> "gemma3"
109+
// - "docker.io/myorg/gemma3:latest" -> "myorg/gemma3"
96110
// - "hf.co/bartowski/model:latest" -> "hf.co/bartowski/model"
97111
func stripDefaultsFromModelName(model string) string {
98-
// Check if model has ai/ prefix without tag (implicitly :latest) - strip just ai/
112+
// Get the current default registry (checking for environment override)
113+
defaultRegistry := getDefaultRegistry()
114+
115+
// Handle the common default registries that are aliases for each other
116+
// Always handle "index.docker.io" and "docker.io" as defaults regardless of DEFAULT_REGISTRY env var
117+
// since they are equivalent and commonly used interchangeably
118+
defaultRegistries := []string{"index.docker.io/", "docker.io/"}
119+
if defaultRegistry != "" &&
120+
defaultRegistry != "index.docker.io" &&
121+
defaultRegistry != "docker.io" {
122+
123+
// Ensure it has a trailing slash for correct prefix trimming
124+
if !strings.HasSuffix(defaultRegistry, "/") {
125+
defaultRegistry += "/"
126+
}
127+
// Overwrite the list to contain only the custom registry
128+
defaultRegistries = []string{defaultRegistry}
129+
}
130+
131+
// Check for the common default registries first
132+
for _, reg := range defaultRegistries {
133+
if strings.HasPrefix(model, reg) {
134+
// Remove the registry prefix
135+
model = strings.TrimPrefix(model, reg)
136+
break
137+
}
138+
}
139+
140+
// If model has default org prefix (without tag, or with :latest tag), strip the org
141+
// but preserve other tags
99142
if strings.HasPrefix(model, defaultOrg+"/") {
100143
model = strings.TrimPrefix(model, defaultOrg+"/")
101144
}

cmd/cli/commands/utils_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,31 @@ func TestStripDefaultsFromModelName(t *testing.T) {
137137
input: "",
138138
expected: "",
139139
},
140+
{
141+
name: "docker.io registry with ai prefix and latest tag",
142+
input: "docker.io/ai/gemma3:latest",
143+
expected: "gemma3",
144+
},
145+
{
146+
name: "index.docker.io registry with ai prefix and latest tag",
147+
input: "index.docker.io/ai/gemma3:latest",
148+
expected: "gemma3",
149+
},
150+
{
151+
name: "docker.io registry with ai prefix and custom tag",
152+
input: "docker.io/ai/gemma3:v1",
153+
expected: "gemma3:v1",
154+
},
155+
{
156+
name: "docker.io registry with custom org and latest tag",
157+
input: "docker.io/myorg/gemma3:latest",
158+
expected: "myorg/gemma3",
159+
},
160+
{
161+
name: "index.docker.io registry with custom org and latest tag",
162+
input: "index.docker.io/myorg/gemma3:latest",
163+
expected: "myorg/gemma3",
164+
},
140165
}
141166

142167
for _, tt := range tests {

0 commit comments

Comments
 (0)