From 2944ea508ee20ee1b9d4ef8e820e46bbb93ca532 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Mon, 17 Apr 2023 16:08:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=B9=E5=99=A8=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=95=9C=E5=83=8F=E5=AD=98=E5=9C=A8=E5=88=A4?= =?UTF-8?q?=E6=96=AD=20(#674)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/container.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/backend/app/service/container.go b/backend/app/service/container.go index a800bf352..41b2c9a3b 100644 --- a/backend/app/service/container.go +++ b/backend/app/service/container.go @@ -198,8 +198,10 @@ func (u *ContainerService) ContainerCreate(req dto.ContainerCreate) error { global.LOG.Infof("new container info %s has been made, now start to create", req.Name) ctx := context.Background() - if err := pullImages(ctx, client, req.Image); err != nil { - return err + if !checkImageExist(client, req.Image) { + if err := pullImages(ctx, client, req.Image); err != nil { + return err + } } container, err := client.ContainerCreate(ctx, config, hostConf, &network.NetworkingConfig{}, &v1.Platform{}, req.Name) if err != nil { @@ -332,6 +334,23 @@ func calculateNetwork(network map[string]types.NetworkStats) (float64, float64) return rx, tx } +func checkImageExist(client *client.Client, image string) bool { + images, err := client.ImageList(context.Background(), types.ImageListOptions{}) + if err != nil { + fmt.Println(err) + return false + } + + for _, img := range images { + for _, tag := range img.RepoTags { + if tag == image || tag == image+":latest" { + return true + } + } + } + return false +} + func pullImages(ctx context.Context, client *client.Client, image string) error { out, err := client.ImagePull(ctx, image, types.ImagePullOptions{}) if err != nil {