本文整理汇总了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
}