简体   繁体   English

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

[英]how to create container with memory limit in docker go client

I am trying to create a container with memory limit using the docker go client - https://godoc.org/github.com/docker/docker/client#Client.ContainerCreate However I cannot figure out where to add these parameters in the function. 我正在尝试使用docker go client创建一个具有内存限制的容器 - https://godoc.org/github.com/docker/docker/client#Client.ContainerCreate但是我无法弄清楚在函数中添加这些参数的位置。

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

In the docker api, it comes under Host Config structure but in go doc I saw the option under resources which is used in HostConfig - https://godoc.org/github.com/docker/docker/api/types/container#HostConfig 在docker api中,它位于Host Config结构下,但是在go doc中我看到了HostConfig中使用的资源选项 - https://godoc.org/github.com/docker/docker/api/types/container#HostConfig

Calling like this 这样打电话

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,
)

unknown field 'Memory' in struct literal of type container.HostConfig. struct.HostConfig类型的struct literal中的未知字段“Memory”。 Since it does not have a field name and only type I have no idea how to add resources to Hostconfig. 由于它没有字段名称而且只有类型我不知道如何向Hostconfig添加资源。 Any help is appreciated - I am a newbie at go and am trying to tweak an opensource project I was using - redzilla - due to my system's resource constraints 任何帮助表示赞赏 - 我是一个新手,我正在尝试调整我正在使用的开源项目 - redzilla - 由于我的系统资源限制

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM