github.com/moby/docker@v26.1.3+incompatible/runconfig/hostconfig.go (about)

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