-
Notifications
You must be signed in to change notification settings - Fork 573
feat: support memory/cpu driver options for docker-container #2048
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support memory/cpu driver options for docker-container #2048
Conversation
closes #2049 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea and I think these options could be helpful for those using the docker-container
driver. I'd like to use a different method for parsing the options though to avoid pulling in new dependencies and to make it more consistent with the other docker products that have the same functionality.
driver/docker-container/driver.go
Outdated
memory string | ||
memorySwap string | ||
cpuQuota int64 | ||
cpuPeriod int64 | ||
cpuShares int64 | ||
cpusetCpus string | ||
cpusetMems string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might be better if we utilize the equivalent types from the docker create
command: https://github.com/docker/cli/blob/0e70f1b7b831565336006298b9443b015c3c87a5/cli/command/container/opts.go#L34-L132
This plugin also has docker/cli
as a dependency so we should be able to utilize the github.com/docker/cli/opts
package which I think we should use instead so the options here are consistent with the equivalent options.
An alternative is to utilize the method that compose does to parse the options, but it's a bit more adhoc. The underlying implementation seems to be the same, but we may not want to reuse that section of code mostly because it is not easily reusable outside of a YAML context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, let's avoid adding unnecessary dep. opts.MemBytes
can be used similar to
Line 74 in e018f8b
shmSize dockeropts.MemBytes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jsternberg @crazy-max done, the types have been aligned to github.com/docker/cli/opts
, and the unnecessary deps have been removed
driver/docker-container/driver.go
Outdated
if d.memory != "" { | ||
var memory uint64 | ||
memory, err := humanize.ParseBytes(d.memory) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
hc.Resources.Memory = int64(memory) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Matching with my recommendation above, I think we can do this:
if d.memory != "" {
var memory opts.MemBytes
if err := memory.Set(d.memory); err != nil {
return err
}
hc.Resources.Memory = int64(memory)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
go.mod
Outdated
@@ -16,6 +16,7 @@ require ( | |||
github.com/docker/cli-docs-tool v0.6.0 | |||
github.com/docker/docker v24.0.5+incompatible | |||
github.com/docker/go-units v0.5.0 | |||
github.com/dustin/go-humanize v1.0.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm hesitant to add the dependencies that are added here since the functionality included in this PR is stuff that's also been done in other docker code. My preference is going to be that we utilize those methods rather than add a new way to parse the units.
The same applies to the other two dependencies here.
If there's a compelling reason why this is brand new functionality for docker and these dependencies make implementing that functionality easier, these dependencies seem solid. My preference is just going to be on not adding dependencies if there's an existing alternative.
driver/docker-container/factory.go
Outdated
case k == "memory-swap": | ||
d.memorySwap = v | ||
case k == "cpu-period": | ||
d.cpuPeriod = int64(cast.ToInt(v)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think strconv.ParseInt
makes more sense here and then we don't need a dependency. The container create
command uses the cli flags and that uses strconv.ParseInt
internally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jsternberg done
f2874f7
to
8143bf4
Compare
@tonistiigi tagging you since this touches user-facing configuration options so. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs docs update in https://github.com/docker/buildx/blob/master/docs/reference/buildx_create.md#docker-container-driver-1
And on docs website as well in follow-up https://docs.docker.com/build/drivers/docker-container/#synopsis (cc @dvdksn)
added @crazy-max @dvdksn |
313a1ca
to
7c79b78
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(docs) LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you squash your commits please? Otherwise LGTM
7c79b78
to
9941e3d
Compare
Signed-off-by: Zero <tobewhatwewant@outlook.com>
9941e3d
to
cfcd1d9
Compare
@crazy-max something wrong ? |
fixes #2049
limit builder container memory
use:
docker buildx create --driver docker-container --driver-opt memory=32G --name zmicro