2014-02-11 23:04:39 -05:00
|
|
|
package runconfig
|
|
|
|
|
|
|
|
import (
|
2014-05-19 18:04:51 -04:00
|
|
|
"strings"
|
|
|
|
|
2015-12-18 13:36:17 -05:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2015-12-18 12:58:48 -05:00
|
|
|
"github.com/docker/go-connections/nat"
|
2014-02-11 23:04:39 -05:00
|
|
|
)
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
// Merge merges two Config, the image container configuration (defaults values),
|
|
|
|
// and the user container configuration, either passed by the API or generated
|
|
|
|
// by the cli.
|
|
|
|
// It will mutate the specified user configuration (userConf) with the image
|
|
|
|
// configuration where the user configuration is incomplete.
|
2015-12-18 13:36:17 -05:00
|
|
|
func Merge(userConf, imageConf *container.Config) error {
|
2014-02-11 23:04:39 -05:00
|
|
|
if userConf.User == "" {
|
|
|
|
userConf.User = imageConf.User
|
|
|
|
}
|
2014-07-08 13:10:28 -04:00
|
|
|
if len(userConf.ExposedPorts) == 0 {
|
2014-02-11 23:04:39 -05:00
|
|
|
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{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-08 13:10:28 -04:00
|
|
|
if len(userConf.Env) == 0 {
|
2014-02-11 23:04:39 -05:00
|
|
|
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
|
2015-07-03 14:37:34 -04:00
|
|
|
break
|
2014-02-11 23:04:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
userConf.Env = append(userConf.Env, imageEnv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-19 18:04:51 -04:00
|
|
|
|
2015-08-11 19:48:41 -04:00
|
|
|
if userConf.Labels == nil {
|
|
|
|
userConf.Labels = map[string]string{}
|
|
|
|
}
|
|
|
|
if imageConf.Labels != nil {
|
|
|
|
for l := range userConf.Labels {
|
|
|
|
imageConf.Labels[l] = userConf.Labels[l]
|
|
|
|
}
|
|
|
|
userConf.Labels = imageConf.Labels
|
|
|
|
}
|
|
|
|
|
2015-04-10 20:05:21 -04:00
|
|
|
if userConf.Entrypoint.Len() == 0 {
|
|
|
|
if userConf.Cmd.Len() == 0 {
|
2014-07-10 01:46:11 -04:00
|
|
|
userConf.Cmd = imageConf.Cmd
|
|
|
|
}
|
2014-10-23 20:23:25 -04:00
|
|
|
|
|
|
|
if userConf.Entrypoint == nil {
|
|
|
|
userConf.Entrypoint = imageConf.Entrypoint
|
|
|
|
}
|
2014-02-11 23:04:39 -05:00
|
|
|
}
|
|
|
|
if userConf.WorkingDir == "" {
|
|
|
|
userConf.WorkingDir = imageConf.WorkingDir
|
|
|
|
}
|
2014-07-08 13:10:28 -04:00
|
|
|
if len(userConf.Volumes) == 0 {
|
2014-02-11 23:04:39 -05:00
|
|
|
userConf.Volumes = imageConf.Volumes
|
|
|
|
} else {
|
|
|
|
for k, v := range imageConf.Volumes {
|
|
|
|
userConf.Volumes[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|