diff --git a/backend/app/repo/app_install.go b/backend/app/repo/app_install.go index 85ecd2e23..1655b812b 100644 --- a/backend/app/repo/app_install.go +++ b/backend/app/repo/app_install.go @@ -22,6 +22,7 @@ type IAppInstallRepo interface { WithContainerName(containerName string) DBOption WithPort(port int) DBOption WithIdNotInWebsite() DBOption + WithIDNotIs(id uint) DBOption ListBy(opts ...DBOption) ([]model.AppInstall, error) GetFirst(opts ...DBOption) (model.AppInstall, error) Create(ctx context.Context, install *model.AppInstall) error @@ -56,6 +57,12 @@ func (a *AppInstallRepo) WithAppId(appId uint) DBOption { } } +func (a *AppInstallRepo) WithIDNotIs(id uint) DBOption { + return func(g *gorm.DB) *gorm.DB { + return g.Where("id != ?", id) + } +} + func (a *AppInstallRepo) WithAppIdsIn(appIds []uint) DBOption { return func(g *gorm.DB) *gorm.DB { return g.Where("app_id in (?)", appIds) diff --git a/backend/app/service/app.go b/backend/app/service/app.go index 9f5d3dffb..db6da647d 100644 --- a/backend/app/service/app.go +++ b/backend/app/service/app.go @@ -296,6 +296,15 @@ func (a AppService) Install(ctx context.Context, req request.AppInstallCreate) ( err = buserr.New(constant.ErrContainerName) return } + containerExist := false + containerExist, err = checkContainerNameIsExist(req.ContainerName, appInstall.GetPath()) + if err != nil { + return + } + if containerExist { + err = buserr.New(constant.ErrContainerName) + return + } } req.Params[constant.ContainerName] = containerName appInstall.ContainerName = containerName diff --git a/backend/app/service/app_install.go b/backend/app/service/app_install.go index fb2fcd1a4..c617e5e28 100644 --- a/backend/app/service/app_install.go +++ b/backend/app/service/app_install.go @@ -267,7 +267,7 @@ func (a *AppInstallService) Update(req request.AppInstalledUpdate) error { return err } } - if err := addDockerComposeCommonParam(composeMap, installed.ServiceName, req.AppContainerConfig, req.Params); err != nil { + if err = addDockerComposeCommonParam(composeMap, installed.ServiceName, req.AppContainerConfig, req.Params); err != nil { return err } composeByte, err := yaml.Marshal(composeMap) @@ -279,7 +279,20 @@ func (a *AppInstallService) Update(req request.AppInstalledUpdate) error { req.Params[constant.ContainerName] = installed.ContainerName } else { req.Params[constant.ContainerName] = req.ContainerName - installed.ContainerName = req.ContainerName + if installed.ContainerName != req.ContainerName { + exist, _ := appInstallRepo.GetFirst(appInstallRepo.WithContainerName(req.ContainerName), appInstallRepo.WithIDNotIs(installed.ID)) + if exist.ID > 0 { + return buserr.New(constant.ErrContainerName) + } + containerExist, err := checkContainerNameIsExist(req.ContainerName, installed.GetPath()) + if err != nil { + return err + } + if containerExist { + return buserr.New(constant.ErrContainerName) + } + installed.ContainerName = req.ContainerName + } } } diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go index 774364482..8231ff50a 100644 --- a/backend/app/service/app_utils.go +++ b/backend/app/service/app_utils.go @@ -35,6 +35,7 @@ import ( "github.com/1Panel-dev/1Panel/backend/utils/compose" composeV2 "github.com/1Panel-dev/1Panel/backend/utils/docker" "github.com/1Panel-dev/1Panel/backend/utils/files" + dockerTypes "github.com/docker/docker/api/types" "github.com/pkg/errors" ) @@ -542,6 +543,31 @@ func getServiceFromInstall(appInstall *model.AppInstall) (service *composeV2.Com return } +func checkContainerNameIsExist(containerName, appDir string) (bool, error) { + client, err := composeV2.NewDockerClient() + if err != nil { + return false, err + } + var options dockerTypes.ContainerListOptions + list, err := client.ContainerList(context.Background(), options) + if err != nil { + return false, err + } + for _, container := range list { + if containerName == container.Names[0][1:] { + if workDir, ok := container.Labels[composeWorkdirLabel]; ok { + if workDir != appDir { + return true, nil + } + } else { + return true, nil + } + } + + } + return false, nil +} + func upApp(appInstall *model.AppInstall) { upProject := func(appInstall *model.AppInstall) (err error) { if err == nil {