hostconfig.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package runconfig // import "github.com/docker/docker/runconfig"
  2. import (
  3. "io"
  4. "strings"
  5. "github.com/docker/docker/api/types/container"
  6. "github.com/docker/docker/api/types/network"
  7. )
  8. // DecodeHostConfig creates a HostConfig based on the specified Reader.
  9. // It assumes the content of the reader will be JSON, and decodes it.
  10. func decodeHostConfig(src io.Reader) (*container.HostConfig, error) {
  11. var w ContainerConfigWrapper
  12. if err := loadJSON(src, &w); err != nil {
  13. return nil, err
  14. }
  15. return w.getHostConfig(), nil
  16. }
  17. // SetDefaultNetModeIfBlank changes the NetworkMode in a HostConfig structure
  18. // to default if it is not populated. This ensures backwards compatibility after
  19. // the validation of the network mode was moved from the docker CLI to the
  20. // docker daemon.
  21. func SetDefaultNetModeIfBlank(hc *container.HostConfig) {
  22. if hc != nil && hc.NetworkMode == "" {
  23. hc.NetworkMode = network.NetworkDefault
  24. }
  25. }
  26. // validateNetContainerMode ensures that the various combinations of requested
  27. // network settings wrt container mode are valid.
  28. func validateNetContainerMode(c *container.Config, hc *container.HostConfig) error {
  29. parts := strings.Split(string(hc.NetworkMode), ":")
  30. if parts[0] == "container" {
  31. if len(parts) < 2 || parts[1] == "" {
  32. return validationError("Invalid network mode: invalid container format container:<name|id>")
  33. }
  34. }
  35. if hc.NetworkMode.IsContainer() && c.Hostname != "" {
  36. return ErrConflictNetworkHostname
  37. }
  38. if hc.NetworkMode.IsContainer() && len(hc.Links) > 0 {
  39. return ErrConflictContainerNetworkAndLinks
  40. }
  41. if hc.NetworkMode.IsContainer() && len(hc.DNS) > 0 {
  42. return ErrConflictNetworkAndDNS
  43. }
  44. if hc.NetworkMode.IsContainer() && len(hc.ExtraHosts) > 0 {
  45. return ErrConflictNetworkHosts
  46. }
  47. if hc.NetworkMode.IsContainer() && (len(hc.PortBindings) > 0 || hc.PublishAllPorts) {
  48. return ErrConflictNetworkPublishPorts
  49. }
  50. if hc.NetworkMode.IsContainer() && len(c.ExposedPorts) > 0 {
  51. return ErrConflictNetworkExposePorts
  52. }
  53. return nil
  54. }