2014-02-11 23:04:39 -05:00
|
|
|
package runconfig
|
|
|
|
|
|
|
|
import (
|
2015-04-10 20:05:21 -04:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2017-02-28 23:03:43 -05:00
|
|
|
"strings"
|
2014-05-02 19:59:28 -04:00
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2014-02-11 23:04:39 -05:00
|
|
|
)
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
// DecodeHostConfig creates a HostConfig based on the specified Reader.
|
|
|
|
// It assumes the content of the reader will be JSON, and decodes it.
|
2017-07-19 10:20:13 -04:00
|
|
|
func decodeHostConfig(src io.Reader) (*container.HostConfig, error) {
|
2015-04-10 20:05:21 -04:00
|
|
|
decoder := json.NewDecoder(src)
|
|
|
|
|
2015-05-14 10:39:44 -04:00
|
|
|
var w ContainerConfigWrapper
|
2015-04-10 20:05:21 -04:00
|
|
|
if err := decoder.Decode(&w); err != nil {
|
|
|
|
return nil, err
|
2014-07-10 17:51:15 -04:00
|
|
|
}
|
2014-08-04 19:14:43 -04:00
|
|
|
|
2015-07-09 18:12:36 -04:00
|
|
|
hc := w.getHostConfig()
|
2015-04-10 20:05:21 -04:00
|
|
|
return hc, nil
|
2014-02-11 23:04:39 -05:00
|
|
|
}
|
2015-07-09 18:12:36 -04:00
|
|
|
|
|
|
|
// SetDefaultNetModeIfBlank changes the NetworkMode in a HostConfig structure
|
|
|
|
// to default if it is not populated. This ensures backwards compatibility after
|
|
|
|
// the validation of the network mode was moved from the docker CLI to the
|
|
|
|
// docker daemon.
|
2017-01-31 21:03:51 -05:00
|
|
|
func SetDefaultNetModeIfBlank(hc *container.HostConfig) {
|
2015-07-09 18:12:36 -04:00
|
|
|
if hc != nil {
|
2015-12-18 13:36:17 -05:00
|
|
|
if hc.NetworkMode == container.NetworkMode("") {
|
|
|
|
hc.NetworkMode = container.NetworkMode("default")
|
2015-07-09 18:12:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-28 23:03:43 -05:00
|
|
|
|
2017-03-10 12:39:22 -05:00
|
|
|
// validateNetContainerMode ensures that the various combinations of requested
|
2017-02-28 23:03:43 -05:00
|
|
|
// network settings wrt container mode are valid.
|
2017-03-10 12:39:22 -05:00
|
|
|
func validateNetContainerMode(c *container.Config, hc *container.HostConfig) error {
|
2017-02-28 23:03:43 -05:00
|
|
|
// We may not be passed a host config, such as in the case of docker commit
|
|
|
|
if hc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
parts := strings.Split(string(hc.NetworkMode), ":")
|
|
|
|
if parts[0] == "container" {
|
|
|
|
if len(parts) < 2 || parts[1] == "" {
|
2017-07-19 10:20:13 -04:00
|
|
|
return validationError("Invalid network mode: invalid container format container:<name|id>")
|
2017-02-28 23:03:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hc.NetworkMode.IsContainer() && c.Hostname != "" {
|
|
|
|
return ErrConflictNetworkHostname
|
|
|
|
}
|
|
|
|
|
|
|
|
if hc.NetworkMode.IsContainer() && len(hc.Links) > 0 {
|
|
|
|
return ErrConflictContainerNetworkAndLinks
|
|
|
|
}
|
|
|
|
|
|
|
|
if hc.NetworkMode.IsContainer() && len(hc.DNS) > 0 {
|
|
|
|
return ErrConflictNetworkAndDNS
|
|
|
|
}
|
|
|
|
|
|
|
|
if hc.NetworkMode.IsContainer() && len(hc.ExtraHosts) > 0 {
|
|
|
|
return ErrConflictNetworkHosts
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hc.NetworkMode.IsContainer() || hc.NetworkMode.IsHost()) && c.MacAddress != "" {
|
|
|
|
return ErrConflictContainerNetworkAndMac
|
|
|
|
}
|
|
|
|
|
|
|
|
if hc.NetworkMode.IsContainer() && (len(hc.PortBindings) > 0 || hc.PublishAllPorts == true) {
|
|
|
|
return ErrConflictNetworkPublishPorts
|
|
|
|
}
|
|
|
|
|
|
|
|
if hc.NetworkMode.IsContainer() && len(c.ExposedPorts) > 0 {
|
|
|
|
return ErrConflictNetworkExposePorts
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|