普通网友 2018-10-18 09:33
浏览 92

如何在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 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

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. Since it does not have a field name and only type I have no idea how to add resources to 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

  • 写回答

1条回答 默认 最新

  • 普通网友 2018-10-18 09:49
    关注

    You can define memory limit using Resources field of HostConfig struct.

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

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式