Skip to content

Commit

Permalink
use a go binary for docker network joining
Browse files Browse the repository at this point in the history
This gets rid of the brittle bash code that shells out to the docker
cli.
  • Loading branch information
codekitchen committed Mar 2, 2016
1 parent 11414b0 commit 877e68c
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
join-networks.tar.gz
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "vendor/github.com/fsouza/go-dockerclient"]
path = vendor/github.com/fsouza/go-dockerclient
url = https://github.com/codekitchen/go-dockerclient.git
branch = add-network-id
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ RUN wget https://github.com/codekitchen/docker-gen/releases/download/v0.6-groupB
&& rm docker-gen-linux-amd64-$DOCKER_GEN_VERSION.tar.gz
# /XXX

RUN wget https://github.com/codekitchen/dinghy-http-proxy/releases/download/join-networks-v1/join-networks.tar.gz \
&& tar -C /app -xzvf join-networks.tar.gz \
&& rm join-networks.tar.gz

# override nginx configs
COPY *.conf /etc/nginx/conf.d/

Expand Down
6 changes: 6 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -e

docker run --rm -v "$PWD":/go/src/github.com/codekitchen/dinghy-http-proxy -w /go/src/github.com/codekitchen/dinghy-http-proxy golang:1.6 go build -v -o join-networks
tar czvf join-networks.tar.gz join-networks
rm join-networks
64 changes: 64 additions & 0 deletions join-networks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"flag"

docker "github.com/fsouza/go-dockerclient"
)

var (
endpoint = "unix:///tmp/docker.sock"
)

func main() {
var containerName = flag.String("container-name", "", "the name of this docker container")
flag.Parse()

client, err := docker.NewClient(endpoint)
if err != nil {
panic(err)
}

currentNetworks := getJoinedNetworks(client, *containerName)
bridgeNetworks := getBridgeNetworks(client)

for _, id := range bridgeNetworks {
if !currentNetworks[id] {
err := client.ConnectNetwork(id, docker.NetworkConnectionOptions{
Container: *containerName,
})
if err != nil {
panic(err)
}
}
}
}

func getJoinedNetworks(client *docker.Client, containerName string) (networks map[string]bool) {
networks = make(map[string]bool)

container, err := client.InspectContainer(containerName)
if err != nil {
panic(err)
}

for _, net := range container.NetworkSettings.Networks {
networks[net.NetworkID] = true
}

return
}

func getBridgeNetworks(client *docker.Client) (ids []string) {
networks, err := client.ListNetworks()
if err != nil {
panic(err)
}

for _, net := range networks {
if net.Driver == "bridge" {
ids = append(ids, net.ID)
}
}
return
}
12 changes: 1 addition & 11 deletions reload-nginx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,7 @@ name=${CONTAINER_NAME:-dinghy_http_proxy}
# Join any networks we haven't joined yet, so that we can talk to containers on
# those networks. This allows us to talk to containers created with
# docker-compose v2 configs.
#
# This would be better rewritten as a binary that talks directly to the docker
# API, to avoid the brittle CLI parsing.
docker inspect -f '{{ range .NetworkSettings.Networks }}{{ .NetworkID }}
{{ end }}' $name > /tmp/current_networks

for network in `docker network ls | grep bridge | awk '{print $1}'`; do
if ! grep -q $network /tmp/current_networks; then
docker network connect $network $name
fi
done
/app/join-networks -container-name $name

# Now that we can reach these other hosts, reload nginx. Order is important
# here, as nginx errors on startup if it can't resolve any of the specified
Expand Down
1 change: 1 addition & 0 deletions vendor/github.com/fsouza/go-dockerclient
Submodule go-dockerclient added at ddbe4d

0 comments on commit 877e68c

Please sign in to comment.