github.com/nf/docker@v1.8.1/runconfig/merge.go (about)

     1  package runconfig
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/docker/docker/pkg/nat"
     7  )
     8  
     9  func Merge(userConf, imageConf *Config) error {
    10  	if userConf.User == "" {
    11  		userConf.User = imageConf.User
    12  	}
    13  	if len(userConf.ExposedPorts) == 0 {
    14  		userConf.ExposedPorts = imageConf.ExposedPorts
    15  	} else if imageConf.ExposedPorts != nil {
    16  		if userConf.ExposedPorts == nil {
    17  			userConf.ExposedPorts = make(nat.PortSet)
    18  		}
    19  		for port := range imageConf.ExposedPorts {
    20  			if _, exists := userConf.ExposedPorts[port]; !exists {
    21  				userConf.ExposedPorts[port] = struct{}{}
    22  			}
    23  		}
    24  	}
    25  
    26  	if len(userConf.Env) == 0 {
    27  		userConf.Env = imageConf.Env
    28  	} else {
    29  		for _, imageEnv := range imageConf.Env {
    30  			found := false
    31  			imageEnvKey := strings.Split(imageEnv, "=")[0]
    32  			for _, userEnv := range userConf.Env {
    33  				userEnvKey := strings.Split(userEnv, "=")[0]
    34  				if imageEnvKey == userEnvKey {
    35  					found = true
    36  					break
    37  				}
    38  			}
    39  			if !found {
    40  				userConf.Env = append(userConf.Env, imageEnv)
    41  			}
    42  		}
    43  	}
    44  
    45  	if userConf.Entrypoint.Len() == 0 {
    46  		if userConf.Cmd.Len() == 0 {
    47  			userConf.Cmd = imageConf.Cmd
    48  		}
    49  
    50  		if userConf.Entrypoint == nil {
    51  			userConf.Entrypoint = imageConf.Entrypoint
    52  		}
    53  	}
    54  	if userConf.WorkingDir == "" {
    55  		userConf.WorkingDir = imageConf.WorkingDir
    56  	}
    57  	if len(userConf.Volumes) == 0 {
    58  		userConf.Volumes = imageConf.Volumes
    59  	} else {
    60  		for k, v := range imageConf.Volumes {
    61  			userConf.Volumes[k] = v
    62  		}
    63  	}
    64  	return nil
    65  }