Skip to content

Commit 9fa26ff

Browse files
committed
Initial commit
0 parents  commit 9fa26ff

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
docker-gen

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.SILENT :
2+
.PHONY : docker-gen clean fmt
3+
4+
all: docker-gen
5+
6+
docker-gen:
7+
echo "Building docker-gen"
8+
go build
9+

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
docker-gen
2+
=====
3+
4+
Config file generator using running docker container meta-data.
5+
6+
This is mostly a proof of concept to generate config files for:
7+
8+
* fluentd, logstash or other centralized logging tools that tail the containers JSON log file.
9+
* logrotate files to rotate container JSON log files
10+
11+
`go get github.com/jwilder/docker-gen`
12+
13+
`docker-gen template.file`
14+
15+
TODO:
16+
17+
* Restart command hooks for when files are regenerated
18+
* Tail docker event stream to detect when containers are started and stopped automatically

docker-gen.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"github.com/fsouza/go-dockerclient"
5+
"os"
6+
"path/filepath"
7+
"text/template"
8+
)
9+
10+
func usage() {
11+
println("Usage: docker-log template.file")
12+
}
13+
14+
func generateFile(templatePath string, containers []docker.APIContainers) {
15+
tmpl, err := template.ParseFiles(templatePath)
16+
if err != nil {
17+
panic(err)
18+
}
19+
20+
err = tmpl.ExecuteTemplate(os.Stdout, filepath.Base(templatePath), containers)
21+
}
22+
23+
func main() {
24+
25+
if len(os.Args) != 2 {
26+
usage()
27+
os.Exit(1)
28+
}
29+
30+
endpoint := "unix:///var/run/docker.sock"
31+
client, err := docker.NewClient(endpoint)
32+
33+
if err != nil {
34+
panic(err)
35+
}
36+
37+
containers, err := client.ListContainers(docker.ListContainersOptions{
38+
All: false,
39+
})
40+
if err != nil {
41+
panic(err)
42+
}
43+
44+
generateFile(os.Args[1], containers)
45+
}

templates/fluentd.conf.tmpl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
## File input
3+
## read docker logs with tag=docker.container
4+
5+
{{range $key, $value := .}}
6+
<source>
7+
type tail
8+
format json
9+
time_key time
10+
path /var/lib/docker/containers/{{ $value.ID }}/{{ $value.ID }}-json.log
11+
pos_file /var/lib/docker/containers/{{ $value.ID }}/{{ $value.ID }}-json.log.pos
12+
tag docker.container.{{printf "%.*s" 12 $value.ID}}
13+
rotate_wait 5
14+
</source>
15+
{{end}}
16+
17+
<match docker.**>
18+
type stdout
19+
</match>
20+

0 commit comments

Comments
 (0)