代码之家  ›  专栏  ›  技术社区  ›  rmvpaps

如何在docker go客户端中创建内存受限的容器

  •  1
  • rmvpaps  · 技术社区  · 6 年前

    我正在尝试使用docker go客户端创建一个内存有限的容器- https://godoc.org/github.com/docker/docker/client#Client.ContainerCreate 但是,我不知道在函数中添加这些参数的位置。

    docker run-m 250m——名称测试回购/标签

    在docker api中,它位于主机配置结构下,但在go doc中,我看到了在HostConfig中使用的资源下的选项- https://godoc.org/github.com/docker/docker/api/types/container#HostConfig

    像这样打电话

    import(
    ....
    "github.com/docker/docker/api/types"
    "github.com/docker/docker/api/types/container"
    "github.com/docker/docker/api/types/events"
    "github.com/docker/docker/api/types/filters"
    "github.com/docker/docker/client"
    "github.com/docker/go-connections/nat"
    )
    
    ...
    
    resp, err1 := cli.ContainerCreate(ctx,
        &container.Config{
                User:         strconv.Itoa(os.Getuid()), // avoid permission issues
                Image:        cfg.ImageName,
                AttachStdin:  false,
                AttachStdout: true,
                AttachStderr: true,
                Tty:          true,
                ExposedPorts: exposedPorts,
                Labels:       labels,
                Env:          envVars,
        },
        &container.HostConfig{
                Binds:       binds,
                NetworkMode: container.NetworkMode(cfg.Network),
                PortBindings: nat.PortMap{
                        "1880": []nat.PortBinding{
                                nat.PortBinding{
                                        HostIP:   "",
                                        HostPort: "1880",
                                },
                        }},
                AutoRemove: true,
                Memory : 262144000, //this does not work
        },
        nil, // &network.NetworkingConfig{},
        name,
    )
    

    容器类型的struct literal中的未知字段“Memory”。HostConfig。由于它没有字段名和唯一的类型,我不知道如何向Hostconfig添加资源。非常感谢您的帮助——我是go的新手,由于系统资源的限制,我正在尝试调整我使用的一个开源项目——redzilla

    1 回复  |  直到 6 年前
        1
  •  6
  •   Oleg Butuzov    6 年前

    您可以使用 Resources HostConfig结构的字段。

    Resources: container.Resources{ Memory:3e+7 }