3

我正在尝试使用 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,
)

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

4

1 回答 1

6

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

Resources: container.Resources{ Memory:3e+7 }
于 2018-10-18T09:49:07.247 回答