Skip to content

Commit cbaf7d9

Browse files
committed
Add --init option to docker service create
Signed-off-by: Timothy Higinbottom <timhigins@gmail.com> Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent a8ee42a commit cbaf7d9

5 files changed

Lines changed: 40 additions & 0 deletions

File tree

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: 8 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,11 @@ 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+
updateBool := func(flag string, field *bool) {
241+
if flags.Changed(flag) {
242+
*field, _ = flags.GetBool(flag)
243+
}
244+
}
238245
updateString := func(flag string, field *string) {
239246
if flags.Changed(flag) {
240247
*field, _ = flags.GetString(flag)
@@ -306,6 +313,7 @@ func updateService(ctx context.Context, apiClient client.NetworkAPIClient, flags
306313
updateString(flagWorkdir, &cspec.Dir)
307314
updateString(flagUser, &cspec.User)
308315
updateString(flagHostname, &cspec.Hostname)
316+
updateBool(flagInit, &cspec.Init)
309317
if err := updateIsolation(flagIsolation, &cspec.Isolation); err != nil {
310318
return err
311319
}

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 --stop-signal=SIGUSR1
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 --stop-signal, 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 --stop-signal=SIGWINCH
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{

vendor/github.com/docker/docker/api/types/swarm/container.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)