通过docker-client 调docker API, 根据需求进行调用
package main import ( "context" "fmt" "github.com/docker/docker/api/types" "github.com/docker/docker/client" "github.com/docker/go-connections/nat" "github.com/docker/docker/api/types/container" ) func main() { cli, err := client.NewEnvClient() if err != nil { panic(err) } body,err:=cli.ContainerCreate(context.TODO(),&container.Config{ Tty: true, OpenStdin:true, Image:"nginx:latest", },&container.HostConfig{ PortBindings:nat.PortMap{nat.Port("80/tcp"):[]nat.PortBinding{{"0.0.0.0","10001"}}}, },nil,nil,"testnginx2") if err!=nil{ fmt.Println(err) } fmt.Println(body) containerID:=body.ID err=cli.ContainerStart(context.TODO() ,containerID, types.ContainerStartOptions{}) fmt.Println(err) //containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{}) //if err != nil { // panic(err) //} // // //for _, container := range containers { // fmt.Printf("%s %s ", container.ID[:10], container.Image) //} }