2018-02-05 21:05:59 +00:00
|
|
|
package runconfig // import "github.com/docker/docker/runconfig"
|
2014-02-12 04:04:39 +00:00
|
|
|
|
|
|
|
import (
|
2015-04-11 00:05:21 +00:00
|
|
|
"io"
|
2017-03-01 04:03:43 +00:00
|
|
|
"strings"
|
2014-05-02 23:59:28 +00:00
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2023-09-10 11:33:21 +00:00
|
|
|
"github.com/docker/docker/api/types/network"
|
2014-02-12 04:04:39 +00:00
|
|
|
)
|
|
|
|
|
2015-07-25 09:11:45 +00: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 14:20:13 +00:00
|
|
|
func decodeHostConfig(src io.Reader) (*container.HostConfig, error) {
|
2015-05-14 14:39:44 +00:00
|
|
|
var w ContainerConfigWrapper
|
2022-04-08 21:27:50 +00:00
|
|
|
if err := loadJSON(src, &w); err != nil {
|
2015-04-11 00:05:21 +00:00
|
|
|
return nil, err
|
2014-07-10 21:51:15 +00:00
|
|
|
}
|
2021-08-09 09:11:54 +00:00
|
|
|
return w.getHostConfig(), nil
|
2014-02-12 04:04:39 +00:00
|
|
|
}
|
2015-07-09 22:12:36 +00: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-02-01 02:03:51 +00:00
|
|
|
func SetDefaultNetModeIfBlank(hc *container.HostConfig) {
|
2021-08-09 09:11:54 +00:00
|
|
|
if hc != nil && hc.NetworkMode == "" {
|
2023-09-10 11:33:21 +00:00
|
|
|
hc.NetworkMode = network.NetworkDefault
|
2015-07-09 22:12:36 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-01 04:03:43 +00:00
|
|
|
|
2017-03-10 17:39:22 +00:00
|
|
|
// validateNetContainerMode ensures that the various combinations of requested
|
2017-03-01 04:03:43 +00:00
|
|
|
// network settings wrt container mode are valid.
|
2017-03-10 17:39:22 +00:00
|
|
|
func validateNetContainerMode(c *container.Config, hc *container.HostConfig) error {
|
2017-03-01 04:03:43 +00:00
|
|
|
parts := strings.Split(string(hc.NetworkMode), ":")
|
|
|
|
if parts[0] == "container" {
|
|
|
|
if len(parts) < 2 || parts[1] == "" {
|
2017-07-19 14:20:13 +00:00
|
|
|
return validationError("Invalid network mode: invalid container format container:<name|id>")
|
2017-03-01 04:03:43 +00: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
|
|
|
|
}
|
|
|
|
|
2017-09-11 18:55:05 +00:00
|
|
|
if hc.NetworkMode.IsContainer() && (len(hc.PortBindings) > 0 || hc.PublishAllPorts) {
|
2017-03-01 04:03:43 +00:00
|
|
|
return ErrConflictNetworkPublishPorts
|
|
|
|
}
|
|
|
|
|
|
|
|
if hc.NetworkMode.IsContainer() && len(c.ExposedPorts) > 0 {
|
|
|
|
return ErrConflictNetworkExposePorts
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|