2018-02-05 16:05:59 -05:00
|
|
|
package runconfig // import "github.com/docker/docker/runconfig"
|
2014-02-11 23:04:39 -05:00
|
|
|
|
|
|
|
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-05-14 10:39:44 -04:00
|
|
|
var w ContainerConfigWrapper
|
2021-08-09 05:11:54 -04:00
|
|
|
if err := json.NewDecoder(src).Decode(&w); err != nil {
|
2015-04-10 20:05:21 -04:00
|
|
|
return nil, err
|
2014-07-10 17:51:15 -04:00
|
|
|
}
|
2021-08-09 05:11:54 -04:00
|
|
|
return w.getHostConfig(), 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) {
|
2021-08-09 05:11:54 -04:00
|
|
|
if hc != nil && hc.NetworkMode == "" {
|
|
|
|
hc.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
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-09-11 14:55:05 -04:00
|
|
|
if hc.NetworkMode.IsContainer() && (len(hc.PortBindings) > 0 || hc.PublishAllPorts) {
|
2017-02-28 23:03:43 -05:00
|
|
|
return ErrConflictNetworkPublishPorts
|
|
|
|
}
|
|
|
|
|
|
|
|
if hc.NetworkMode.IsContainer() && len(c.ExposedPorts) > 0 {
|
|
|
|
return ErrConflictNetworkExposePorts
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|