Browse code

Merge pull request #27857 from vasil-yordanov/docker-service-hostname-2

Adding the hostname option to docker service command

Vincent Demeester authored on 2016/11/04 23:22:55
Showing 6 changed files
... ...
@@ -13,6 +13,7 @@ type ContainerSpec struct {
13 13
 	Labels          map[string]string       `json:",omitempty"`
14 14
 	Command         []string                `json:",omitempty"`
15 15
 	Args            []string                `json:",omitempty"`
16
+	Hostname        string                  `json:",omitempty"`
16 17
 	Env             []string                `json:",omitempty"`
17 18
 	Dir             string                  `json:",omitempty"`
18 19
 	User            string                  `json:",omitempty"`
... ...
@@ -33,6 +33,7 @@ func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
33 33
 
34 34
 	flags.VarP(&opts.labels, flagLabel, "l", "Service labels")
35 35
 	flags.Var(&opts.containerLabels, flagContainerLabel, "Container labels")
36
+	flags.StringVar(&opts.hostname, flagHostname, "", "Container hostname")
36 37
 	flags.VarP(&opts.env, flagEnv, "e", "Set environment variables")
37 38
 	flags.Var(&opts.envFile, flagEnvFile, "Read in a file of environment variables")
38 39
 	flags.Var(&opts.mounts, flagMount, "Attach a filesystem mount to the service")
... ...
@@ -316,6 +316,7 @@ type serviceOptions struct {
316 316
 	containerLabels opts.ListOpts
317 317
 	image           string
318 318
 	args            []string
319
+	hostname        string
319 320
 	env             opts.ListOpts
320 321
 	envFile         opts.ListOpts
321 322
 	workdir         string
... ...
@@ -387,6 +388,7 @@ func (opts *serviceOptions) ToService() (swarm.ServiceSpec, error) {
387 387
 				Image:           opts.image,
388 388
 				Args:            opts.args,
389 389
 				Env:             currentEnv,
390
+				Hostname:        opts.hostname,
390 391
 				Labels:          runconfigopts.ConvertKVStringsToMap(opts.containerLabels.GetAll()),
391 392
 				Dir:             opts.workdir,
392 393
 				User:            opts.user,
... ...
@@ -486,6 +488,7 @@ const (
486 486
 	flagContainerLabelRemove  = "container-label-rm"
487 487
 	flagContainerLabelAdd     = "container-label-add"
488 488
 	flagEndpointMode          = "endpoint-mode"
489
+	flagHostname              = "hostname"
489 490
 	flagEnv                   = "env"
490 491
 	flagEnvFile               = "env-file"
491 492
 	flagEnvRemove             = "env-rm"
... ...
@@ -13,14 +13,15 @@ import (
13 13
 
14 14
 func containerSpecFromGRPC(c *swarmapi.ContainerSpec) types.ContainerSpec {
15 15
 	containerSpec := types.ContainerSpec{
16
-		Image:   c.Image,
17
-		Labels:  c.Labels,
18
-		Command: c.Command,
19
-		Args:    c.Args,
20
-		Env:     c.Env,
21
-		Dir:     c.Dir,
22
-		User:    c.User,
23
-		Groups:  c.Groups,
16
+		Image:    c.Image,
17
+		Labels:   c.Labels,
18
+		Command:  c.Command,
19
+		Args:     c.Args,
20
+		Hostname: c.Hostname,
21
+		Env:      c.Env,
22
+		Dir:      c.Dir,
23
+		User:     c.User,
24
+		Groups:   c.Groups,
24 25
 	}
25 26
 
26 27
 	// Mounts
... ...
@@ -67,14 +68,15 @@ func containerSpecFromGRPC(c *swarmapi.ContainerSpec) types.ContainerSpec {
67 67
 
68 68
 func containerToGRPC(c types.ContainerSpec) (*swarmapi.ContainerSpec, error) {
69 69
 	containerSpec := &swarmapi.ContainerSpec{
70
-		Image:   c.Image,
71
-		Labels:  c.Labels,
72
-		Command: c.Command,
73
-		Args:    c.Args,
74
-		Env:     c.Env,
75
-		Dir:     c.Dir,
76
-		User:    c.User,
77
-		Groups:  c.Groups,
70
+		Image:    c.Image,
71
+		Labels:   c.Labels,
72
+		Command:  c.Command,
73
+		Args:     c.Args,
74
+		Hostname: c.Hostname,
75
+		Env:      c.Env,
76
+		Dir:      c.Dir,
77
+		User:     c.User,
78
+		Groups:   c.Groups,
78 79
 	}
79 80
 
80 81
 	if c.StopGracePeriod != nil {
... ...
@@ -128,6 +128,7 @@ func (c *containerConfig) config() *enginecontainer.Config {
128 128
 	config := &enginecontainer.Config{
129 129
 		Labels:      c.labels(),
130 130
 		User:        c.spec().User,
131
+		Hostname:    c.spec().Hostname,
131 132
 		Env:         c.spec().Env,
132 133
 		WorkingDir:  c.spec().Dir,
133 134
 		Image:       c.image(),
... ...
@@ -32,6 +32,7 @@ Options:
32 32
       --health-retries int               Consecutive failures needed to report unhealthy
33 33
       --health-timeout duration          Maximum time to allow one check to run
34 34
       --help                             Print usage
35
+      --hostname                         Service containers hostname
35 36
   -l, --label value                      Service labels (default [])
36 37
       --limit-cpu value                  Limit CPUs (default 0.000)
37 38
       --limit-memory value               Limit Memory (default 0 B)
... ...
@@ -134,6 +135,12 @@ This sets environmental variables for all tasks in a service. For example:
134 134
 $ docker service create --name redis_2 --replicas 5 --env MYVAR=foo redis:3.0.6
135 135
 ```
136 136
 
137
+### Create a docker service with specific hostname (--hostname)
138
+
139
+This option sets the docker service containers hostname to a specific string. For example:
140
+```bash
141
+$ docker service create --name redis --hostname myredis redis:3.0.6
142
+```
137 143
 ### Set metadata on a service (-l, --label)
138 144
 
139 145
 A label is a `key=value` pair that applies metadata to a service. To label a