Skip to content

Commit c1cd45d

Browse files
committed
Choose default-cgroup parent by cgroup driver
It's "/docker" for cgroupfs and "system.slice" for systemd. Fix moby#19140 Signed-off-by: Alexander Morozov <[email protected]>
1 parent 7fab931 commit c1cd45d

File tree

7 files changed

+25
-22
lines changed

7 files changed

+25
-22
lines changed

daemon/config_unix.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (config *Config) InstallFlags(cmd *flag.FlagSet, usageFn func(string) strin
7878
cmd.BoolVar(&config.Bridge.EnableUserlandProxy, []string{"-userland-proxy"}, true, usageFn("Use userland proxy for loopback traffic"))
7979
cmd.BoolVar(&config.EnableCors, []string{"#api-enable-cors", "#-api-enable-cors"}, false, usageFn("Enable CORS headers in the remote API, this is deprecated by --api-cors-header"))
8080
cmd.StringVar(&config.CorsHeaders, []string{"-api-cors-header"}, "", usageFn("Set CORS headers in the remote API"))
81-
cmd.StringVar(&config.CgroupParent, []string{"-cgroup-parent"}, "/docker", usageFn("Set parent cgroup for all containers"))
81+
cmd.StringVar(&config.CgroupParent, []string{"-cgroup-parent"}, "", usageFn("Set parent cgroup for all containers"))
8282

8383
config.attachExperimentalFlags(cmd, usageFn)
8484
}

daemon/container_operations_unix.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/docker/docker/pkg/fileutils"
2424
"github.com/docker/docker/pkg/idtools"
2525
"github.com/docker/docker/pkg/mount"
26+
"github.com/docker/docker/pkg/parsers"
2627
"github.com/docker/docker/pkg/stringid"
2728
"github.com/docker/docker/runconfig"
2829
"github.com/docker/go-units"
@@ -241,6 +242,20 @@ func (daemon *Daemon) populateCommand(c *container.Container, env []string) erro
241242
}
242243
uidMap, gidMap := daemon.GetUIDGIDMaps()
243244

245+
defaultCgroupParent := "/docker"
246+
if daemon.configStore.CgroupParent != "" {
247+
defaultCgroupParent = daemon.configStore.CgroupParent
248+
} else {
249+
for _, option := range daemon.configStore.ExecOptions {
250+
key, val, err := parsers.ParseKeyValueOpt(option)
251+
if err != nil || !strings.EqualFold(key, "native.cgroupdriver") {
252+
continue
253+
}
254+
if val == "systemd" {
255+
defaultCgroupParent = "system.slice"
256+
}
257+
}
258+
}
244259
c.Command = &execdriver.Command{
245260
CommonCommand: execdriver.CommonCommand{
246261
ID: c.ID,
@@ -258,7 +273,7 @@ func (daemon *Daemon) populateCommand(c *container.Container, env []string) erro
258273
AutoCreatedDevices: autoCreatedDevices,
259274
CapAdd: c.HostConfig.CapAdd.Slice(),
260275
CapDrop: c.HostConfig.CapDrop.Slice(),
261-
CgroupParent: daemon.configStore.CgroupParent,
276+
CgroupParent: defaultCgroupParent,
262277
GIDMapping: gidMap,
263278
GroupAdd: c.HostConfig.GroupAdd,
264279
Ipc: ipc,

daemon/execdriver/driver_unix.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,11 @@ func InitContainer(c *Command) *configs.Config {
146146
// This can be overridden later by driver during mount setup based
147147
// on volume options
148148
SetRootPropagation(container, mount.RPRIVATE)
149+
container.Cgroups.Parent = c.CgroupParent
149150

150151
// check to see if we are running in ramdisk to disable pivot root
151152
container.NoPivotRoot = os.Getenv("DOCKER_RAMDISK") != ""
152153

153-
// Default parent cgroup is "docker". Override if required.
154-
if c.CgroupParent != "" {
155-
container.Cgroups.Parent = c.CgroupParent
156-
}
157154
return container
158155
}
159156

daemon/execdriver/native/driver.go

-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616

1717
"github.com/Sirupsen/logrus"
1818
"github.com/docker/docker/daemon/execdriver"
19-
"github.com/docker/docker/daemon/execdriver/native/template"
2019
"github.com/docker/docker/pkg/parsers"
2120
"github.com/docker/docker/pkg/pools"
2221
"github.com/docker/docker/pkg/reexec"
@@ -90,7 +89,6 @@ func NewDriver(root string, options []string) (*Driver, error) {
9089
case "systemd":
9190
if systemd.UseSystemd() {
9291
cgm = libcontainer.SystemdCgroups
93-
template.SystemdCgroups = true
9492
} else {
9593
// warn them that they chose the wrong driver
9694
logrus.Warn("You cannot use systemd as native.cgroupdriver, using cgroupfs instead")

daemon/execdriver/native/template/default_template_linux.go

+1-9
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ import (
99

1010
const defaultMountFlags = syscall.MS_NOEXEC | syscall.MS_NOSUID | syscall.MS_NODEV
1111

12-
// SystemdCgroups indicates whether systemd cgroup implemenation is in use or not
13-
var SystemdCgroups = false
14-
1512
// New returns the docker default configuration for libcontainer
1613
func New() *configs.Config {
1714
container := &configs.Config{
@@ -40,7 +37,7 @@ func New() *configs.Config {
4037
{Type: "NEWUSER"},
4138
}),
4239
Cgroups: &configs.Cgroup{
43-
Parent: "/docker",
40+
ScopePrefix: "docker", // systemd only
4441
Resources: &configs.Resources{
4542
AllowAllDevices: false,
4643
MemorySwappiness: -1,
@@ -99,10 +96,5 @@ func New() *configs.Config {
9996
container.AppArmorProfile = "docker-default"
10097
}
10198

102-
if SystemdCgroups {
103-
container.Cgroups.Parent = "system.slice"
104-
container.Cgroups.ScopePrefix = "docker"
105-
}
106-
10799
return container
108100
}

docs/reference/commandline/daemon.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ weight = -1
1717

1818
Options:
1919
--api-cors-header="" Set CORS headers in the remote API
20-
--authz-plugin=[] Set authorization plugins to load
20+
--authz-plugin=[] Set authorization plugins to load
2121
-b, --bridge="" Attach containers to a network bridge
2222
--bip="" Specify network bridge IP
23-
--cgroup-parent=/docker Set parent cgroup for all containers
23+
--cgroup-parent= Set parent cgroup for all containers
2424
-D, --debug Enable debug mode
2525
--default-gateway="" Container default gateway IPv4 address
2626
--default-gateway-v6="" Container default gateway IPv6 address
@@ -647,7 +647,8 @@ set like this:
647647
# Default cgroup parent
648648

649649
The `--cgroup-parent` option allows you to set the default cgroup parent
650-
to use for containers. If this option is not set, it defaults to `/docker`.
650+
to use for containers. If this option is not set, it defaults to `/docker` for
651+
fs cgroup driver and `system.slice` for systemd cgroup driver.
651652

652653
If the cgroup has a leading forward slash (`/`), the cgroup is created
653654
under the root cgroup, otherwise the cgroup is created under the daemon

man/docker-daemon.8.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ docker-daemon - Enable daemon mode
1010
[**--authz-plugin**[=*[]*]]
1111
[**-b**|**--bridge**[=*BRIDGE*]]
1212
[**--bip**[=*BIP*]]
13-
[**--cgroup-parent**[=*/docker*]]
13+
[**--cgroup-parent**[=*[]*]]
1414
[**--cluster-store**[=*[]*]]
1515
[**--cluster-advertise**[=*[]*]]
1616
[**--cluster-store-opt**[=*map[]*]]
@@ -82,7 +82,7 @@ format.
8282
Use the provided CIDR notation address for the dynamically created bridge (docker0); Mutually exclusive of \-b
8383

8484
**--cgroup-parent**=""
85-
Set parent cgroup for all containers. Default is "/docker".
85+
Set parent cgroup for all containers. Default is "/docker" for fs cgroup driver and "system.slice" for systemd cgroup driver.
8686

8787
**--cluster-store**=""
8888
URL of the distributed storage backend

0 commit comments

Comments
 (0)