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

rmv*_*aps 3 go docker moby

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

docker run -m 250m --name test repo/tag

在 docker api 中,它位于 Host Config 结构下,但在 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,
)
Run Code Online (Sandbox Code Playgroud)

类型为 container.HostConfig 的结构体中的未知字段“内存”。由于它没有字段名称并且只有类型,因此我不知道如何将资源添加到 Hostconfig。感谢任何帮助 - 我是新手,正在尝试调整我正在使用的开源项目 - redzilla - 由于我的系统资源限制

Ole*_*zov 6

您可以使用ResourcesHostConfig 结构的字段定义内存限制。

Resources: container.Resources{ Memory:3e+7 }
Run Code Online (Sandbox Code Playgroud)