runconfig/merge.go
6393c383
 package runconfig
 
 import (
e454be75
 	"strings"
 
b3ee9ac7
 	"github.com/docker/docker/nat"
a02f67be
 	"github.com/docker/docker/pkg/log"
6393c383
 )
 
 func Merge(userConf, imageConf *Config) error {
 	if userConf.User == "" {
 		userConf.User = imageConf.User
 	}
 	if userConf.Memory == 0 {
 		userConf.Memory = imageConf.Memory
 	}
 	if userConf.MemorySwap == 0 {
 		userConf.MemorySwap = imageConf.MemorySwap
 	}
 	if userConf.CpuShares == 0 {
 		userConf.CpuShares = imageConf.CpuShares
 	}
7dba5024
 	if len(userConf.ExposedPorts) == 0 {
6393c383
 		userConf.ExposedPorts = imageConf.ExposedPorts
 	} else if imageConf.ExposedPorts != nil {
 		if userConf.ExposedPorts == nil {
 			userConf.ExposedPorts = make(nat.PortSet)
 		}
 		for port := range imageConf.ExposedPorts {
 			if _, exists := userConf.ExposedPorts[port]; !exists {
 				userConf.ExposedPorts[port] = struct{}{}
 			}
 		}
 	}
 
7dba5024
 	if len(userConf.PortSpecs) > 0 {
6393c383
 		if userConf.ExposedPorts == nil {
 			userConf.ExposedPorts = make(nat.PortSet)
 		}
 		ports, _, err := nat.ParsePortSpecs(userConf.PortSpecs)
 		if err != nil {
 			return err
 		}
 		for port := range ports {
 			if _, exists := userConf.ExposedPorts[port]; !exists {
 				userConf.ExposedPorts[port] = struct{}{}
 			}
 		}
 		userConf.PortSpecs = nil
 	}
7dba5024
 	if len(imageConf.PortSpecs) > 0 {
6393c383
 		// FIXME: I think we can safely remove this. Leaving it for now for the sake of reverse-compat paranoia.
a02f67be
 		log.Debugf("Migrating image port specs to containter: %s", strings.Join(imageConf.PortSpecs, ", "))
6393c383
 		if userConf.ExposedPorts == nil {
 			userConf.ExposedPorts = make(nat.PortSet)
 		}
 
 		ports, _, err := nat.ParsePortSpecs(imageConf.PortSpecs)
 		if err != nil {
 			return err
 		}
 		for port := range ports {
 			if _, exists := userConf.ExposedPorts[port]; !exists {
 				userConf.ExposedPorts[port] = struct{}{}
 			}
 		}
 	}
ab26c16b
 
7dba5024
 	if len(userConf.Env) == 0 {
6393c383
 		userConf.Env = imageConf.Env
 	} else {
 		for _, imageEnv := range imageConf.Env {
 			found := false
 			imageEnvKey := strings.Split(imageEnv, "=")[0]
 			for _, userEnv := range userConf.Env {
 				userEnvKey := strings.Split(userEnv, "=")[0]
 				if imageEnvKey == userEnvKey {
 					found = true
 				}
 			}
 			if !found {
 				userConf.Env = append(userConf.Env, imageEnv)
 			}
 		}
 	}
e454be75
 
7dba5024
 	if len(userConf.Entrypoint) == 0 {
aa2d6dbc
 		if len(userConf.Cmd) == 0 {
 			userConf.Cmd = imageConf.Cmd
 		}
7f8cdeb1
 
 		if userConf.Entrypoint == nil {
 			userConf.Entrypoint = imageConf.Entrypoint
 		}
6393c383
 	}
 	if userConf.WorkingDir == "" {
 		userConf.WorkingDir = imageConf.WorkingDir
 	}
7dba5024
 	if len(userConf.Volumes) == 0 {
6393c383
 		userConf.Volumes = imageConf.Volumes
 	} else {
 		for k, v := range imageConf.Volumes {
 			userConf.Volumes[k] = v
 		}
 	}
 	return nil
 }