github.com/dougm/docker@v1.5.0/runconfig/merge.go (about)

     1  package runconfig
     2  
     3  import (
     4  	"strings"
     5  
     6  	log "github.com/Sirupsen/logrus"
     7  	"github.com/docker/docker/nat"
     8  )
     9  
    10  func Merge(userConf, imageConf *Config) error {
    11  	if userConf.User == "" {
    12  		userConf.User = imageConf.User
    13  	}
    14  	if userConf.Memory == 0 {
    15  		userConf.Memory = imageConf.Memory
    16  	}
    17  	if userConf.MemorySwap == 0 {
    18  		userConf.MemorySwap = imageConf.MemorySwap
    19  	}
    20  	if userConf.CpuShares == 0 {
    21  		userConf.CpuShares = imageConf.CpuShares
    22  	}
    23  	if len(userConf.ExposedPorts) == 0 {
    24  		userConf.ExposedPorts = imageConf.ExposedPorts
    25  	} else if imageConf.ExposedPorts != nil {
    26  		if userConf.ExposedPorts == nil {
    27  			userConf.ExposedPorts = make(nat.PortSet)
    28  		}
    29  		for port := range imageConf.ExposedPorts {
    30  			if _, exists := userConf.ExposedPorts[port]; !exists {
    31  				userConf.ExposedPorts[port] = struct{}{}
    32  			}
    33  		}
    34  	}
    35  
    36  	if len(userConf.PortSpecs) > 0 {
    37  		if userConf.ExposedPorts == nil {
    38  			userConf.ExposedPorts = make(nat.PortSet)
    39  		}
    40  		ports, _, err := nat.ParsePortSpecs(userConf.PortSpecs)
    41  		if err != nil {
    42  			return err
    43  		}
    44  		for port := range ports {
    45  			if _, exists := userConf.ExposedPorts[port]; !exists {
    46  				userConf.ExposedPorts[port] = struct{}{}
    47  			}
    48  		}
    49  		userConf.PortSpecs = nil
    50  	}
    51  	if len(imageConf.PortSpecs) > 0 {
    52  		// FIXME: I think we can safely remove this. Leaving it for now for the sake of reverse-compat paranoia.
    53  		log.Debugf("Migrating image port specs to containter: %s", strings.Join(imageConf.PortSpecs, ", "))
    54  		if userConf.ExposedPorts == nil {
    55  			userConf.ExposedPorts = make(nat.PortSet)
    56  		}
    57  
    58  		ports, _, err := nat.ParsePortSpecs(imageConf.PortSpecs)
    59  		if err != nil {
    60  			return err
    61  		}
    62  		for port := range ports {
    63  			if _, exists := userConf.ExposedPorts[port]; !exists {
    64  				userConf.ExposedPorts[port] = struct{}{}
    65  			}
    66  		}
    67  	}
    68  
    69  	if len(userConf.Env) == 0 {
    70  		userConf.Env = imageConf.Env
    71  	} else {
    72  		for _, imageEnv := range imageConf.Env {
    73  			found := false
    74  			imageEnvKey := strings.Split(imageEnv, "=")[0]
    75  			for _, userEnv := range userConf.Env {
    76  				userEnvKey := strings.Split(userEnv, "=")[0]
    77  				if imageEnvKey == userEnvKey {
    78  					found = true
    79  				}
    80  			}
    81  			if !found {
    82  				userConf.Env = append(userConf.Env, imageEnv)
    83  			}
    84  		}
    85  	}
    86  
    87  	if len(userConf.Entrypoint) == 0 {
    88  		if len(userConf.Cmd) == 0 {
    89  			userConf.Cmd = imageConf.Cmd
    90  		}
    91  
    92  		if userConf.Entrypoint == nil {
    93  			userConf.Entrypoint = imageConf.Entrypoint
    94  		}
    95  	}
    96  	if userConf.WorkingDir == "" {
    97  		userConf.WorkingDir = imageConf.WorkingDir
    98  	}
    99  	if len(userConf.Volumes) == 0 {
   100  		userConf.Volumes = imageConf.Volumes
   101  	} else {
   102  		for k, v := range imageConf.Volumes {
   103  			userConf.Volumes[k] = v
   104  		}
   105  	}
   106  	return nil
   107  }