func RunContainer(opt DockerConfig) (string, error) {
    ctx := context.Background()
    cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
    if err != nil {
        return "", err
    }
    defer cli.Close()

    resp, err := cli.ContainerCreate(ctx, &container.Config{
        Image: opt.Image,
        Cmd: opt.Cmd,
        ExposedPorts: nat.PortSet{ nat.Port(opt.Port[0]) : struct{}{} },
        Tty: true,
        Env: opt.Env,
    },
    &container.HostConfig{
        NetworkMode: container.NetworkMode(opt.Network),
        PortBindings: nat.PortMap{ nat.Port(opt.Port[0]) :
            []nat.PortBinding{ nat.PortBinding{ HostPort: opt.Port[1] } }, },
        Resources: container.Resources{
            Memory: opt.Memory,
            NanoCPUs: int64(opt.Cpu*(math.Pow(10, 9))),
        },
    },
    nil, opt.Name)
    if err != nil {
        return "", err
    }

    err = cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
    if err != nil {
        return "", err
    }

    return resp.ID, nil
}
// create and run container - interactive and detached set
// image (already pulled) should be imagename:version
// default/empty cmd is /bin/bash
create and run container - interactive and detached set
image (already pulled) should be imagename:version
default/empty cmd is /bin/bash
// create and run container - interactive and detached set // image ( already pulled ) should be imagename : version // default / empty cmd is / bin / bash
func RunContainer(opt DockerConfig) (string, error) {
    ctx := context.Background()
    cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
    if err != nil {
        return "", err
    }
    defer cli.Close()
    resp, err := cli.ContainerCreate(ctx, &container.Config{
        Image: opt.Image,
        Cmd: opt.Cmd,
        ExposedPorts: nat.PortSet{ nat.Port(opt.Port[0]) : struct{}{} },
        Tty: true,
        Env: opt.Env,
    },
    &container.HostConfig{
        NetworkMode: container.NetworkMode(opt.Network),
        PortBindings: nat.PortMap{ nat.Port(opt.Port[0]) :
            []nat.PortBinding{ nat.PortBinding{ HostPort: opt.Port[1] } }, },
        Resources: container.Resources{
            Memory: opt.Memory,
            NanoCPUs: int64(opt.Cpu*(math.Pow(10, 9))),
        },
    },
    nil, opt.Name)
    if err != nil {
        return "", err
    }
    err = cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{})
    if err != nil {
        return "", err
    }
    return resp.ID, nil
}
func RunContainer ( opt DockerConfig ) ( string , error ) { ctx := context . Background ( ) cli , err := client . NewClientWithOpts ( client . FromEnv , client . WithAPIVersionNegotiation ( ) ) if err != nil { return " " , err } defer cli . Close ( ) resp , err := cli . ContainerCreate ( ctx , & container . Config { Image : opt . Image , Cmd : opt . Cmd , ExposedPorts : nat . PortSet { nat . Port ( opt . Port [ 0 ] ) : struct { } { } } , Tty : true , Env : opt . Env , } , & container . HostConfig { NetworkMode : container . NetworkMode ( opt . Network ) , PortBindings : nat . PortMap { nat . Port ( opt . Port [ 0 ] ) : [ ] nat . PortBinding { nat . PortBinding { HostPort : opt . Port [ 1 ] } } , } , Resources : container . Resources { Memory : opt . Memory , NanoCPUs : int64 ( opt . Cpu * ( math . Pow ( 10 , 9 ) ) ) , } , } , nil , opt . Name ) if err != nil { return " " , err } err = cli . ContainerStart ( ctx , resp . ID , types . ContainerStartOptions { } ) if err != nil { return " " , err } return resp . ID , nil }
create and run container - interactive and detached set
image (already pulled) should be imagename:version
default/empty cmd is /bin/bash
create and run container - interactive and detached set image ( already pulled ) should be imagename : version default / empty cmd is / bin / bash