mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 10:17:17 +08:00
feat: 优化docker兼容
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user