diff --git a/container.go b/container.go index 5122fd7fcf..d4e8c65f0e 100644 --- a/container.go +++ b/container.go @@ -11,6 +11,7 @@ import ( "io" "io/ioutil" "log" + "net" "os" "os/exec" "path" @@ -19,7 +20,6 @@ import ( "strings" "syscall" "time" - "net" ) type Container struct { @@ -41,6 +41,8 @@ type Container struct { SysInitPath string ResolvConfPath string + HostnamePath string + HostsPath string cmd *exec.Cmd stdout *utils.WriteBroadcaster @@ -60,6 +62,7 @@ type Container struct { type Config struct { Hostname string + Domainname string User string Memory int64 // Memory limit (in bytes) MemorySwap int64 // Total memory usage (memory + swap); set `-1' to disable swap @@ -202,8 +205,17 @@ func ParseRun(args []string, capabilities *Capabilities) (*Config, *HostConfig, return nil, nil, cmd, err } + hostname := *flHostname + domainname := "" + + parts := strings.SplitN(hostname, ".", 2) + if len(parts) > 1 { + hostname = parts[0] + domainname = parts[1] + } config := &Config{ - Hostname: *flHostname, + Hostname: hostname, + Domainname: domainname, PortSpecs: flPorts, User: *flUser, Tty: *flTty, @@ -823,10 +835,10 @@ func (container *Container) allocateNetwork() error { iface = &NetworkInterface{disabled: true} } else { iface = &NetworkInterface{ - IPNet: net.IPNet{IP: net.ParseIP(container.NetworkSettings.IPAddress), Mask: manager.bridgeNetwork.Mask}, + IPNet: net.IPNet{IP: net.ParseIP(container.NetworkSettings.IPAddress), Mask: manager.bridgeNetwork.Mask}, Gateway: manager.bridgeNetwork.IP, manager: manager, - } + } ipNum := ipToInt(iface.IPNet.IP) manager.ipAllocator.inUse[ipNum] = struct{}{} } @@ -837,10 +849,10 @@ func (container *Container) allocateNetwork() error { portSpecs = container.Config.PortSpecs } else { for backend, frontend := range container.NetworkSettings.PortMapping["Tcp"] { - portSpecs = append(portSpecs, fmt.Sprintf("%s:%s/tcp",frontend, backend)) + portSpecs = append(portSpecs, fmt.Sprintf("%s:%s/tcp", frontend, backend)) } for backend, frontend := range container.NetworkSettings.PortMapping["Udp"] { - portSpecs = append(portSpecs, fmt.Sprintf("%s:%s/udp",frontend, backend)) + portSpecs = append(portSpecs, fmt.Sprintf("%s:%s/udp", frontend, backend)) } } diff --git a/graph.go b/graph.go index c54725fdb4..ffbb3f6a6a 100644 --- a/graph.go +++ b/graph.go @@ -202,6 +202,8 @@ func (graph *Graph) getDockerInitLayer() (string, error) { "/sys": "dir", "/.dockerinit": "file", "/etc/resolv.conf": "file", + "/etc/hosts": "file", + "/etc/hostname": "file", // "var/run": "dir", // "var/lock": "dir", } { diff --git a/lxc_template.go b/lxc_template.go index d357c02b43..7013581232 100644 --- a/lxc_template.go +++ b/lxc_template.go @@ -30,6 +30,10 @@ lxc.network.ipv4 = {{.NetworkSettings.IPAddress}}/{{.NetworkSettings.IPPrefixLen {{$ROOTFS := .RootfsPath}} lxc.rootfs = {{$ROOTFS}} +# enable domain name support +lxc.mount.entry = {{.HostnamePath}} {{$ROOTFS}}/etc/hostname none bind,ro 0 0 +lxc.mount.entry = {{.HostsPath}} {{$ROOTFS}}/etc/hosts none bind,ro 0 0 + # use a dedicated pts for the container (and limit the number of pseudo terminal # available) lxc.pts = 1024 diff --git a/runtime.go b/runtime.go index 1ee7aa26f9..aff1773fdf 100644 --- a/runtime.go +++ b/runtime.go @@ -366,7 +366,33 @@ func (runtime *Runtime) Create(config *Config) (*Container, error) { if err := container.ToDisk(); err != nil { return nil, err } - // Step 3: register the container + + // Step 3: if hostname, build hostname and hosts files + container.HostnamePath = path.Join(container.root, "hostname") + ioutil.WriteFile(container.HostnamePath, []byte(container.Config.Hostname+"\n"), 0644) + + hostsContent := []byte(` +127.0.0.1 localhost +::1 localhost ip6-localhost ip6-loopback +fe00::0 ip6-localnet +ff00::0 ip6-mcastprefix +ff02::1 ip6-allnodes +ff02::2 ip6-allrouters +`) + + container.HostsPath = path.Join(container.root, "hosts") + + if container.Config.Domainname != "" { + hostsContent = append([]byte(fmt.Sprintf("::1\t\t%s.%s %s\n", container.Config.Hostname, container.Config.Domainname, container.Config.Hostname)), hostsContent...) + hostsContent = append([]byte(fmt.Sprintf("127.0.0.1\t%s.%s %s\n", container.Config.Hostname, container.Config.Domainname, container.Config.Hostname)), hostsContent...) + } else { + hostsContent = append([]byte(fmt.Sprintf("::1\t\t%s\n", container.Config.Hostname)), hostsContent...) + hostsContent = append([]byte(fmt.Sprintf("127.0.0.1\t%s\n", container.Config.Hostname)), hostsContent...) + } + + ioutil.WriteFile(container.HostsPath, hostsContent, 0644) + + // Step 4: register the container if err := runtime.Register(container); err != nil { return nil, err } @@ -489,4 +515,3 @@ func (history *History) Add(container *Container) { *history = append(*history, container) sort.Sort(history) } -