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


Golang PortBinding.HostPort方法代码示例

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


在下文中一共展示了PortBinding.HostPort方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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


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