diff --git a/backend/app/service/docker.go b/backend/app/service/docker.go index 300926ac7efc..003fce1266c7 100644 --- a/backend/app/service/docker.go +++ b/backend/app/service/docker.go @@ -4,6 +4,7 @@ import ( "bufio" "context" "encoding/json" + "fmt" "os" "path" "strings" @@ -213,6 +214,9 @@ func (u *DockerService) UpdateConf(req dto.SettingUpdate) error { if err := os.WriteFile(constant.DaemonJsonPath, newJson, 0640); err != nil { return err } + if err := validateDockerConfig(); err != nil { + return err + } stdout, err := cmd.Exec("systemctl restart docker") if err != nil { @@ -260,6 +264,10 @@ func (u *DockerService) UpdateLogOption(req dto.LogOption) error { return err } + if err := validateDockerConfig(); err != nil { + return err + } + stdout, err := cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) @@ -300,6 +308,10 @@ func (u *DockerService) UpdateIpv6Option(req dto.Ipv6Option) error { return err } + if err := validateDockerConfig(); err != nil { + return err + } + stdout, err := cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) @@ -329,6 +341,10 @@ func (u *DockerService) UpdateConfByFile(req dto.DaemonJsonUpdateByFile) error { _, _ = write.WriteString(req.File) write.Flush() + if err := validateDockerConfig(); err != nil { + return err + } + stdout, err := cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) @@ -348,6 +364,13 @@ func (u *DockerService) OperateDocker(req dto.DockerOperation) error { } } } + + if req.Operation == "restart" { + if err := validateDockerConfig(); err != nil { + return err + } + } + stdout, err := cmd.Execf("systemctl %s %s ", req.Operation, service) if err != nil { return errors.New(string(stdout)) @@ -403,3 +426,11 @@ func changeLogOption(daemonMap map[string]interface{}, logMaxFile, logMaxSize st } } } + +func validateDockerConfig() error { + stdout, err := cmd.Exec("dockerd --validate") + if err != nil || (stdout != "" && strings.TrimSpace(stdout) != "configuration OK") { + return fmt.Errorf("Docker configuration validation failed, err: %v", stdout) + } + return nil +} diff --git a/backend/app/service/image_repo.go b/backend/app/service/image_repo.go index e5dad9e90398..6954ea5c1e6a 100644 --- a/backend/app/service/image_repo.go +++ b/backend/app/service/image_repo.go @@ -86,6 +86,10 @@ func (u *ImageRepoService) Create(req dto.ImageRepoCreate) error { } if req.Protocol == "http" { _ = u.handleRegistries(req.DownloadUrl, "", "create") + if err := validateDockerConfig(); err != nil { + return err + } + stdout, err := cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) @@ -159,6 +163,10 @@ func (u *ImageRepoService) Update(req dto.ImageRepoUpdate) error { if repo.Auth { _, _ = cmd.ExecWithCheck("docker", "logout", repo.DownloadUrl) } + if err := validateDockerConfig(); err != nil { + return err + } + stdout, err := cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout))