diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/docker.go | 80 |
1 files changed, 67 insertions, 13 deletions
diff --git a/src/docker.go b/src/docker.go index 26b0b67..142c87f 100644 --- a/src/docker.go +++ b/src/docker.go @@ -7,9 +7,9 @@ import ( "os" "time" + "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/mount" - "github.com/docker/docker/api/types/network" "github.com/docker/docker/client" ) @@ -60,6 +60,9 @@ func spawnCompanion(username string, accesscode string) { }, } + // configure the host + // this mounts the docker socket and the companion seed file into the + // container HostConfig := &container.HostConfig{ Mounts: []mount.Mount{ { @@ -75,26 +78,77 @@ func spawnCompanion(username string, accesscode string) { }, } - containerNetworkID, err := setupNetwork("circus", true) - - NetworkingConfig := &network.NetworkingConfig{ - EndpointsConfig: map[string]*network.EndpointSettings{ - "circus": { - NetworkID: containerNetworkID, - }, - }, - } - log.Println("Starting the container") - _, err := dockerCLI.ContainerCreate(dockerCtx, Config, HostConfig, NetworkingConfig, "") + // create the docker container + resp, err := dockerCLI.ContainerCreate(dockerCtx, Config, HostConfig, nil, "") if err != nil { panic(err) } - log.Println("Container started") + log.Println("Container started!") + + // id of the created container + containerID := resp.ID + + log.Printf("Adding the container (%s) to the circus network", containerID) + + // get the circus network id + circusNetworkID := getCircusNetworkID() + log.Printf("circus network ID: %s", circusNetworkID) + + // connect the companion container to the circus network + connectContainerToNetwork(containerID, circusNetworkID) + + log.Println("Container added to the circus network") } func generateSessionSalt() string { return fmt.Sprintf("%d", time.Now().UnixNano()) } + +// listDockerNetworks lists the docker networks +func listDockerNetworks(dockerCtx context.Context, dockerCLI *client.Client) ([]types.NetworkResource, error) { + + // list the docker networks + networks, err := dockerCLI.NetworkList(dockerCtx, types.NetworkListOptions{}) + if err != nil { + return nil, fmt.Errorln("network list err: ", err) + } + + return networks, nil +} + +// getDockerNetworkIDFromName returns the docker network ID using the given name +// to find it. +func getDockerNetworkIDFromName(string NetworkNameToFind) (string, error) { + + for _, network := range networks { + if network.Name == NetworkNameToFind { + return networkID, nil + } + } + return "", fmt.Errorf("could not find the network id for the network %s: %s", circus, err) +} + +// connectContainerToNetwork inserts the container with the given ID into the +// network with the given ID +func connectContainerToNetwork(string containerID, string networkID) { + err = dockerCLI.NetworkConnect(dockerCtx, networkID, containerID, nil) + if err != nil { + log.Printf("Could not connect container %s to network %s: %s", containerID[:10], networkID[:10], err) + } +} + +func getCircusNetworkID(dockerCtx context.Context, dockerCLI *client.Client) (string, error) { + networks, err := listDockerNetworks(dockerCtx, dockerCLI) + if err != nil { + return "", err + } + + circusNetworkID, err := getDockerNetworkIDFromName("circus") + if err != nil { + return "", err + } + return circusNetworkID, nil +} |