@@ -8,18 +8,18 @@ import (
88 "fmt"
99 "io"
1010 "net/http"
11+ "net/netip"
1112 "os"
1213 "path/filepath"
1314 "strconv"
1415 "strings"
1516 "time"
1617
17- "github.com/docker/docker/api/types/container"
18- "github.com/docker/docker/api/types/image"
19- "github.com/docker/docker/api/types/mount"
20- "github.com/docker/docker/client"
21- "github.com/docker/go-connections/nat"
2218 gpupkg "github.com/docker/model-runner/cmd/cli/pkg/gpu"
19+ "github.com/moby/moby/api/types/container"
20+ "github.com/moby/moby/api/types/mount"
21+ "github.com/moby/moby/api/types/network"
22+ "github.com/moby/moby/client"
2323 "github.com/spf13/cobra"
2424)
2525
@@ -137,7 +137,7 @@ func pullNIMImage(ctx context.Context, dockerClient *client.Client, model string
137137 }
138138 }
139139
140- pullOptions := image. PullOptions {}
140+ pullOptions := client. ImagePullOptions {}
141141
142142 // Set authentication if available
143143 if authStr != "" {
@@ -186,7 +186,9 @@ func pullNIMImage(ctx context.Context, dockerClient *client.Client, model string
186186 defer reader .Close ()
187187
188188 // Stream pull progress
189- io .Copy (cmd .OutOrStdout (), reader )
189+ //
190+ // TODO(thaJeztah): format output / progress?
191+ _ , _ = io .Copy (cmd .OutOrStdout (), reader )
190192
191193 return nil
192194}
@@ -195,15 +197,16 @@ func pullNIMImage(ctx context.Context, dockerClient *client.Client, model string
195197func findNIMContainer (ctx context.Context , dockerClient * client.Client , model string ) (string , error ) {
196198 containerName := nimContainerName (model )
197199
198- containers , err := dockerClient .ContainerList (ctx , container. ListOptions {
200+ res , err := dockerClient .ContainerList (ctx , client. ContainerListOptions {
199201 All : true ,
200202 })
201203 if err != nil {
202204 return "" , fmt .Errorf ("failed to list containers: %w" , err )
203205 }
204206
205- for _ , c := range containers {
207+ for _ , c := range res . Items {
206208 for _ , name := range c .Names {
209+ // TODO(thaJeztah): replace this with a filter, or use "inspect"
207210 if strings .TrimPrefix (name , "/" ) == containerName {
208211 return c .ID , nil
209212 }
@@ -246,17 +249,18 @@ func createNIMContainer(ctx context.Context, dockerClient *client.Client, model
246249 }
247250
248251 // Container configuration
249- env := []string {}
252+ var env []string
250253 if ngcAPIKey != "" {
251254 env = append (env , "NGC_API_KEY=" + ngcAPIKey )
252255 }
253256
254- portStr := strconv .Itoa (nimDefaultPort )
257+ hostPort , _ := network .PortFrom (nimDefaultPort , network .TCP )
258+
255259 config := & container.Config {
256260 Image : model ,
257261 Env : env ,
258- ExposedPorts : nat .PortSet {
259- nat . Port ( portStr + "/tcp" ) : struct {}{},
262+ ExposedPorts : network .PortSet {
263+ hostPort : struct {}{},
260264 },
261265 }
262266
@@ -269,11 +273,11 @@ func createNIMContainer(ctx context.Context, dockerClient *client.Client, model
269273 Target : "/opt/nim/.cache" ,
270274 },
271275 },
272- PortBindings : nat .PortMap {
273- nat . Port ( portStr + "/tcp" ) : []nat .PortBinding {
276+ PortBindings : network .PortMap {
277+ hostPort : []network .PortBinding {
274278 {
275- HostIP : "127.0.0.1" ,
276- HostPort : portStr ,
279+ HostIP : netip . MustParseAddr ( "127.0.0.1" ) ,
280+ HostPort : strconv . Itoa ( nimDefaultPort ) ,
277281 },
278282 },
279283 },
@@ -291,13 +295,19 @@ func createNIMContainer(ctx context.Context, dockerClient *client.Client, model
291295 }
292296
293297 // Create the container
294- resp , err := dockerClient .ContainerCreate (ctx , config , hostConfig , nil , nil , containerName )
298+ resp , err := dockerClient .ContainerCreate (ctx , client.ContainerCreateOptions {
299+ Config : config ,
300+ HostConfig : hostConfig ,
301+ NetworkingConfig : nil ,
302+ Platform : nil ,
303+ Name : containerName ,
304+ })
295305 if err != nil {
296306 return "" , fmt .Errorf ("failed to create NIM container: %w" , err )
297307 }
298308
299309 // Start the container
300- if err := dockerClient .ContainerStart (ctx , resp .ID , container. StartOptions {}); err != nil {
310+ if _ , err := dockerClient .ContainerStart (ctx , resp .ID , client. ContainerStartOptions {}); err != nil {
301311 return "" , fmt .Errorf ("failed to start NIM container: %w" , err )
302312 }
303313
@@ -315,13 +325,13 @@ func createNIMContainer(ctx context.Context, dockerClient *client.Client, model
315325func waitForNIMReady (ctx context.Context , cmd * cobra.Command ) error {
316326 cmd .Println ("Waiting for NIM to be ready (this may take several minutes)..." )
317327
318- client := & http.Client {
328+ httpClient := & http.Client {
319329 Timeout : 5 * time .Second ,
320330 }
321331
322332 maxRetries := 120 // 10 minutes with 5 second intervals
323333 for i := 0 ; i < maxRetries ; i ++ {
324- resp , err := client .Get (fmt .Sprintf ("http://127.0.0.1:%d/v1/models" , nimDefaultPort ))
334+ resp , err := httpClient .Get (fmt .Sprintf ("http://127.0.0.1:%d/v1/models" , nimDefaultPort ))
325335 if err == nil {
326336 resp .Body .Close ()
327337 if resp .StatusCode == http .StatusOK {
@@ -356,14 +366,14 @@ func runNIMModel(ctx context.Context, dockerClient *client.Client, model string,
356366
357367 if containerID != "" {
358368 // Container exists, check if it's running
359- inspect , err := dockerClient .ContainerInspect (ctx , containerID )
369+ inspect , err := dockerClient .ContainerInspect (ctx , containerID , client. ContainerInspectOptions {} )
360370 if err != nil {
361371 return fmt .Errorf ("failed to inspect NIM container: %w" , err )
362372 }
363373
364- if ! inspect .State .Running {
374+ if ! inspect .Container . State .Running {
365375 // Container exists but is not running, start it
366- if err := dockerClient .ContainerStart (ctx , containerID , container. StartOptions {}); err != nil {
376+ if _ , err := dockerClient .ContainerStart (ctx , containerID , client. ContainerStartOptions {}); err != nil {
367377 return fmt .Errorf ("failed to start existing NIM container: %w" , err )
368378 }
369379 cmd .Printf ("Started existing NIM container %s\n " , nimContainerName (model ))
@@ -397,7 +407,7 @@ func chatWithNIM(cmd *cobra.Command, model, prompt string) error {
397407 // The NIM container runs on localhost:8000 and provides an OpenAI-compatible API
398408
399409 // Create a simple HTTP client to talk to the NIM
400- client := & http.Client {
410+ httpClient := & http.Client {
401411 Timeout : 300 * time .Second ,
402412 }
403413
@@ -422,7 +432,7 @@ func chatWithNIM(cmd *cobra.Command, model, prompt string) error {
422432
423433 req .Header .Set ("Content-Type" , "application/json" )
424434
425- resp , err := client .Do (req )
435+ resp , err := httpClient .Do (req )
426436 if err != nil {
427437 return fmt .Errorf ("failed to send request to NIM: %w" , err )
428438 }
0 commit comments