rmvpaps
rmvpaps

Reputation: 147

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

Upvotes: 3

Views: 1936

Answers (1)

Oleg Butuzov
Oleg Butuzov

Reputation: 5395

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

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

Upvotes: 7

Related Questions