当前位置: 首页>>代码示例>>Golang>>正文


Golang dockerclient.PortBinding类代码示例

本文整理汇总了Golang中github.com/samalba/dockerclient.PortBinding的典型用法代码示例。如果您正苦于以下问题:Golang PortBinding类的具体用法?Golang PortBinding怎么用?Golang PortBinding使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了PortBinding类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: RunByConfig

func (d *DockerProxy) RunByConfig(runConfig ContainerRunConfig) (string, error) {
	portBingds := map[string][]dockerclient.PortBinding{}
	exposedPorts := map[string]struct{}{}
	for _, port := range runConfig.PortBindings {
		pb := dockerclient.PortBinding{}
		pb.HostIp = "0.0.0.0"
		pb.HostPort = fmt.Sprintf("%d", port.GetHostPort())
		key := fmt.Sprintf("%d/%s", port.ContainerPort, port.Protocal)
		portBingds[key] = []dockerclient.PortBinding{pb}

		exposedPorts[key] = struct{}{}
	}

	config := &dockerclient.ContainerConfig{}
	config.Image = runConfig.Image
	config.Env = runConfig.Envs
	config.Cmd = runConfig.Cmds
	config.Hostname = runConfig.Hostname

	config.ExposedPorts = exposedPorts

	hostConfig := &dockerclient.HostConfig{}

	hostConfig.PortBindings = portBingds
	hostConfig.Binds = runConfig.Bindings
	hostConfig.Dns = runConfig.DNS
	hostConfig.RestartPolicy = dockerclient.RestartPolicy{
		Name:              runConfig.RestartPolicy.Name,
		MaximumRetryCount: int64(runConfig.RestartPolicy.MaxTry),
	}

	config.HostConfig = *hostConfig

	cid, err := d.CreateContainer(config, runConfig.Name)
	if err != nil {
		return "", errors.New(fmt.Sprintf("Failed to create a container. name: %s, error: %s", runConfig.Name, err.Error()))
	}
	fmt.Printf("Container created. name:%s, id:%s\n", runConfig.Name, cid)

	if err := d.StartContainer(cid, hostConfig); err != nil {
		fmt.Printf("Failed to start container. name:%s, id:%s.\n", runConfig.Name, cid)
		return cid, err
	}
	fmt.Printf("Container start successfully. name:%s, id:%s.\n", runConfig.Name, cid)

	return cid, nil
}
开发者ID:sdgdsffdsfff,项目名称:dockerf,代码行数:47,代码来源:docker_proxy.go

示例2: GenerateProxyConfig


//.........这里部分代码省略.........

		if interlockData.CheckInterval != 0 {
			checkInterval = interlockData.CheckInterval
			logMessage(log.DebugLevel,
				fmt.Sprintf("using custom check interval for %s: %d", domain, checkInterval))
		}

		hostBalanceAlgorithms[domain] = "roundrobin"

		if interlockData.BalanceAlgorithm != "" {
			hostBalanceAlgorithms[domain] = interlockData.BalanceAlgorithm
		}

		if len(interlockData.BackendOptions) > 0 {
			hostBackendOptions[domain] = interlockData.BackendOptions
			logMessage(log.DebugLevel,
				fmt.Sprintf("using backend options for %s: %s", domain, strings.Join(interlockData.BackendOptions, ",")))
		}

		hostSSLOnly[domain] = false
		if interlockData.SSLOnly {
			logMessage(log.DebugLevel,
				fmt.Sprintf("configuring ssl redirect for %s", domain))
			hostSSLOnly[domain] = true
		}

		//host := cInfo.NetworkSettings.IpAddress
		ports := cInfo.NetworkSettings.Ports
		if len(ports) == 0 {
			logMessage(log.WarnLevel, fmt.Sprintf("%s: no ports exposed", cntId))
			continue
		}

		var portDef dockerclient.PortBinding

		for _, v := range ports {
			if len(v) > 0 {
				portDef = dockerclient.PortBinding{
					HostIp:   v[0].HostIp,
					HostPort: v[0].HostPort,
				}
				break
			}
		}

		if p.pluginConfig.ProxyBackendOverrideAddress != "" {
			portDef.HostIp = p.pluginConfig.ProxyBackendOverrideAddress
		}

		addr := fmt.Sprintf("%s:%s", portDef.HostIp, portDef.HostPort)

		if interlockData.Port != 0 {
			interlockPort := fmt.Sprintf("%d", interlockData.Port)
			for k, v := range ports {
				parts := strings.Split(k, "/")
				if parts[0] == interlockPort {
					port := v[0]
					logMessage(log.DebugLevel,
						fmt.Sprintf("%s: found specified port %s exposed as %s", domain, interlockPort, port.HostPort))
					addr = fmt.Sprintf("%s:%s", portDef.HostIp, port.HostPort)
					break
				}
			}
		}

		container_name := cInfo.Name[1:]
开发者ID:wbchaf,项目名称:interlock,代码行数:67,代码来源:haproxy.go

示例3: generateNginxConfig

func (p NginxPlugin) generateNginxConfig() (*NginxConfig, error) {
	containers, err := p.client.ListContainers(false, false, "")
	if err != nil {
		return nil, err
	}

	var hosts []*Host
	upstreamServers := map[string][]string{}
	serverNames := map[string][]string{}
	//hostBalanceAlgorithms := map[string]string{}
	hostSSL := map[string]bool{}
	hostSSLCert := map[string]string{}
	hostSSLCertKey := map[string]string{}
	hostSSLOnly := map[string]bool{}
	hostWebsocketEndpoints := map[string][]string{}

	for _, c := range containers {
		cntId := c.Id[:12]
		// load interlock data
		cInfo, err := p.client.InspectContainer(cntId)
		if err != nil {
			return nil, err
		}

		env := cInfo.Config.Env
		interlockData := &InterlockData{}

		for _, e := range env {
			envParts := strings.Split(e, "=")
			if envParts[0] == "INTERLOCK_DATA" {
				b := bytes.NewBufferString(envParts[1])
				if err := json.NewDecoder(b).Decode(&interlockData); err != nil {
					logMessage(log.WarnLevel,
						fmt.Sprintf("%s: unable to parse interlock data: %s", cntId, err))
				}
				break
			}
		}
		hostname := cInfo.Config.Hostname
		domain := cInfo.Config.Domainname

		if interlockData.Hostname != "" {
			hostname = interlockData.Hostname
		}

		if interlockData.Domain != "" {
			domain = interlockData.Domain
		}

		if domain == "" {
			continue
		}

		if hostname != domain && hostname != "" {
			domain = fmt.Sprintf("%s.%s", hostname, domain)
		}

		// check if the first server name is there; if not, add
		// this happens if there are multiple backend containers
		if _, ok := serverNames[domain]; !ok {
			serverNames[domain] = []string{domain}
		}

		hostSSL[domain] = interlockData.SSL

		hostSSLOnly[domain] = false
		if interlockData.SSLOnly {
			logMessage(log.DebugLevel,
				fmt.Sprintf("configuring ssl redirect for %s", domain))
			hostSSLOnly[domain] = true
		}

		// set cert paths
		baseCertPath := p.pluginConfig.SSLCertDir
		if interlockData.SSLCert != "" {
			certPath := filepath.Join(baseCertPath, interlockData.SSLCert)
			logMessage(log.InfoLevel,
				fmt.Sprintf("ssl cert for %s: %s", domain, certPath))
			hostSSLCert[domain] = certPath
		}

		if interlockData.SSLCertKey != "" {
			keyPath := filepath.Join(baseCertPath, interlockData.SSLCertKey)
			logMessage(log.InfoLevel,
				fmt.Sprintf("ssl key for %s: %s", domain, keyPath))
			hostSSLCertKey[domain] = keyPath
		}

		ports := cInfo.NetworkSettings.Ports
		if len(ports) == 0 {
			logMessage(log.WarnLevel, fmt.Sprintf("%s: no ports exposed", cntId))
			continue
		}

		var portDef dockerclient.PortBinding

		for _, v := range ports {
			if len(v) > 0 {
				portDef = dockerclient.PortBinding{
					HostIp:   v[0].HostIp,
//.........这里部分代码省略.........
开发者ID:wbchaf,项目名称:interlock,代码行数:101,代码来源:nginx.go

示例4: GenerateProxyConfig

func (p *NginxLoadBalancer) GenerateProxyConfig() (*Config, error) {
	containers, err := p.client.ListContainers(false, false, "")
	if err != nil {
		return nil, err
	}

	var hosts []*Host
	upstreamServers := map[string][]string{}
	serverNames := map[string][]string{}
	//hostBalanceAlgorithms := map[string]string{}
	hostSSL := map[string]bool{}
	hostSSLCert := map[string]string{}
	hostSSLCertKey := map[string]string{}
	hostSSLOnly := map[string]bool{}
	hostSSLBackend := map[string]bool{}
	hostWebsocketEndpoints := map[string][]string{}

	for _, c := range containers {
		cntId := c.Id[:12]
		// load interlock data
		cInfo, err := p.client.InspectContainer(cntId)
		if err != nil {
			return nil, err
		}

		hostname := cInfo.Config.Hostname
		domain := cInfo.Config.Domainname

		if v, ok := cInfo.Config.Labels[ext.InterlockHostnameLabel]; ok {
			hostname = v
		}

		if v, ok := cInfo.Config.Labels[ext.InterlockDomainLabel]; ok {
			domain = v
		}

		if domain == "" {
			continue
		}

		if hostname != domain && hostname != "" {
			domain = fmt.Sprintf("%s.%s", hostname, domain)
		}

		// check if the first server name is there; if not, add
		// this happens if there are multiple backend containers
		if _, ok := serverNames[domain]; !ok {
			serverNames[domain] = []string{domain}
		}

		if _, ok := cInfo.Config.Labels[ext.InterlockSSLLabel]; ok {
			hostSSL[domain] = true
		}

		hostSSLOnly[domain] = false

		if _, ok := cInfo.Config.Labels[ext.InterlockSSLOnlyLabel]; ok {
			log().Infof("configuring ssl redirect for %s", domain)
			hostSSLOnly[domain] = true
		}

		// check ssl backend
		hostSSLBackend[domain] = false
		if _, ok := cInfo.Config.Labels[ext.InterlockSSLBackendLabel]; ok {
			log().Debugf("configuring ssl backend for %s", domain)
			hostSSLBackend[domain] = true
		}

		// set cert paths
		baseCertPath := p.cfg.SSLCertPath
		if v, ok := cInfo.Config.Labels[ext.InterlockSSLCertLabel]; ok {
			certPath := filepath.Join(baseCertPath, v)
			log().Infof("ssl cert for %s: %s", domain, certPath)
			hostSSLCert[domain] = certPath
		}

		if v, ok := cInfo.Config.Labels[ext.InterlockSSLCertKeyLabel]; ok {
			keyPath := filepath.Join(baseCertPath, v)
			log().Infof("ssl key for %s: %s", domain, keyPath)
			hostSSLCertKey[domain] = keyPath
		}

		ports := cInfo.NetworkSettings.Ports
		if len(ports) == 0 {
			log().Warnf("%s: no ports exposed", cntId)
			continue
		}

		var portDef dockerclient.PortBinding

		for _, v := range ports {
			if len(v) > 0 {
				portDef = dockerclient.PortBinding{
					HostIp:   v[0].HostIp,
					HostPort: v[0].HostPort,
				}
				break
			}
		}

//.........这里部分代码省略.........
开发者ID:tmrudick,项目名称:interlock,代码行数:101,代码来源:generate.go


注:本文中的github.com/samalba/dockerclient.PortBinding类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。