Browse code

Allow use to set his own dns via -dns

Guillaume J. Charmes authored on 2013/04/11 11:02:23
Showing 2 changed files
... ...
@@ -62,6 +62,7 @@ type Config struct {
62 62
 	StdinOnce    bool // If true, close stdin after the 1 attached client disconnects.
63 63
 	Env          []string
64 64
 	Cmd          []string
65
+	Dns          []string
65 66
 	Image        string // Name of the image as it was passed by the operator (eg. could be symbolic)
66 67
 }
67 68
 
... ...
@@ -86,6 +87,9 @@ func ParseRun(args []string, stdout io.Writer) (*Config, error) {
86 86
 	var flEnv ListOpts
87 87
 	cmd.Var(&flEnv, "e", "Set environment variables")
88 88
 
89
+	var flDns ListOpts
90
+	cmd.Var(&flDns, "dns", "Set custom dns servers")
91
+
89 92
 	if err := cmd.Parse(args); err != nil {
90 93
 		return nil, err
91 94
 	}
... ...
@@ -123,6 +127,7 @@ func ParseRun(args []string, stdout io.Writer) (*Config, error) {
123 123
 		AttachStderr: flAttach.Get("stderr"),
124 124
 		Env:          flEnv,
125 125
 		Cmd:          runCmd,
126
+		Dns:          flDns,
126 127
 		Image:        image,
127 128
 	}
128 129
 	// When allocating stdin in attached mode, close stdin at client disconnect
... ...
@@ -83,8 +83,6 @@ func (runtime *Runtime) Create(config *Config) (*Container, error) {
83 83
 		config.Hostname = id[:12]
84 84
 	}
85 85
 
86
-	resolvConfPath := "/etc/resolv.conf"
87
-
88 86
 	container := &Container{
89 87
 		// FIXME: we should generate the ID here instead of receiving it as an argument
90 88
 		Id:              id,
... ...
@@ -95,8 +93,7 @@ func (runtime *Runtime) Create(config *Config) (*Container, error) {
95 95
 		Image:           img.Id, // Always use the resolved image id
96 96
 		NetworkSettings: &NetworkSettings{},
97 97
 		// FIXME: do we need to store this in the container?
98
-		SysInitPath:    sysInitPath,
99
-		ResolvConfPath: resolvConfPath,
98
+		SysInitPath: sysInitPath,
100 99
 	}
101 100
 	container.root = runtime.containerRoot(container.Id)
102 101
 	// Step 1: create the container directory.
... ...
@@ -104,6 +101,24 @@ func (runtime *Runtime) Create(config *Config) (*Container, error) {
104 104
 	if err := os.Mkdir(container.root, 0700); err != nil {
105 105
 		return nil, err
106 106
 	}
107
+
108
+	// If custom dns exists, then create a resolv.conf for the container
109
+	if len(config.Dns) > 0 {
110
+		container.ResolvConfPath = path.Join(container.root, "resolv.conf")
111
+		f, err := os.Create(container.ResolvConfPath)
112
+		if err != nil {
113
+			return nil, err
114
+		}
115
+		defer f.Close()
116
+		for _, dns := range config.Dns {
117
+			if _, err := f.Write([]byte("nameserver " + dns + "\n")); err != nil {
118
+				return nil, err
119
+			}
120
+		}
121
+	} else {
122
+		container.ResolvConfPath = "/etc/resolv.conf"
123
+	}
124
+
107 125
 	// Step 2: save the container json
108 126
 	if err := container.ToDisk(); err != nil {
109 127
 		return nil, err