diff --git a/builder.go b/builder.go index 9124f76ac1..5ccce8293e 100644 --- a/builder.go +++ b/builder.go @@ -3,6 +3,7 @@ package docker import ( "fmt" "github.com/dotcloud/docker/utils" + "io/ioutil" "os" "path" "time" @@ -119,7 +120,31 @@ func (builder *Builder) 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\tlocalhost\n" + + "::1\t\tlocalhost ip6-localhost ip6-loopback\n" + + "fe00::0\t\tip6-localnet\n" + + "ff00::0\t\tip6-mcastprefix\n" + + "ff02::1\t\tip6-allnodes\n" + + "ff02::2\t\tip6-allrouters\n") + + container.HostsPath = path.Join(container.root, "hosts") + + if container.Config.Domainname != "" { + hostsContent = append([]byte("127.0.0.1\t"+container.Config.Hostname+"."+container.Config.Domainname+" "+container.Config.Hostname+"\n"+ + "::1\t\t"+container.Config.Hostname+"."+container.Config.Domainname+" "+container.Config.Hostname+"\n"), hostsContent...) + } else { + hostsContent = append([]byte("127.0.0.1\t"+container.Config.Hostname+"\n"+ + "::1\t\t"+container.Config.Hostname+"\n"), hostsContent...) + } + + ioutil.WriteFile(container.HostsPath, hostsContent, 0644) + + // Step 4: register the container if err := builder.runtime.Register(container); err != nil { return nil, err } diff --git a/container.go b/container.go index 488cba3229..ecbf8e561f 100644 --- a/container.go +++ b/container.go @@ -42,6 +42,8 @@ type Container struct { SysInitPath string ResolvConfPath string + HostnamePath string + HostsPath string cmd *exec.Cmd stdout *utils.WriteBroadcaster @@ -702,9 +704,6 @@ func (container *Container) Start(hostConfig *HostConfig) error { params = append(params, "-e", "TERM=xterm") } - params = append(params, "-h", container.Config.Hostname) - params = append(params, "-d", container.Config.Domainname) - // Setup environment params = append(params, "-e", "HOME=/", 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/sysinit.go b/sysinit.go index fe741382c3..34f1cbdac6 100644 --- a/sysinit.go +++ b/sysinit.go @@ -4,7 +4,6 @@ import ( "flag" "fmt" "github.com/dotcloud/docker/utils" - "io/ioutil" "log" "os" "os/exec" @@ -93,8 +92,7 @@ func SysInit() { var u = flag.String("u", "", "username or uid") var gw = flag.String("g", "", "gateway address") var workdir = flag.String("w", "", "workdir") - var hostname = flag.String("h", "", "hostname") - var domainname = flag.String("d", "", "domainname") + var flEnv ListOpts flag.Var(&flEnv, "e", "Set environment variables") @@ -103,7 +101,6 @@ func SysInit() { cleanupEnv(flEnv) setupNetworking(*gw) setupWorkingDirectory(*workdir) - setupHostname(*hostname, *domainname) changeUser(*u) executeProgram(flag.Arg(0), flag.Args()) }