opts.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package run
  2. import (
  3. "strconv"
  4. "github.com/docker/go-connections/nat"
  5. "github.com/docker/api/containers"
  6. )
  7. type runOpts struct {
  8. name string
  9. publish []string
  10. }
  11. func toPorts(ports []string) ([]containers.Port, error) {
  12. _, bindings, err := nat.ParsePortSpecs(ports)
  13. if err != nil {
  14. return nil, err
  15. }
  16. var result []containers.Port
  17. for port, bind := range bindings {
  18. for _, portbind := range bind {
  19. var hostPort uint32
  20. if portbind.HostPort != "" {
  21. hp, err := strconv.Atoi(portbind.HostPort)
  22. if err != nil {
  23. return nil, err
  24. }
  25. hostPort = uint32(hp)
  26. } else {
  27. hostPort = uint32(port.Int())
  28. }
  29. result = append(result, containers.Port{
  30. HostPort: hostPort,
  31. ContainerPort: uint32(port.Int()),
  32. Protocol: port.Proto(),
  33. HostIP: portbind.HostIP,
  34. })
  35. }
  36. }
  37. return result, nil
  38. }
  39. func (r *runOpts) toContainerConfig(image string) (containers.ContainerConfig, error) {
  40. publish, err := toPorts(r.publish)
  41. if err != nil {
  42. return containers.ContainerConfig{}, err
  43. }
  44. return containers.ContainerConfig{
  45. ID: r.name,
  46. Image: image,
  47. Ports: publish,
  48. }, nil
  49. }