Skip to content

Commit f01d574

Browse files
committed
Add docker model start and stop commands
To start and stop the docker model runner container. Signed-off-by: Eric Curtin <eric.curtin@docker.com>
1 parent cb15122 commit f01d574

File tree

8 files changed

+187
-0
lines changed

8 files changed

+187
-0
lines changed

cmd/cli/commands/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ func NewRootCmd(cli *command.DockerCli) *cobra.Command {
106106
newTagCmd(),
107107
newInstallRunner(),
108108
newUninstallRunner(),
109+
newStartRunner(),
110+
newStopRunner(),
109111
newConfigureCmd(),
110112
newPSCmd(),
111113
newDFCmd(),

cmd/cli/commands/start-runner.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"github.com/docker/model-runner/cmd/cli/pkg/types"
6+
7+
"github.com/docker/model-runner/cmd/cli/commands/completion"
8+
"github.com/docker/model-runner/cmd/cli/desktop"
9+
"github.com/docker/model-runner/cmd/cli/pkg/standalone"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
func newStartRunner() *cobra.Command {
14+
c := &cobra.Command{
15+
Use: "start",
16+
Short: "Start Docker Model Runner container",
17+
RunE: func(cmd *cobra.Command, args []string) error {
18+
// Ensure that we're running in a supported model runner context.
19+
if kind := modelRunner.EngineKind(); kind == types.ModelRunnerEngineKindDesktop {
20+
cmd.Println("Standalone start not supported with Docker Desktop")
21+
cmd.Println("Use `docker desktop enable model-runner` instead")
22+
return nil
23+
} else if kind == types.ModelRunnerEngineKindMobyManual {
24+
cmd.Println("Standalone start not supported with MODEL_RUNNER_HOST set")
25+
return nil
26+
}
27+
28+
// Create a Docker client for the active context.
29+
dockerClient, err := desktop.DockerClientForContext(dockerCLI, dockerCLI.CurrentContext())
30+
if err != nil {
31+
return fmt.Errorf("failed to create Docker client: %w", err)
32+
}
33+
34+
// Start the model runner container.
35+
if err := standalone.StartControllerContainer(cmd.Context(), dockerClient, cmd); err != nil {
36+
return fmt.Errorf("unable to start model runner container: %w", err)
37+
}
38+
39+
return nil
40+
},
41+
ValidArgsFunction: completion.NoComplete,
42+
}
43+
return c
44+
}

cmd/cli/commands/stop-runner.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"github.com/docker/model-runner/cmd/cli/pkg/types"
6+
7+
"github.com/docker/model-runner/cmd/cli/commands/completion"
8+
"github.com/docker/model-runner/cmd/cli/desktop"
9+
"github.com/docker/model-runner/cmd/cli/pkg/standalone"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
func newStopRunner() *cobra.Command {
14+
c := &cobra.Command{
15+
Use: "stop",
16+
Short: "Stop Docker Model Runner container",
17+
RunE: func(cmd *cobra.Command, args []string) error {
18+
// Ensure that we're running in a supported model runner context.
19+
if kind := modelRunner.EngineKind(); kind == types.ModelRunnerEngineKindDesktop {
20+
cmd.Println("Standalone stop not supported with Docker Desktop")
21+
cmd.Println("Use `docker desktop disable model-runner` instead")
22+
return nil
23+
} else if kind == types.ModelRunnerEngineKindMobyManual {
24+
cmd.Println("Standalone stop not supported with MODEL_RUNNER_HOST set")
25+
return nil
26+
}
27+
28+
// Create a Docker client for the active context.
29+
dockerClient, err := desktop.DockerClientForContext(dockerCLI, dockerCLI.CurrentContext())
30+
if err != nil {
31+
return fmt.Errorf("failed to create Docker client: %w", err)
32+
}
33+
34+
// Stop the model runner container.
35+
if err := standalone.StopControllerContainer(cmd.Context(), dockerClient, cmd); err != nil {
36+
return fmt.Errorf("unable to stop model runner container: %w", err)
37+
}
38+
39+
return nil
40+
},
41+
ValidArgsFunction: completion.NoComplete,
42+
}
43+
return c
44+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
command: docker model start
2+
short: Start Docker Model Runner container
3+
long: |
4+
Start the Docker Model Runner container. This command starts a previously stopped container without pulling images or recreating it.
5+
usage: docker model start
6+
pname: docker model
7+
plink: docker_model.yaml
8+
deprecated: false
9+
hidden: false
10+
experimental: false
11+
experimentalcli: false
12+
kubernetes: false
13+
swarm: false
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
command: docker model stop
2+
short: Stop Docker Model Runner container
3+
long: |
4+
Stop the Docker Model Runner container. This command stops the running container without removing it.
5+
usage: docker model stop
6+
pname: docker model
7+
plink: docker_model.yaml
8+
deprecated: false
9+
hidden: false
10+
experimental: false
11+
experimentalcli: false
12+
kubernetes: false
13+
swarm: false
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# docker model start
2+
3+
<!---MARKER_GEN_START-->
4+
Start Docker Model Runner container
5+
6+
<!---MARKER_GEN_END-->
7+
8+
## Description
9+
10+
Start the Docker Model Runner container. This command starts a previously stopped container without pulling images or recreating it.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# docker model stop
2+
3+
<!---MARKER_GEN_START-->
4+
Stop Docker Model Runner container
5+
6+
<!---MARKER_GEN_END-->
7+
8+
## Description
9+
10+
Stop the Docker Model Runner container. This command stops the running container without removing it.

cmd/cli/pkg/standalone/containers.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,57 @@ func CreateControllerContainer(ctx context.Context, dockerClient *client.Client,
347347
return nil
348348
}
349349

350+
// StartControllerContainer starts the model runner controller container.
351+
func StartControllerContainer(ctx context.Context, dockerClient client.ContainerAPIClient, printer StatusPrinter) error {
352+
// Find the controller container.
353+
containerID, containerName, _, err := FindControllerContainer(ctx, dockerClient)
354+
if err != nil {
355+
return fmt.Errorf("unable to find model runner container: %w", err)
356+
}
357+
if containerID == "" {
358+
return errors.New("model runner container not found")
359+
}
360+
361+
// Start the container.
362+
if containerName != "" {
363+
printer.Printf("Starting model runner container %s (%s)...\n", containerName, containerID[:12])
364+
} else {
365+
printer.Printf("Starting model runner container %s...\n", containerID[:12])
366+
}
367+
if err := ensureContainerStarted(ctx, dockerClient, containerID); err != nil {
368+
return fmt.Errorf("failed to start container: %w", err)
369+
}
370+
371+
printer.Printf("Model runner container started successfully\n")
372+
return nil
373+
}
374+
375+
// StopControllerContainer stops the model runner controller container.
376+
func StopControllerContainer(ctx context.Context, dockerClient client.ContainerAPIClient, printer StatusPrinter) error {
377+
// Find the controller container.
378+
containerID, containerName, _, err := FindControllerContainer(ctx, dockerClient)
379+
if err != nil {
380+
return fmt.Errorf("unable to find model runner container: %w", err)
381+
}
382+
if containerID == "" {
383+
return errors.New("model runner container not found")
384+
}
385+
386+
// Stop the container.
387+
if containerName != "" {
388+
printer.Printf("Stopping model runner container %s (%s)...\n", containerName, containerID[:12])
389+
} else {
390+
printer.Printf("Stopping model runner container %s...\n", containerID[:12])
391+
}
392+
stopTimeout := 10 // seconds
393+
if err := dockerClient.ContainerStop(ctx, containerID, container.StopOptions{Timeout: &stopTimeout}); err != nil {
394+
return fmt.Errorf("failed to stop container: %w", err)
395+
}
396+
397+
printer.Printf("Model runner container stopped successfully\n")
398+
return nil
399+
}
400+
350401
// PruneControllerContainers stops and removes any model runner controller
351402
// containers.
352403
func PruneControllerContainers(ctx context.Context, dockerClient client.ContainerAPIClient, skipRunning bool, printer StatusPrinter) error {

0 commit comments

Comments
 (0)