You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.1 KiB
51 lines
1.1 KiB
package docker
|
|
|
|
import (
|
|
"context"
|
|
"gowebsocket/common"
|
|
"gowebsocket/controllers"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/api/types/container"
|
|
"github.com/docker/docker/client"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Image struct {
|
|
Name string `form:"name" binding:"required"`
|
|
}
|
|
|
|
func Run(c *gin.Context) {
|
|
var image Image
|
|
err := c.Bind(&image)
|
|
if err != nil {
|
|
c.JSON(common.ParameterIllegal, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
q := c.Request.URL.Query()
|
|
// 获取镜像名称
|
|
imageName := q.Get("name")
|
|
|
|
ctx := context.Background()
|
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
|
if err != nil {
|
|
c.JSON(common.ServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
resp, err := cli.ContainerCreate(ctx, &container.Config{
|
|
Image: imageName,
|
|
}, nil, nil, nil, "")
|
|
if err != nil {
|
|
c.JSON(common.ServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
|
|
panic(err)
|
|
}
|
|
data := make(map[string]interface{})
|
|
data["image_id"] = resp.ID
|
|
|
|
controllers.Response(c, common.OK, "", data)
|
|
}
|