Exemple #1
0
func ports(c *project.ServiceConfig) (map[dockerclient.Port]struct{}, map[dockerclient.Port][]dockerclient.PortBinding, error) {
	ports, binding, err := nat.ParsePortSpecs(c.Ports)
	if err != nil {
		return nil, nil, err
	}

	exPorts, _, err := nat.ParsePortSpecs(c.Expose)
	if err != nil {
		return nil, nil, err
	}

	for k, v := range exPorts {
		ports[k] = v
	}

	exposedPorts := map[dockerclient.Port]struct{}{}
	for k, v := range ports {
		exposedPorts[dockerclient.Port(k)] = v
	}

	portBindings := map[dockerclient.Port][]dockerclient.PortBinding{}
	for k, bv := range binding {
		dcbs := make([]dockerclient.PortBinding, len(bv))
		for k, v := range bv {
			dcbs[k] = dockerclient.PortBinding{HostIP: v.HostIP, HostPort: v.HostPort}
		}
		portBindings[dockerclient.Port(k)] = dcbs
	}
	return exposedPorts, portBindings, nil
}
Exemple #2
0
// Port returns the host port the specified port is mapped on.
func (c *Container) Port(port string) (string, error) {
	info, err := c.findInfo()
	if err != nil {
		return "", err
	}

	if bindings, ok := info.NetworkSettings.Ports[dockerclient.Port(port)]; ok {
		result := []string{}
		for _, binding := range bindings {
			result = append(result, binding.HostIP+":"+binding.HostPort)
		}

		return strings.Join(result, "\n"), nil
	}
	return "", nil
}