Procházet zdrojové kódy

Allow use to set his own dns via -dns

Guillaume J. Charmes před 12 roky
rodič
revize
7673afc843
2 změnil soubory, kde provedl 24 přidání a 4 odebrání
  1. 5 0
      container.go
  2. 19 4
      runtime.go

+ 5 - 0
container.go

@@ -62,6 +62,7 @@ type Config struct {
 	StdinOnce    bool // If true, close stdin after the 1 attached client disconnects.
 	Env          []string
 	Cmd          []string
+	Dns          []string
 	Image        string // Name of the image as it was passed by the operator (eg. could be symbolic)
 }
 
@@ -86,6 +87,9 @@ func ParseRun(args []string, stdout io.Writer) (*Config, error) {
 	var flEnv ListOpts
 	cmd.Var(&flEnv, "e", "Set environment variables")
 
+	var flDns ListOpts
+	cmd.Var(&flDns, "dns", "Set custom dns servers")
+
 	if err := cmd.Parse(args); err != nil {
 		return nil, err
 	}
@@ -123,6 +127,7 @@ func ParseRun(args []string, stdout io.Writer) (*Config, error) {
 		AttachStderr: flAttach.Get("stderr"),
 		Env:          flEnv,
 		Cmd:          runCmd,
+		Dns:          flDns,
 		Image:        image,
 	}
 	// When allocating stdin in attached mode, close stdin at client disconnect

+ 19 - 4
runtime.go

@@ -83,8 +83,6 @@ func (runtime *Runtime) Create(config *Config) (*Container, error) {
 		config.Hostname = id[:12]
 	}
 
-	resolvConfPath := "/etc/resolv.conf"
-
 	container := &Container{
 		// FIXME: we should generate the ID here instead of receiving it as an argument
 		Id:              id,
@@ -95,8 +93,7 @@ func (runtime *Runtime) Create(config *Config) (*Container, error) {
 		Image:           img.Id, // Always use the resolved image id
 		NetworkSettings: &NetworkSettings{},
 		// FIXME: do we need to store this in the container?
-		SysInitPath:    sysInitPath,
-		ResolvConfPath: resolvConfPath,
+		SysInitPath: sysInitPath,
 	}
 	container.root = runtime.containerRoot(container.Id)
 	// Step 1: create the container directory.
@@ -104,6 +101,24 @@ func (runtime *Runtime) Create(config *Config) (*Container, error) {
 	if err := os.Mkdir(container.root, 0700); err != nil {
 		return nil, err
 	}
+
+	// If custom dns exists, then create a resolv.conf for the container
+	if len(config.Dns) > 0 {
+		container.ResolvConfPath = path.Join(container.root, "resolv.conf")
+		f, err := os.Create(container.ResolvConfPath)
+		if err != nil {
+			return nil, err
+		}
+		defer f.Close()
+		for _, dns := range config.Dns {
+			if _, err := f.Write([]byte("nameserver " + dns + "\n")); err != nil {
+				return nil, err
+			}
+		}
+	} else {
+		container.ResolvConfPath = "/etc/resolv.conf"
+	}
+
 	// Step 2: save the container json
 	if err := container.ToDisk(); err != nil {
 		return nil, err