mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Update host networking with hostname and files
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
parent
a785882b29
commit
5ca6532011
2 changed files with 37 additions and 36 deletions
|
@ -343,7 +343,7 @@ func populateCommand(c *Container, env []string) error {
|
||||||
case "none":
|
case "none":
|
||||||
case "host":
|
case "host":
|
||||||
en.HostNetworking = true
|
en.HostNetworking = true
|
||||||
case "bridge":
|
case "bridge", "": // empty string to support existing containers
|
||||||
if !c.Config.NetworkDisabled {
|
if !c.Config.NetworkDisabled {
|
||||||
network := c.NetworkSettings
|
network := c.NetworkSettings
|
||||||
en.Interface = &execdriver.NetworkInterface{
|
en.Interface = &execdriver.NetworkInterface{
|
||||||
|
@ -503,9 +503,18 @@ func (container *Container) StderrLogPipe() io.ReadCloser {
|
||||||
return utils.NewBufReader(reader)
|
return utils.NewBufReader(reader)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (container *Container) buildHostnameAndHostsFiles(IP string) {
|
func (container *Container) buildHostname() {
|
||||||
container.HostnamePath = path.Join(container.root, "hostname")
|
container.HostnamePath = path.Join(container.root, "hostname")
|
||||||
ioutil.WriteFile(container.HostnamePath, []byte(container.Config.Hostname+"\n"), 0644)
|
|
||||||
|
if container.Config.Domainname != "" {
|
||||||
|
ioutil.WriteFile(container.HostnamePath, []byte(fmt.Sprintf("%s.%s\n", container.Config.Hostname, container.Config.Domainname)), 0644)
|
||||||
|
} else {
|
||||||
|
ioutil.WriteFile(container.HostnamePath, []byte(container.Config.Hostname+"\n"), 0644)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (container *Container) buildHostnameAndHostsFiles(IP string) {
|
||||||
|
container.buildHostname()
|
||||||
|
|
||||||
hostsContent := []byte(`
|
hostsContent := []byte(`
|
||||||
127.0.0.1 localhost
|
127.0.0.1 localhost
|
||||||
|
@ -523,12 +532,11 @@ ff02::2 ip6-allrouters
|
||||||
} else if !container.Config.NetworkDisabled {
|
} else if !container.Config.NetworkDisabled {
|
||||||
hostsContent = append([]byte(fmt.Sprintf("%s\t%s\n", IP, container.Config.Hostname)), hostsContent...)
|
hostsContent = append([]byte(fmt.Sprintf("%s\t%s\n", IP, container.Config.Hostname)), hostsContent...)
|
||||||
}
|
}
|
||||||
|
|
||||||
ioutil.WriteFile(container.HostsPath, hostsContent, 0644)
|
ioutil.WriteFile(container.HostsPath, hostsContent, 0644)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (container *Container) allocateNetwork() error {
|
func (container *Container) allocateNetwork() error {
|
||||||
if container.Config.NetworkDisabled {
|
if container.Config.NetworkDisabled || container.hostConfig.NetworkMode == "host" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -981,14 +989,22 @@ func (container *Container) setupContainerDns() error {
|
||||||
if container.ResolvConfPath != "" {
|
if container.ResolvConfPath != "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
config = container.hostConfig
|
config = container.hostConfig
|
||||||
daemon = container.daemon
|
daemon = container.daemon
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if config.NetworkMode == "host" {
|
||||||
|
container.ResolvConfPath = "/etc/resolv.conf"
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
resolvConf, err := utils.GetResolvConf()
|
resolvConf, err := utils.GetResolvConf()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// If custom dns exists, then create a resolv.conf for the container
|
// If custom dns exists, then create a resolv.conf for the container
|
||||||
if len(config.Dns) > 0 || len(daemon.config.Dns) > 0 || len(config.DnsSearch) > 0 || len(daemon.config.DnsSearch) > 0 {
|
if len(config.Dns) > 0 || len(daemon.config.Dns) > 0 || len(config.DnsSearch) > 0 || len(daemon.config.DnsSearch) > 0 {
|
||||||
var (
|
var (
|
||||||
|
@ -1028,7 +1044,22 @@ func (container *Container) setupContainerDns() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (container *Container) initializeNetworking() error {
|
func (container *Container) initializeNetworking() error {
|
||||||
if container.daemon.config.DisableNetwork {
|
var err error
|
||||||
|
if container.hostConfig.NetworkMode == "host" {
|
||||||
|
container.Config.Hostname, err = os.Hostname()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.SplitN(container.Config.Hostname, ".", 2)
|
||||||
|
if len(parts) > 1 {
|
||||||
|
container.Config.Hostname = parts[0]
|
||||||
|
container.Config.Domainname = parts[1]
|
||||||
|
}
|
||||||
|
container.HostsPath = "/etc/hosts"
|
||||||
|
|
||||||
|
container.buildHostname()
|
||||||
|
} else if container.daemon.config.DisableNetwork {
|
||||||
container.Config.NetworkDisabled = true
|
container.Config.NetworkDisabled = true
|
||||||
container.buildHostnameAndHostsFiles("127.0.1.1")
|
container.buildHostnameAndHostsFiles("127.0.1.1")
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -22,33 +22,3 @@ func TestParseLxcConfOpt(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseNetMode(t *testing.T) {
|
|
||||||
testFlags := []struct {
|
|
||||||
flag string
|
|
||||||
mode string
|
|
||||||
container string
|
|
||||||
err bool
|
|
||||||
}{
|
|
||||||
{"", "", "", true},
|
|
||||||
{"bridge", "bridge", "", false},
|
|
||||||
{"disable", "disable", "", false},
|
|
||||||
{"container:foo", "container", "foo", false},
|
|
||||||
{"container:", "", "", true},
|
|
||||||
{"container", "", "", true},
|
|
||||||
{"unknown", "", "", true},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, to := range testFlags {
|
|
||||||
mode, err := parseNetMode(to.flag)
|
|
||||||
if mode != to.mode {
|
|
||||||
t.Fatalf("-net %s: expected net mode: %q, got: %q", to.flag, to.mode, mode)
|
|
||||||
}
|
|
||||||
if container != to.container {
|
|
||||||
t.Fatalf("-net %s: expected net container: %q, got: %q", to.flag, to.container, container)
|
|
||||||
}
|
|
||||||
if (err != nil) != to.err {
|
|
||||||
t.Fatal("-net %s: expected an error got none", to.flag)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue