Skip to content

Commit ff13f03

Browse files
committed
Add --init option to docker service create
Signed-off-by: Timothy Higinbottom <[email protected]> Signed-off-by: Vincent Demeester <[email protected]>
1 parent 4cb3c70 commit ff13f03

7 files changed

Lines changed: 53 additions & 0 deletions

File tree

cli/command/formatter/service.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ ContainerSpec:
8585
{{- if .ContainerWorkDir }}
8686
Dir: {{ .ContainerWorkDir }}
8787
{{- end -}}
88+
{{- if .HasContainerInit }}
89+
Init: {{ .ContainerInit }}
90+
{{- end -}}
8891
{{- if .ContainerUser }}
8992
User: {{ .ContainerUser }}
9093
{{- end }}
@@ -372,6 +375,14 @@ func (ctx *serviceInspectContext) ContainerUser() string {
372375
return ctx.Service.Spec.TaskTemplate.ContainerSpec.User
373376
}
374377

378+
func (ctx *serviceInspectContext) HasContainerInit() bool {
379+
return ctx.Service.Spec.TaskTemplate.ContainerSpec.Init != nil
380+
}
381+
382+
func (ctx *serviceInspectContext) ContainerInit() bool {
383+
return *ctx.Service.Spec.TaskTemplate.ContainerSpec.Init
384+
}
385+
375386
func (ctx *serviceInspectContext) ContainerMounts() []mounttypes.Mount {
376387
return ctx.Service.Spec.TaskTemplate.ContainerSpec.Mounts
377388
}

cli/command/service/create.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command {
5858
flags.SetAnnotation(flagDNSSearch, "version", []string{"1.25"})
5959
flags.Var(&opts.hosts, flagHost, "Set one or more custom host-to-IP mappings (host:ip)")
6060
flags.SetAnnotation(flagHost, "version", []string{"1.25"})
61+
flags.BoolVar(&opts.init, flagInit, false, "Use an init inside each service container to forward signals and reap processes")
62+
flags.SetAnnotation(flagInit, "version", []string{"1.37"})
6163

6264
flags.Var(cliopts.NewListOptsRef(&opts.resources.resGenericResources, ValidateSingleGenericResource), "generic-resource", "User defined resources")
6365
flags.SetAnnotation(flagHostAdd, "version", []string{"1.32"})

cli/command/service/opts.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ type serviceOptions struct {
480480
user string
481481
groups opts.ListOpts
482482
credentialSpec credentialSpecOpt
483+
init bool
483484
stopSignal string
484485
tty bool
485486
readOnly bool
@@ -624,6 +625,7 @@ func (options *serviceOptions) ToService(ctx context.Context, apiClient client.N
624625
TTY: options.tty,
625626
ReadOnly: options.readOnly,
626627
Mounts: options.mounts.Value(),
628+
Init: &options.init,
627629
DNSConfig: &swarm.DNSConfig{
628630
Nameservers: options.dns.GetAll(),
629631
Search: options.dnsSearch.GetAll(),
@@ -875,6 +877,7 @@ const (
875877
flagRollbackMonitor = "rollback-monitor"
876878
flagRollbackOrder = "rollback-order"
877879
flagRollbackParallelism = "rollback-parallelism"
880+
flagInit = "init"
878881
flagStopGracePeriod = "stop-grace-period"
879882
flagStopSignal = "stop-signal"
880883
flagTTY = "tty"

cli/command/service/update.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ func newUpdateCommand(dockerCli command.Cli) *cobra.Command {
9494
flags.SetAnnotation(flagDNSSearchAdd, "version", []string{"1.25"})
9595
flags.Var(&options.hosts, flagHostAdd, "Add a custom host-to-IP mapping (host:ip)")
9696
flags.SetAnnotation(flagHostAdd, "version", []string{"1.25"})
97+
flags.BoolVar(&options.init, flagInit, false, "Use an init inside each service container to forward signals and reap processes")
98+
flags.SetAnnotation(flagInit, "version", []string{"1.37"})
9799

98100
// Add needs parsing, Remove only needs the key
99101
flags.Var(newListOptsVar(), flagGenericResourcesRemove, "Remove a Generic resource")
@@ -235,6 +237,12 @@ func runUpdate(dockerCli command.Cli, flags *pflag.FlagSet, options *serviceOpti
235237

236238
// nolint: gocyclo
237239
func updateService(ctx context.Context, apiClient client.NetworkAPIClient, flags *pflag.FlagSet, spec *swarm.ServiceSpec) error {
240+
updateBoolPtr := func(flag string, field **bool) {
241+
if flags.Changed(flag) {
242+
b, _ := flags.GetBool(flag)
243+
*field = &b
244+
}
245+
}
238246
updateString := func(flag string, field *string) {
239247
if flags.Changed(flag) {
240248
*field, _ = flags.GetString(flag)
@@ -306,6 +314,7 @@ func updateService(ctx context.Context, apiClient client.NetworkAPIClient, flags
306314
updateString(flagWorkdir, &cspec.Dir)
307315
updateString(flagUser, &cspec.User)
308316
updateString(flagHostname, &cspec.Hostname)
317+
updateBoolPtr(flagInit, &cspec.Init)
309318
if err := updateIsolation(flagIsolation, &cspec.Isolation); err != nil {
310319
return err
311320
}

cli/command/service/update_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,32 @@ func TestUpdateReadOnly(t *testing.T) {
547547
assert.Check(t, !cspec.ReadOnly)
548548
}
549549

550+
func TestUpdateInit(t *testing.T) {
551+
spec := &swarm.ServiceSpec{
552+
TaskTemplate: swarm.TaskSpec{
553+
ContainerSpec: &swarm.ContainerSpec{},
554+
},
555+
}
556+
cspec := spec.TaskTemplate.ContainerSpec
557+
558+
// Update with --init=true
559+
flags := newUpdateCommand(nil).Flags()
560+
flags.Set("init", "true")
561+
updateService(nil, nil, flags, spec)
562+
assert.Check(t, is.Equal(true, *cspec.Init))
563+
564+
// Update without --init, no change
565+
flags = newUpdateCommand(nil).Flags()
566+
updateService(nil, nil, flags, spec)
567+
assert.Check(t, is.Equal(true, *cspec.Init))
568+
569+
// Update with --init=false
570+
flags = newUpdateCommand(nil).Flags()
571+
flags.Set("init", "false")
572+
updateService(nil, nil, flags, spec)
573+
assert.Check(t, is.Equal(false, *cspec.Init))
574+
}
575+
550576
func TestUpdateStopSignal(t *testing.T) {
551577
spec := &swarm.ServiceSpec{
552578
TaskTemplate: swarm.TaskSpec{

docs/reference/commandline/service_create.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Options:
4343
--help Print usage
4444
--host list Set one or more custom host-to-IP mappings (host:ip)
4545
--hostname string Container hostname
46+
--init bool Use an init inside each service container to forward signals and reap processes
4647
--isolation string Service container isolation mode
4748
-l, --label list Service labels
4849
--limit-cpu decimal Limit CPUs

docs/reference/commandline/service_update.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Options:
5454
--host-add list Add a custom host-to-IP mapping (host:ip)
5555
--host-rm list Remove a custom host-to-IP mapping (host:ip)
5656
--hostname string Container hostname
57+
--init bool Use an init inside each service container to forward signals and reap processes
5758
--image string Service image tag
5859
--isolation string Service container isolation mode
5960
--label-add list Add or update a service label

0 commit comments

Comments
 (0)