diff --git a/builtin/docker/platform.go b/builtin/docker/platform.go index 567a4854251..317d1c8b940 100644 --- a/builtin/docker/platform.go +++ b/builtin/docker/platform.go @@ -4,6 +4,7 @@ import ( "context" "os" "path/filepath" + "strconv" "strings" "fmt" @@ -15,6 +16,7 @@ import ( "github.com/docker/docker/client" "github.com/docker/docker/pkg/jsonmessage" "github.com/docker/go-connections/nat" + goUnits "github.com/docker/go-units" "github.com/hashicorp/go-hclog" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -174,9 +176,25 @@ func (p *Platform) Deploy( containerBinds = append(containerBinds, p.config.Binds...) } + var resources container.Resources + if p.config.Resources != nil { + memory, err := goUnits.FromHumanSize(p.config.Resources["memory"]) + if err != nil { + return nil, err + } + resources.Memory = memory + + cpu, err := strconv.ParseInt(p.config.Resources["cpu"], 10, 64) + if err != nil { + return nil, err + } + resources.CPUShares = cpu + } + hostconfig := container.HostConfig{ Binds: containerBinds, PortBindings: bindings, + Resources: resources, } // Containers can only be connected to 1 network at creation time @@ -414,6 +432,10 @@ type PlatformConfig struct { // An array of strings with network names to connect the container to Networks []string `hcl:"networks,optional"` + // A map of resources to configure the container with such as memory and cpu + // limits. + Resources map[string]string `hcl:"resources,optional"` + // A path to a directory that will be created for the service to store // temporary data. ScratchSpace string `hcl:"scratch_path,optional"` @@ -503,6 +525,16 @@ deploy { ), ) + doc.SetField( + "resources", + "A map of resources to configure the container with, such as memory or cpu limits.", + docs.Summary( + "these options are used to configure the container used when deploying", + "with docker. Currently, the supported resources are 'memory' and 'cpu' limits.", + "The field 'memory' is expected to be defined as \"512MB\", \"44kB\", etc.", + ), + ) + doc.SetField( "scratch_path", "a path within the container to store temporary data", diff --git a/go.mod b/go.mod index d4895a0c2f7..f348f81f712 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( github.com/docker/docker v1.4.2-0.20200319182547-c7ad2b866182 github.com/docker/go-connections v0.4.0 github.com/docker/go-metrics v0.0.1 // indirect + github.com/docker/go-units v0.4.0 github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect github.com/dustin/go-humanize v1.0.0 github.com/elazarl/go-bindata-assetfs v1.0.1