-
Notifications
You must be signed in to change notification settings - Fork 601
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9fa26ff
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
docker-gen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.SILENT : | ||
.PHONY : docker-gen clean fmt | ||
|
||
all: docker-gen | ||
|
||
docker-gen: | ||
echo "Building docker-gen" | ||
go build | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
docker-gen | ||
===== | ||
|
||
Config file generator using running docker container meta-data. | ||
|
||
This is mostly a proof of concept to generate config files for: | ||
|
||
* fluentd, logstash or other centralized logging tools that tail the containers JSON log file. | ||
* logrotate files to rotate container JSON log files | ||
|
||
`go get github.com/jwilder/docker-gen` | ||
|
||
`docker-gen template.file` | ||
|
||
TODO: | ||
|
||
* Restart command hooks for when files are regenerated | ||
* Tail docker event stream to detect when containers are started and stopped automatically |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/fsouza/go-dockerclient" | ||
"os" | ||
"path/filepath" | ||
"text/template" | ||
) | ||
|
||
func usage() { | ||
println("Usage: docker-log template.file") | ||
} | ||
|
||
func generateFile(templatePath string, containers []docker.APIContainers) { | ||
tmpl, err := template.ParseFiles(templatePath) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = tmpl.ExecuteTemplate(os.Stdout, filepath.Base(templatePath), containers) | ||
} | ||
|
||
func main() { | ||
|
||
if len(os.Args) != 2 { | ||
usage() | ||
os.Exit(1) | ||
} | ||
|
||
endpoint := "unix:///var/run/docker.sock" | ||
client, err := docker.NewClient(endpoint) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
containers, err := client.ListContainers(docker.ListContainersOptions{ | ||
All: false, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
generateFile(os.Args[1], containers) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
|
||
## File input | ||
## read docker logs with tag=docker.container | ||
|
||
{{range $key, $value := .}} | ||
<source> | ||
type tail | ||
format json | ||
time_key time | ||
path /var/lib/docker/containers/{{ $value.ID }}/{{ $value.ID }}-json.log | ||
pos_file /var/lib/docker/containers/{{ $value.ID }}/{{ $value.ID }}-json.log.pos | ||
tag docker.container.{{printf "%.*s" 12 $value.ID}} | ||
rotate_wait 5 | ||
</source> | ||
{{end}} | ||
|
||
<match docker.**> | ||
type stdout | ||
</match> | ||
|