Skip to content

Commit a3948f8

Browse files
committed
Merge pull request #27 from JustAdam/container-restart
Added flag -restart-container to allow restarting of docker containers.
2 parents 1b6ec01 + 9455238 commit a3948f8

File tree

2 files changed

+54
-27
lines changed

2 files changed

+54
-27
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ $ docker-gen
4040
Usage: docker-gen [options] <template> [<dest>]
4141
```
4242

43-
[-config file] [-watch=false] [-notify="restart xyz"] [-interval=0] [-endpoint tcp|unix://..]
43+
[-config file] [-watch=false] [-notify="restart xyz"] [-notify-sighup="nginx-proxy"] [-interval=0] [-endpoint tcp|unix://..]
4444

4545
*Options:*
4646
```
@@ -50,6 +50,7 @@ Usage: docker-gen [options] <template> [<dest>]
5050
-interval=0:run notify command interval (s). Useful for service registration use cases.
5151
-notify="": run command after template is regenerated ["restart xyz"]. Useful for restarting nginx,
5252
reloading haproxy, etc..
53+
-notify-sighup="": send HUP signal to container. Equivalent to `docker kill -s HUP container-ID`
5354
-only-exposed=false: only include containers with exposed ports
5455
-only-published=false: only include containers with published ports (implies -only-exposed)
5556
-version=false: show version

docker-gen.go

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@ import (
1515
)
1616

1717
var (
18-
buildVersion string
19-
version bool
20-
watch bool
21-
notifyCmd string
22-
onlyExposed bool
23-
onlyPublished bool
24-
configFile string
25-
configs ConfigFile
26-
interval int
27-
endpoint string
28-
wg sync.WaitGroup
18+
buildVersion string
19+
version bool
20+
watch bool
21+
notifyCmd string
22+
notifySigHUPContainerID string
23+
onlyExposed bool
24+
onlyPublished bool
25+
configFile string
26+
configs ConfigFile
27+
interval int
28+
endpoint string
29+
wg sync.WaitGroup
2930
)
3031

3132
type Event struct {
@@ -66,13 +67,14 @@ func (i *DockerImage) String() string {
6667
}
6768

6869
type Config struct {
69-
Template string
70-
Dest string
71-
Watch bool
72-
NotifyCmd string
73-
OnlyExposed bool
74-
OnlyPublished bool
75-
Interval int
70+
Template string
71+
Dest string
72+
Watch bool
73+
NotifyCmd string
74+
NotifyContainers map[string]docker.Signal
75+
OnlyExposed bool
76+
OnlyPublished bool
77+
Interval int
7678
}
7779

7880
type ConfigFile struct {
@@ -120,7 +122,7 @@ func (r *RuntimeContainer) PublishedAddresses() []Address {
120122
}
121123

122124
func usage() {
123-
println("Usage: docker-gen [-config file] [-watch=false] [-notify=\"restart xyz\"] [-interval=0] [-endpoint tcp|unix://..] <template> [<dest>]")
125+
println("Usage: docker-gen [-config file] [-watch=false] [-notify=\"restart xyz\"] [-notify-sighup=\"container-ID\"] [-interval=0] [-endpoint tcp|unix://..] <template> [<dest>]")
124126
}
125127

126128
func generateFromContainers(client *docker.Client) {
@@ -136,6 +138,7 @@ func generateFromContainers(client *docker.Client) {
136138
continue
137139
}
138140
runNotifyCmd(config)
141+
sendSignalToContainer(client, config)
139142
}
140143
}
141144

@@ -153,6 +156,23 @@ func runNotifyCmd(config Config) {
153156
}
154157
}
155158

159+
func sendSignalToContainer(client *docker.Client, config Config) {
160+
if len(config.NotifyContainers) < 1 {
161+
return
162+
}
163+
164+
for container, signal := range config.NotifyContainers {
165+
log.Printf("Sending container '%s' signal '%v'", container, signal)
166+
killOpts := docker.KillContainerOptions{
167+
ID: container,
168+
Signal: signal,
169+
}
170+
if err := client.KillContainer(killOpts); err != nil {
171+
log.Printf("Error sending signal to container: %s", err)
172+
}
173+
}
174+
}
175+
156176
func loadConfig(file string) error {
157177
_, err := toml.DecodeFile(file, &configs)
158178
if err != nil {
@@ -186,6 +206,7 @@ func generateAtInterval(client *docker.Client, configs ConfigFile) {
186206
// ignore changed return value. always run notify command
187207
generateFile(configCopy, containers)
188208
runNotifyCmd(configCopy)
209+
sendSignalToContainer(client, configCopy)
189210
case <-quit:
190211
ticker.Stop()
191212
return
@@ -225,6 +246,7 @@ func initFlags() {
225246
flag.BoolVar(&onlyExposed, "only-exposed", false, "only include containers with exposed ports")
226247
flag.BoolVar(&onlyPublished, "only-published", false, "only include containers with published ports (implies -only-exposed)")
227248
flag.StringVar(&notifyCmd, "notify", "", "run command after template is regenerated")
249+
flag.StringVar(&notifySigHUPContainerID, "notify-sighup", "", "send HUP signal to container. Equivalent to `docker kill -s HUP container-ID`")
228250
flag.StringVar(&configFile, "config", "", "config file with template directives")
229251
flag.IntVar(&interval, "interval", 0, "notify command interval (s)")
230252
flag.StringVar(&endpoint, "endpoint", "", "docker api endpoint")
@@ -252,13 +274,17 @@ func main() {
252274
}
253275
} else {
254276
config := Config{
255-
Template: flag.Arg(0),
256-
Dest: flag.Arg(1),
257-
Watch: watch,
258-
NotifyCmd: notifyCmd,
259-
OnlyExposed: onlyExposed,
260-
OnlyPublished: onlyPublished,
261-
Interval: interval,
277+
Template: flag.Arg(0),
278+
Dest: flag.Arg(1),
279+
Watch: watch,
280+
NotifyCmd: notifyCmd,
281+
NotifyContainers: make(map[string]docker.Signal),
282+
OnlyExposed: onlyExposed,
283+
OnlyPublished: onlyPublished,
284+
Interval: interval,
285+
}
286+
if notifySigHUPContainerID != "" {
287+
config.NotifyContainers[notifySigHUPContainerID] = docker.SIGHUP
262288
}
263289
configs = ConfigFile{
264290
Config: []Config{config}}

0 commit comments

Comments
 (0)