コード例 #1
0
// GetDockerEnvIPv4 returns the IPv4 of a container within the network bridge
// of an environment.
func GetDockerEnvIPv4(
	docker *dockerapi.Client, containerID, networkID string) (string, error) {
	network, err := docker.NetworkInfo(networkID)
	if err != nil {
		return "", err
	}
	endpoint, ok := network.Containers[containerID]
	if !ok {
		return "", fmt.Errorf("Container not found")
	}
	return strings.Split(endpoint.IPv4Address, "/")[0], nil
}
コード例 #2
0
// CreateDockerEnvNetwork creates the network bridge for an environment. Returns
// the network IPv4 of the bridge (useful for listening for connections from
// that bridge) as well as our own IPv4 on that network.
func CreateDockerEnvNetwork(
	docker *dockerapi.Client, environment string) (
	networkID string, networkIPv4 string, ownIPv4 string, err error) {
	// Create.
	networkName := getNetworkName(environment)
	network, err := docker.CreateNetwork(dockerapi.CreateNetworkOptions{
		Name:           networkName,
		Driver:         "bridge",
		CheckDuplicate: true,
	})
	if err != nil {
		if strings.Contains(err.Error(), "already exists") {
			// We already have this network. No need to create it.
			network, err = docker.NetworkInfo(networkName)
			if err != nil {
				return "", "", "", err
			}
		} else {
			return "", "", "", err
		}
	} else {
		// Get more info.
		network, err = docker.NetworkInfo(network.ID)
		if err != nil {
			return "", "", "", err
		}
	}
	if len(network.IPAM.Config) == 0 {
		return "", "", "", fmt.Errorf("Network has no config")
	}

	// Connect ourselves to the network.
	ownIPv4, err = ConnectToDockerEnvNetwork(
		docker, GetOwnContainerID(), network.ID)
	if err != nil {
		docker.RemoveNetwork(network.ID)
		return "", "", "", err
	}

	networkIPv4 = strings.Split(network.IPAM.Config[0].Subnet, "/")[0]
	return network.ID, networkIPv4, ownIPv4, nil
}