Skip to content

Commit

Permalink
chore: simplify commit method
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Mar 5, 2024
1 parent bbb4382 commit 96b2d16
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
18 changes: 9 additions & 9 deletions modules/ollama/ollama.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ func (c *OllamaContainer) ConnectionString(ctx context.Context) (string, error)
return fmt.Sprintf("http://%s:%d", host, port.Int()), nil
}

// Commit it commits the current file system changes in the container into a new image,
// and returns the image name. The image name is generated by appending a random string
// to the tag of the current image.
// Commit it commits the current file system changes in the container into a new target image.
// The target image name should be unique, as this method will commit the current state
// of the container into a new image with the given name, so it doesn't override existing images.
// It should be used for creating an image that contains a loaded model.
func (c *OllamaContainer) Commit(ctx context.Context, targetImage string) (string, error) {
func (c *OllamaContainer) Commit(ctx context.Context, targetImage string) error {
cli, err := testcontainers.NewDockerClientWithOpts(context.Background())
if err != nil {
return "", err
return err
}

list, err := cli.ImageList(ctx, types.ImageListOptions{Filters: filters.NewArgs(filters.Arg("reference", targetImage))})
if err != nil {
return "", fmt.Errorf("listing images %w", err)
return fmt.Errorf("listing images %w", err)
}

if len(list) > 0 {
return "", fmt.Errorf("image %s already exists", targetImage)
return fmt.Errorf("image %s already exists", targetImage)
}

_, err = cli.ContainerCommit(ctx, c.GetContainerID(), container.CommitOptions{
Expand All @@ -65,10 +65,10 @@ func (c *OllamaContainer) Commit(ctx context.Context, targetImage string) (strin
},
})
if err != nil {
return "", fmt.Errorf("committing container %w", err)
return fmt.Errorf("committing container %w", err)
}

return targetImage, nil
return nil
}

// RunContainer creates an instance of the Ollama container type
Expand Down
8 changes: 2 additions & 6 deletions modules/ollama/ollama_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,15 @@ func TestOllama(t *testing.T) {
// Users can change the way this is generated, but it should be unique.
targetImage := fmt.Sprintf("%s-%s", ollama.DefaultOllamaImage, strings.ToLower(uuid.New().String()[:4]))

newImage, err := container.Commit(context.Background(), targetImage)
err := container.Commit(context.Background(), targetImage)
// }
if err != nil {
t.Fatal(err)
}

if newImage == "" {
t.Fatal("new image should not be empty")
}

newOllamaContainer, err := ollama.RunContainer(
context.Background(),
testcontainers.WithImage(newImage),
testcontainers.WithImage(targetImage),
)
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 96b2d16

Please sign in to comment.