github.com/tompao/docker@v1.9.1/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.Labels == nil { 51 userConf.Labels = map[string]string{} 52 } 53 if imageConf.Labels != nil { 54 for l := range userConf.Labels { 55 imageConf.Labels[l] = userConf.Labels[l] 56 } 57 userConf.Labels = imageConf.Labels 58 } 59 60 if userConf.Entrypoint.Len() == 0 { 61 if userConf.Cmd.Len() == 0 { 62 userConf.Cmd = imageConf.Cmd 63 } 64 65 if userConf.Entrypoint == nil { 66 userConf.Entrypoint = imageConf.Entrypoint 67 } 68 } 69 if userConf.WorkingDir == "" { 70 userConf.WorkingDir = imageConf.WorkingDir 71 } 72 if len(userConf.Volumes) == 0 { 73 userConf.Volumes = imageConf.Volumes 74 } else { 75 for k, v := range imageConf.Volumes { 76 userConf.Volumes[k] = v 77 } 78 } 79 return nil 80 }