Skip to content

Commit

Permalink
Merge pull request #11331 from shishir-a412ed/init
Browse files Browse the repository at this point in the history
Add support for --init to docker driver.
  • Loading branch information
schmichael committed Oct 20, 2021
2 parents fceb6ce + 94d06a8 commit 13cc8b3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/11331.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
driver/docker: Added support for Docker's `--init` parameter
```
2 changes: 2 additions & 0 deletions drivers/docker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ var (
"extra_hosts": hclspec.NewAttr("extra_hosts", "list(string)", false),
"force_pull": hclspec.NewAttr("force_pull", "bool", false),
"hostname": hclspec.NewAttr("hostname", "string", false),
"init": hclspec.NewAttr("init", "bool", false),
"interactive": hclspec.NewAttr("interactive", "bool", false),
"ipc_mode": hclspec.NewAttr("ipc_mode", "string", false),
"ipv4_address": hclspec.NewAttr("ipv4_address", "string", false),
Expand Down Expand Up @@ -434,6 +435,7 @@ type TaskConfig struct {
ExtraHosts []string `codec:"extra_hosts"`
ForcePull bool `codec:"force_pull"`
Hostname string `codec:"hostname"`
Init bool `codec:"init"`
Interactive bool `codec:"interactive"`
IPCMode string `codec:"ipc_mode"`
IPv4Address string `codec:"ipv4_address"`
Expand Down
5 changes: 5 additions & 0 deletions drivers/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,11 @@ func (d *Driver) createContainerConfig(task *drivers.TaskConfig, driverConfig *T
hostConfig.CPUSetCPUs = driverConfig.CPUSetCPUs
}

// Enable tini (docker-init) init system.
if driverConfig.Init {
hostConfig.Init = driverConfig.Init
}

// Calculate CPU Quota
// cfs_quota_us is the time per core, so we must
// multiply the time by the number of cores available
Expand Down
25 changes: 25 additions & 0 deletions drivers/docker/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,31 @@ func TestDockerDriver_DNS(t *testing.T) {

}

func TestDockerDriver_Init(t *testing.T) {
if !tu.IsCI() {
t.Parallel()
}
testutil.DockerCompatible(t)
if runtime.GOOS == "windows" {
t.Skip("Windows does not support init.")
}

task, cfg, ports := dockerTask(t)
defer freeport.Return(ports)

cfg.Init = true
require.NoError(t, task.EncodeConcreteDriverConfig(cfg))

client, d, handle, cleanup := dockerSetup(t, task, nil)
defer cleanup()
require.NoError(t, d.WaitUntilStarted(task.ID, 5*time.Second))

container, err := client.InspectContainer(handle.containerID)
require.NoError(t, err)

require.Equal(t, cfg.Init, container.HostConfig.Init)
}

func TestDockerDriver_CPUSetCPUs(t *testing.T) {
if !tu.IsCI() {
t.Parallel()
Expand Down
11 changes: 11 additions & 0 deletions website/content/docs/drivers/docker.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ config {
launching more than one of a task (using `count`) with this option set, every
container the task starts will have the same hostname.

- `init` - (Optional) `true` or `false` (default). Enable init (tini) system when
launching your container. When enabled, an init process will be used as the PID1
in the container. Specifying an init process ensures the usual responsibilities
of an init system, such as reaping zombie processes, are performed inside the
created container.

The default init process used is the first `docker-init` executable found in the
system path of the Docker daemon process. This `docker-init` binary, included in
the default installation, is backed by [tini][tini].

- `interactive` - (Optional) `true` or `false` (default). Keep STDIN open on
the container.

Expand Down Expand Up @@ -1152,6 +1162,7 @@ Windows is relatively new and rapidly evolving you may want to consult the
[cap_drop]: /docs/drivers/docker#cap_drop
[no_net_raw]: /docs/upgrade/upgrade-specific#nomad-1-1-0-rc1-1-0-5-0-12-12
[upgrade_guide_extra_hosts]: /docs/upgrade/upgrade-specific#docker-driver
[tini]: https://github.com/krallin/tini
[docker_caps]: https://docs.docker.com/engine/reference/run/#runtime-privilege-and-linux-capabilities
[allow_caps]: /docs/drivers/docker#allow_caps
[Connect]: /docs/job-specification/connect
Expand Down

0 comments on commit 13cc8b3

Please sign in to comment.