Skip to content

Commit

Permalink
fix(CLI): Remove image list in destroy (#1373)
Browse files Browse the repository at this point in the history
* fix: Add image to error

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

* fix: Get the image digest from container

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

* fix: Add 404

Signed-off-by: Ce Gao <cegao@tensorchord.ai>

Signed-off-by: Ce Gao <cegao@tensorchord.ai>
  • Loading branch information
gaocegege authored Jan 3, 2023
1 parent 8534a63 commit a218dbc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
46 changes: 16 additions & 30 deletions pkg/envd/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (e dockerEngine) ListImage(ctx context.Context) ([]types.EnvdImage, error)
for _, img := range images {
envdImg, err := types.NewImageFromSummary(img)
if err != nil {
return nil, errors.Wrap(err, "failed to create envd image from the docker image")
return nil, errors.Wrapf(err, "failed to create envd image `%s` from the docker image", img.ID)
}
envdImgs = append(envdImgs, *envdImg)
}
Expand Down Expand Up @@ -489,6 +489,18 @@ func (e dockerEngine) StartEnvd(ctx context.Context, so StartOptions) (*StartRes

func (e dockerEngine) Destroy(ctx context.Context, name string) (string, error) {
logger := logrus.WithField("container", name)

ctr, err := e.ContainerInspect(ctx, name)
if err != nil {
errCause := errors.UnwrapAll(err).Error()
if strings.Contains(errCause, "No such container") {
// If the container is not found, it is already destroyed or the name is wrong.
logger.Infof("cannot find container %s, maybe it's already destroyed or the name is wrong", name)
return "", nil
}
return "", errors.Wrap(err, "failed to inspect the container")
}

// Refer to https://docs.docker.com/engine/reference/commandline/container_kill/
if err := e.ContainerKill(ctx, name, "KILL"); err != nil {
errCause := errors.UnwrapAll(err).Error()
Expand All @@ -509,40 +521,14 @@ func (e dockerEngine) Destroy(ctx context.Context, name string) (string, error)
return "", errors.Wrap(err, "failed to remove the container")
}

// remove image
tags, err := e.getContainerTag(ctx, name)
if err != nil {
return "", errors.Wrap(err, "failed to get the container tags")
} else {
for _, tag := range tags {
if _, err := e.ImageRemove(ctx, tag, dockertypes.ImageRemoveOptions{}); err != nil {
return "", errors.Errorf("remove image %s failed: %w", tag, err)
}
logrus.Infof("image(%s) is destroyed", tag)
}
if _, err := e.ImageRemove(ctx, ctr.Image, dockertypes.ImageRemoveOptions{}); err != nil {
return "", errors.Errorf("remove image %s failed: %w", ctr.Image, err)
}
logrus.Infof("image(%s) is destroyed", ctr.Image)

return name, nil
}

func (e dockerEngine) getContainerTag(ctx context.Context, name string) ([]string, error) {
tags := []string{}
// check the images instead of running containers because `envd build` also produce images
images, err := e.ListImage(ctx)
if err != nil {
return tags, err
}
for _, img := range images {
if strings.HasPrefix(img.Name, fmt.Sprintf("%s:", name)) {
tags = append(tags, img.Name)
}
}
if len(tags) == 0 {
logrus.Infof("cannot find the image of %s", name)
}
return tags, nil
}

func (e dockerEngine) WaitUntilRunning(ctx context.Context,
name string, timeout time.Duration) error {
logger := logrus.WithField("container", name)
Expand Down
2 changes: 1 addition & 1 deletion pkg/types/envd.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func NewImageFromSummary(image types.ImageSummary) (*EnvdImage, error) {
}
m, err := newManifest(image.Labels)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to parse manifest")
}
img.EnvdManifest = m
return &img, nil
Expand Down

0 comments on commit a218dbc

Please sign in to comment.