Skip to content

Commit 62733f8

Browse files
committed
Add -endpoint flag for connection to a different docker endpoint
Allow specifying a docker endpoint via the command line. This overrides the DOCKER_HOST env if both are specified. Both unix and tcp URLs are supported.
1 parent 2d77dc7 commit 62733f8

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

docker-gen.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var (
2121
configFile string
2222
configs ConfigFile
2323
interval int
24+
endpoint string
2425
wg sync.WaitGroup
2526
)
2627

@@ -89,7 +90,7 @@ func (r *RuntimeContainer) Equals(o RuntimeContainer) bool {
8990
}
9091

9192
func usage() {
92-
println("Usage: docker-gen [-config file] [-watch=false] [-notify=\"restart xyz\"] [-interval=0] <template> [<dest>]")
93+
println("Usage: docker-gen [-config file] [-watch=false] [-notify=\"restart xyz\"] [-interval=0] [-endpoint tcp|unix://..] <template> [<dest>]")
9394
}
9495

9596
func generateFromContainers(client *docker.Client) {
@@ -187,6 +188,7 @@ func main() {
187188
flag.StringVar(&notifyCmd, "notify", "", "run command after template is regenerated")
188189
flag.StringVar(&configFile, "config", "", "config file with template directives")
189190
flag.IntVar(&interval, "interval", 0, "notify command interval (s)")
191+
flag.StringVar(&endpoint, "endpoint", "", "docker api endpoint")
190192
flag.Parse()
191193

192194
if flag.NArg() < 1 && configFile == "" {
@@ -213,10 +215,10 @@ func main() {
213215
Config: []Config{config}}
214216
}
215217

216-
endpoint := "unix:///var/run/docker.sock"
217-
if os.Getenv("DOCKER_HOST") != "" {
218+
if endpoint == "" && os.Getenv("DOCKER_HOST") != "" {
218219
endpoint = os.Getenv("DOCKER_HOST")
219220
}
221+
220222
client, err := docker.NewClient(endpoint)
221223

222224
if err != nil {

docker_client.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ func splitDockerImage(img string) (string, string, string) {
103103
}
104104

105105
func newConn() (*httputil.ClientConn, error) {
106-
proto, addr, err := parseHost(os.Getenv("DOCKER_HOST"))
106+
dockerHost := endpoint
107+
if dockerHost == "" && os.Getenv("DOCKER_HOST") != "" {
108+
dockerHost = os.Getenv("DOCKER_HOST")
109+
}
110+
111+
proto, addr, err := parseHost(endpoint)
107112
if err != nil {
108113
return nil, err
109114
}

0 commit comments

Comments
 (0)