2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 11:27:17 +08:00

feat: 优化docker兼容

This commit is contained in:
耗子
2024-10-31 03:28:41 +08:00
parent 52e8830605
commit f01afacf34
13 changed files with 107 additions and 49 deletions

View File

@@ -62,9 +62,12 @@ func (r *containerRepo) ListAll() ([]types.Container, error) {
Host: port.IP,
})
}
if len(item.Names) == 0 {
item.Names = append(item.Names, "")
}
containers = append(containers, types.Container{
ID: item.ID,
Name: item.Names[0],
Name: strings.TrimPrefix(item.Names[0], "/"), // https://github.com/moby/moby/issues/7519
Image: item.Image,
ImageID: item.ImageID,
Command: item.Command,
@@ -96,7 +99,7 @@ func (r *containerRepo) ListByName(names string) ([]types.Container, error) {
// Create 创建容器
func (r *containerRepo) Create(req *request.ContainerCreate) (string, error) {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("docker run --name %s", req.Name))
sb.WriteString(fmt.Sprintf("docker run -d --name %s", req.Name))
if req.PublishAllPorts {
sb.WriteString(" -P")
} else {
@@ -155,65 +158,65 @@ func (r *containerRepo) Create(req *request.ContainerCreate) (string, error) {
sb.WriteString(fmt.Sprintf(" --memory %d", req.Memory))
}
sb.WriteString(" %s")
return shell.Execf(sb.String(), req.Image)
sb.WriteString(" %s bash")
return shell.ExecfWithTTY(sb.String(), req.Image)
}
// Remove 移除容器
func (r *containerRepo) Remove(id string) error {
_, err := shell.ExecfWithTimeout(30*time.Second, "docker rm -f %s", id)
_, err := shell.ExecfWithTimeout(120*time.Second, "docker rm -f %s", id)
return err
}
// Start 启动容器
func (r *containerRepo) Start(id string) error {
_, err := shell.ExecfWithTimeout(30*time.Second, "docker start %s", id)
_, err := shell.ExecfWithTimeout(120*time.Second, "docker start %s", id)
return err
}
// Stop 停止容器
func (r *containerRepo) Stop(id string) error {
_, err := shell.ExecfWithTimeout(30*time.Second, "docker stop %s", id)
_, err := shell.ExecfWithTimeout(120*time.Second, "docker stop %s", id)
return err
}
// Restart 重启容器
func (r *containerRepo) Restart(id string) error {
_, err := shell.ExecfWithTimeout(30*time.Second, "docker restart %s", id)
_, err := shell.ExecfWithTimeout(120*time.Second, "docker restart %s", id)
return err
}
// Pause 暂停容器
func (r *containerRepo) Pause(id string) error {
_, err := shell.ExecfWithTimeout(30*time.Second, "docker pause %s", id)
_, err := shell.ExecfWithTimeout(120*time.Second, "docker pause %s", id)
return err
}
// Unpause 恢复容器
func (r *containerRepo) Unpause(id string) error {
_, err := shell.ExecfWithTimeout(30*time.Second, "docker unpause %s", id)
_, err := shell.ExecfWithTimeout(120*time.Second, "docker unpause %s", id)
return err
}
// Kill 杀死容器
func (r *containerRepo) Kill(id string) error {
_, err := shell.ExecfWithTimeout(30*time.Second, "docker kill %s", id)
_, err := shell.ExecfWithTimeout(120*time.Second, "docker kill %s", id)
return err
}
// Rename 重命名容器
func (r *containerRepo) Rename(id string, newName string) error {
_, err := shell.ExecfWithTimeout(30*time.Second, "docker rename %s %s", id, newName)
_, err := shell.ExecfWithTimeout(120*time.Second, "docker rename %s %s", id, newName)
return err
}
// Logs 查看容器日志
func (r *containerRepo) Logs(id string) (string, error) {
return shell.ExecfWithTimeout(30*time.Second, "docker logs %s", id)
return shell.ExecfWithTimeout(120*time.Second, "docker logs %s", id)
}
// Prune 清理未使用的容器
func (r *containerRepo) Prune() error {
_, err := shell.ExecfWithTimeout(30*time.Second, "docker container prune -f")
_, err := shell.ExecfWithTimeout(120*time.Second, "docker container prune -f")
return err
}

View File

@@ -80,7 +80,7 @@ func (r *containerImageRepo) Pull(req *request.ContainerImagePull) error {
sb.WriteString(fmt.Sprintf("docker pull %s", req.Name))
if _, err := shell.Execf(sb.String()); err != nil { // nolint: govet
return fmt.Errorf("pull failed: %w", err)
return err
}
return nil
@@ -88,12 +88,12 @@ func (r *containerImageRepo) Pull(req *request.ContainerImagePull) error {
// Remove 删除镜像
func (r *containerImageRepo) Remove(id string) error {
_, err := shell.ExecfWithTimeout(30*time.Second, "docker rmi %s", id)
_, err := shell.ExecfWithTimeout(120*time.Second, "docker rmi %s", id)
return err
}
// Prune 清理未使用的镜像
func (r *containerImageRepo) Prune() error {
_, err := shell.ExecfWithTimeout(30*time.Second, "docker image prune -f")
_, err := shell.ExecfWithTimeout(120*time.Second, "docker image prune -f")
return err
}

View File

@@ -110,17 +110,17 @@ func (r *containerNetworkRepo) Create(req *request.ContainerNetworkCreate) (stri
sb.WriteString(fmt.Sprintf(" --opt %s=%s", option.Key, option.Value))
}
return shell.ExecfWithTimeout(30*time.Second, sb.String()) // nolint: govet
return shell.ExecfWithTimeout(120*time.Second, sb.String()) // nolint: govet
}
// Remove 删除网络
func (r *containerNetworkRepo) Remove(id string) error {
_, err := shell.ExecfWithTimeout(30*time.Second, "docker network rm -f %s", id)
_, err := shell.ExecfWithTimeout(120*time.Second, "docker network rm -f %s", id)
return err
}
// Prune 清理未使用的网络
func (r *containerNetworkRepo) Prune() error {
_, err := shell.ExecfWithTimeout(30*time.Second, "docker network prune -f")
_, err := shell.ExecfWithTimeout(120*time.Second, "docker network prune -f")
return err
}

View File

@@ -84,17 +84,17 @@ func (r *containerVolumeRepo) Create(req *request.ContainerVolumeCreate) (string
sb.WriteString(fmt.Sprintf(" --opt %s=%s", option.Key, option.Value))
}
return shell.ExecfWithTimeout(30*time.Second, sb.String()) // nolint: govet
return shell.ExecfWithTimeout(120*time.Second, sb.String()) // nolint: govet
}
// Remove 删除存储卷
func (r *containerVolumeRepo) Remove(id string) error {
_, err := shell.ExecfWithTimeout(30*time.Second, "docker volume rm -f %s", id)
_, err := shell.ExecfWithTimeout(120*time.Second, "docker volume rm -f %s", id)
return err
}
// Prune 清理未使用的存储卷
func (r *containerVolumeRepo) Prune() error {
_, err := shell.ExecfWithTimeout(30*time.Second, "docker volume prune -f")
_, err := shell.ExecfWithTimeout(120*time.Second, "docker volume prune -f")
return err
}