diff --git a/backend/app/service/docker.go b/backend/app/service/docker.go index 300926ac7..5ddcd118d 100644 --- a/backend/app/service/docker.go +++ b/backend/app/service/docker.go @@ -213,8 +213,12 @@ func (u *DockerService) UpdateConf(req dto.SettingUpdate) error { if err := os.WriteFile(constant.DaemonJsonPath, newJson, 0640); err != nil { return err } + stdout, err := cmd.Exec("dockerd --validate") + if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { + return errors.New("Docker configuration validation failed: " + string(stdout)) + } - stdout, err := cmd.Exec("systemctl restart docker") + stdout, err = cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) } @@ -260,7 +264,12 @@ func (u *DockerService) UpdateLogOption(req dto.LogOption) error { return err } - stdout, err := cmd.Exec("systemctl restart docker") + stdout, err := cmd.Exec("dockerd --validate") + if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { + return errors.New("Docker configuration validation failed: " + string(stdout)) + } + + stdout, err = cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) } @@ -300,7 +309,12 @@ func (u *DockerService) UpdateIpv6Option(req dto.Ipv6Option) error { return err } - stdout, err := cmd.Exec("systemctl restart docker") + stdout, err := cmd.Exec("dockerd --validate") + if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { + return errors.New("Docker configuration validation failed: " + string(stdout)) + } + + stdout, err = cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) } @@ -329,7 +343,12 @@ func (u *DockerService) UpdateConfByFile(req dto.DaemonJsonUpdateByFile) error { _, _ = write.WriteString(req.File) write.Flush() - stdout, err := cmd.Exec("systemctl restart docker") + stdout, err := cmd.Exec("dockerd --validate") + if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { + return errors.New("Docker configuration validation failed: " + string(stdout)) + } + + stdout, err = cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) } @@ -348,6 +367,14 @@ func (u *DockerService) OperateDocker(req dto.DockerOperation) error { } } } + + if req.Operation == "restart" { + stdout, err := cmd.Exec("dockerd --validate") + if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { + return errors.New("Docker configuration validation failed: " + string(stdout)) + } + } + stdout, err := cmd.Execf("systemctl %s %s ", req.Operation, service) if err != nil { return errors.New(string(stdout)) diff --git a/backend/app/service/firewall.go b/backend/app/service/firewall.go index bb4556f9c..b0dd875c1 100644 --- a/backend/app/service/firewall.go +++ b/backend/app/service/firewall.go @@ -175,6 +175,12 @@ func (u *FirewallService) SearchWithPage(req dto.RuleSearch) (int64, interface{} } func (u *FirewallService) OperateFirewall(operation string) error { + + stdout, err := cmd.Exec("dockerd --validate") + if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { + return errors.New("Docker configuration validation failed: " + string(stdout)) + } + client, err := firewall.NewFirewallClient() if err != nil { return err diff --git a/backend/app/service/image_repo.go b/backend/app/service/image_repo.go index e5dad9e90..46cffe60e 100644 --- a/backend/app/service/image_repo.go +++ b/backend/app/service/image_repo.go @@ -86,7 +86,12 @@ func (u *ImageRepoService) Create(req dto.ImageRepoCreate) error { } if req.Protocol == "http" { _ = u.handleRegistries(req.DownloadUrl, "", "create") - stdout, err := cmd.Exec("systemctl restart docker") + stdout, err := cmd.Exec("dockerd --validate") + if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { + return errors.New("Docker configuration validation failed: " + string(stdout)) + } + + stdout, err = cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) } @@ -159,7 +164,12 @@ func (u *ImageRepoService) Update(req dto.ImageRepoUpdate) error { if repo.Auth { _, _ = cmd.ExecWithCheck("docker", "logout", repo.DownloadUrl) } - stdout, err := cmd.Exec("systemctl restart docker") + stdout, err := cmd.Exec("dockerd --validate") + if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { + return errors.New("Docker configuration validation failed: " + string(stdout)) + } + + stdout, err = cmd.Exec("systemctl restart docker") if err != nil { return errors.New(string(stdout)) } diff --git a/backend/app/service/snapshot_recover.go b/backend/app/service/snapshot_recover.go index 6b707340e..8c949c219 100644 --- a/backend/app/service/snapshot_recover.go +++ b/backend/app/service/snapshot_recover.go @@ -146,6 +146,12 @@ func (u *SnapshotService) HandleSnapshotRecover(snap model.Snapshot, isRecover b global.LOG.Debugf("remove the file %s after the operation is successful", path.Dir(snapFileDir)) _ = os.RemoveAll(path.Dir(snapFileDir)) } + stdout, err := cmd.Exec("dockerd --validate") + if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { + global.LOG.Errorf("Docker configuration validation failed: " + string(stdout)) + return + } + _, _ = cmd.Exec("systemctl daemon-reload && systemctl restart 1panel.service") } @@ -231,6 +237,11 @@ func recoverDaemonJson(src string, fileOp files.FileOp) error { } } + stdout, err := cmd.Exec("dockerd --validate") + if err != nil || (string(stdout) != "" && strings.TrimSpace(stdout) != "configuration OK") { + return errors.New("Docker configuration validation failed: " + string(stdout)) + } + _, _ = cmd.Exec("systemctl restart docker") return nil }