mirror of https://github.com/docker/docs.git
dockerinit: set hostname
Set the hostname in dockerinit instead of with lxc utils. libvirt-lxc doesn't have a way to do this, so do it in a common place.
This commit is contained in:
parent
8224e13bd2
commit
f7c7f7978c
|
@ -6,13 +6,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const LxcTemplate = `
|
const LxcTemplate = `
|
||||||
# hostname
|
|
||||||
{{if .Config.Hostname}}
|
|
||||||
lxc.utsname = {{.Config.Hostname}}
|
|
||||||
{{else}}
|
|
||||||
lxc.utsname = {{.Id}}
|
|
||||||
{{end}}
|
|
||||||
|
|
||||||
{{if .Config.NetworkDisabled}}
|
{{if .Config.NetworkDisabled}}
|
||||||
# network is disabled (-n=false)
|
# network is disabled (-n=false)
|
||||||
lxc.network.type = empty
|
lxc.network.type = empty
|
||||||
|
|
|
@ -29,7 +29,6 @@ func TestLXCConfig(t *testing.T) {
|
||||||
container := &Container{
|
container := &Container{
|
||||||
root: root,
|
root: root,
|
||||||
Config: &Config{
|
Config: &Config{
|
||||||
Hostname: "foobar",
|
|
||||||
Memory: int64(mem),
|
Memory: int64(mem),
|
||||||
CpuShares: int64(cpu),
|
CpuShares: int64(cpu),
|
||||||
NetworkDisabled: true,
|
NetworkDisabled: true,
|
||||||
|
@ -41,7 +40,6 @@ func TestLXCConfig(t *testing.T) {
|
||||||
if err := container.generateLXCConfig(); err != nil {
|
if err := container.generateLXCConfig(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
grepFile(t, container.lxcConfigPath(), "lxc.utsname = foobar")
|
|
||||||
grepFile(t, container.lxcConfigPath(),
|
grepFile(t, container.lxcConfigPath(),
|
||||||
fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
|
fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
|
||||||
grepFile(t, container.lxcConfigPath(),
|
grepFile(t, container.lxcConfigPath(),
|
||||||
|
|
|
@ -26,6 +26,14 @@ type DockerInitArgs struct {
|
||||||
args []string
|
args []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setupHostname(args *DockerInitArgs) error {
|
||||||
|
hostname := getEnv(args, "HOSTNAME")
|
||||||
|
if hostname == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return syscall.Sethostname([]byte(hostname))
|
||||||
|
}
|
||||||
|
|
||||||
// Setup networking
|
// Setup networking
|
||||||
func setupNetworking(args *DockerInitArgs) error {
|
func setupNetworking(args *DockerInitArgs) error {
|
||||||
if args.gateway == "" {
|
if args.gateway == "" {
|
||||||
|
@ -132,9 +140,23 @@ func setupEnv(args *DockerInitArgs) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getEnv(args *DockerInitArgs, key string) string {
|
||||||
|
for _, kv := range args.env {
|
||||||
|
parts := strings.SplitN(kv, "=", 2)
|
||||||
|
if parts[0] == key && len(parts) == 2 {
|
||||||
|
return parts[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func executeProgram(args *DockerInitArgs) error {
|
func executeProgram(args *DockerInitArgs) error {
|
||||||
setupEnv(args)
|
setupEnv(args)
|
||||||
|
|
||||||
|
if err := setupHostname(args); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if err := setupNetworking(args); err != nil {
|
if err := setupNetworking(args); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue