Browse code

Merge pull request #11844 from jbarbier/cgroup-parent-42

Adding cgroup-parent option for docker build

Arnaud Porterie authored on 2015/05/06 06:46:47
Showing 8 changed files
... ...
@@ -58,6 +58,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
58 58
 	flCpuQuota := cmd.Int64([]string{"-cpu-quota"}, 0, "Limit the CPU CFS (Completely Fair Scheduler) quota")
59 59
 	flCPUSetCpus := cmd.String([]string{"-cpuset-cpus"}, "", "CPUs in which to allow execution (0-3, 0,1)")
60 60
 	flCPUSetMems := cmd.String([]string{"-cpuset-mems"}, "", "MEMs in which to allow execution (0-3, 0,1)")
61
+	flCgroupParent := cmd.String([]string{"-cgroup-parent"}, "", "Optional parent cgroup for the container")
61 62
 
62 63
 	cmd.Require(flag.Exact, 1)
63 64
 	cmd.ParseFlags(args, true)
... ...
@@ -276,6 +277,7 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
276 276
 	v.Set("cpuquota", strconv.FormatInt(*flCpuQuota, 10))
277 277
 	v.Set("memory", strconv.FormatInt(memory, 10))
278 278
 	v.Set("memswap", strconv.FormatInt(memorySwap, 10))
279
+	v.Set("cgroupparent", *flCgroupParent)
279 280
 
280 281
 	v.Set("dockerfile", *dockerfileName)
281 282
 
... ...
@@ -1301,6 +1301,7 @@ func (s *Server) postBuild(version version.Version, w http.ResponseWriter, r *ht
1301 1301
 	buildConfig.CpuQuota = int64ValueOrZero(r, "cpuquota")
1302 1302
 	buildConfig.CpuSetCpus = r.FormValue("cpusetcpus")
1303 1303
 	buildConfig.CpuSetMems = r.FormValue("cpusetmems")
1304
+	buildConfig.CgroupParent = r.FormValue("cgroupparent")
1304 1305
 
1305 1306
 	// Job cancellation. Note: not all job types support this.
1306 1307
 	if closeNotifier, ok := w.(http.CloseNotifier); ok {
... ...
@@ -122,12 +122,13 @@ type Builder struct {
122 122
 	noBaseImage    bool          // indicates that this build does not start from any base image, but is being built from an empty file system.
123 123
 
124 124
 	// Set resource restrictions for build containers
125
-	cpuSetCpus string
126
-	cpuSetMems string
127
-	cpuShares  int64
128
-	cpuQuota   int64
129
-	memory     int64
130
-	memorySwap int64
125
+	cpuSetCpus   string
126
+	cpuSetMems   string
127
+	cpuShares    int64
128
+	cpuQuota     int64
129
+	cgroupParent string
130
+	memory       int64
131
+	memorySwap   int64
131 132
 
132 133
 	cancelled <-chan struct{} // When closed, job was cancelled.
133 134
 }
... ...
@@ -552,12 +552,13 @@ func (b *Builder) create() (*daemon.Container, error) {
552 552
 	b.Config.Image = b.image
553 553
 
554 554
 	hostConfig := &runconfig.HostConfig{
555
-		CpuShares:  b.cpuShares,
556
-		CpuQuota:   b.cpuQuota,
557
-		CpusetCpus: b.cpuSetCpus,
558
-		CpusetMems: b.cpuSetMems,
559
-		Memory:     b.memory,
560
-		MemorySwap: b.memorySwap,
555
+		CpuShares:    b.cpuShares,
556
+		CpuQuota:     b.cpuQuota,
557
+		CpusetCpus:   b.cpuSetCpus,
558
+		CpusetMems:   b.cpuSetMems,
559
+		CgroupParent: b.cgroupParent,
560
+		Memory:       b.memory,
561
+		MemorySwap:   b.memorySwap,
561 562
 	}
562 563
 
563 564
 	config := *b.Config
... ...
@@ -52,6 +52,7 @@ type Config struct {
52 52
 	CpuQuota       int64
53 53
 	CpuSetCpus     string
54 54
 	CpuSetMems     string
55
+	CgroupParent   string
55 56
 	AuthConfig     *cliconfig.AuthConfig
56 57
 	ConfigFile     *cliconfig.ConfigFile
57 58
 
... ...
@@ -166,6 +167,7 @@ func Build(d *daemon.Daemon, buildConfig *Config) error {
166 166
 		cpuQuota:        buildConfig.CpuQuota,
167 167
 		cpuSetCpus:      buildConfig.CpuSetCpus,
168 168
 		cpuSetMems:      buildConfig.CpuSetMems,
169
+		cgroupParent:    buildConfig.CgroupParent,
169 170
 		memory:          buildConfig.Memory,
170 171
 		memorySwap:      buildConfig.MemorySwap,
171 172
 		cancelled:       buildConfig.WaitCancelled(),
... ...
@@ -642,8 +642,9 @@ is returned by the `docker attach` command to its caller too:
642 642
       -m, --memory=""          Memory limit for all build containers
643 643
       --memory-swap=""         Total memory (memory + swap), `-1` to disable swap
644 644
       -c, --cpu-shares         CPU Shares (relative weight)
645
-      --cpuset-cpus=""         CPUs in which to allow execution, e.g. `0-3`, `0,1`
646 645
       --cpuset-mems=""         MEMs in which to allow execution, e.g. `0-3`, `0,1`
646
+      --cpuset-cpus=""         CPUs in which to allow exection, e.g. `0-3`, `0,1`
647
+      --cgroup-parent=""       Optional parent cgroup for the container
647 648
 
648 649
 Builds Docker images from a Dockerfile and a "context". A build's context is
649 650
 the files located in the specified `PATH` or `URL`.  The build process can
... ...
@@ -862,6 +863,11 @@ you refer to it on the command line.
862 862
 > children) for security reasons, and to ensure repeatable builds on remote
863 863
 > Docker hosts. This is also the reason why `ADD ../file` will not work.
864 864
 
865
+When `docker build` is run with the `--cgroup-parent` option the containers used
866
+in the build will be run with the [corresponding `docker run`
867
+flag](/reference/run/#specifying-custom-cgroups). 
868
+
869
+
865 870
 ## commit
866 871
 
867 872
     Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
... ...
@@ -465,6 +465,13 @@ Note:
465 465
 
466 466
 You would have to write policy defining a `svirt_apache_t` type.
467 467
 
468
+## Specifying custom cgroups
469
+
470
+Using the `--cgroup-parent` flag, you can pass a specific cgroup to run a
471
+container in. This allows you to create and manage cgroups on their own. You can
472
+define custom resources for those cgroups and put containers under a common
473
+parent group.
474
+
468 475
 ## Runtime constraints on resources
469 476
 
470 477
 The operator can also adjust the performance parameters of the
... ...
@@ -5322,3 +5322,30 @@ func (s *DockerSuite) TestBuildEmptyStringVolume(c *check.C) {
5322 5322
 	}
5323 5323
 
5324 5324
 }
5325
+
5326
+func (s *DockerSuite) TestBuildContainerWithCgroupParent(c *check.C) {
5327
+	testRequires(c, NativeExecDriver)
5328
+	testRequires(c, SameHostDaemon)
5329
+	defer deleteImages()
5330
+
5331
+	cgroupParent := "test"
5332
+	data, err := ioutil.ReadFile("/proc/self/cgroup")
5333
+	if err != nil {
5334
+		c.Fatalf("failed to read '/proc/self/cgroup - %v", err)
5335
+	}
5336
+	selfCgroupPaths := parseCgroupPaths(string(data))
5337
+	_, found := selfCgroupPaths["memory"]
5338
+	if !found {
5339
+		c.Fatalf("unable to find self cpu cgroup path. CgroupsPath: %v", selfCgroupPaths)
5340
+	}
5341
+	cmd := exec.Command(dockerBinary, "build", "--cgroup-parent", cgroupParent, "-")
5342
+	cmd.Stdin = strings.NewReader(`
5343
+FROM busybox
5344
+RUN cat /proc/self/cgroup
5345
+`)
5346
+
5347
+	out, _, err := runCommandWithOutput(cmd)
5348
+	if err != nil {
5349
+		c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
5350
+	}
5351
+}