github.com/mheon/docker@v0.11.2-0.20150922122814-44f47903a831/runconfig/merge.go (about)

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