Add -dns to docker daemon

This commit is contained in:
Guillaume J. Charmes 2013-06-05 14:20:19 -07:00
parent 068076f775
commit 84d68007cb
5 changed files with 30 additions and 15 deletions

View File

@ -7,6 +7,8 @@ import (
"time" "time"
) )
var defaultDns = []string{"8.8.8.8", "8.8.4.4"}
type Builder struct { type Builder struct {
runtime *Runtime runtime *Runtime
repositories *TagStore repositories *TagStore
@ -67,14 +69,20 @@ func (builder *Builder) Create(config *Config) (*Container, error) {
} }
// If custom dns exists, then create a resolv.conf for the container // If custom dns exists, then create a resolv.conf for the container
if len(config.Dns) > 0 || len(builder.runtime.Dns) > 0 {
var dns []string
if len(config.Dns) > 0 { if len(config.Dns) > 0 {
dns = config.Dns
} else {
dns = builder.runtime.Dns
}
container.ResolvConfPath = path.Join(container.root, "resolv.conf") container.ResolvConfPath = path.Join(container.root, "resolv.conf")
f, err := os.Create(container.ResolvConfPath) f, err := os.Create(container.ResolvConfPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer f.Close() defer f.Close()
for _, dns := range config.Dns { for _, dns := range dns {
if _, err := f.Write([]byte("nameserver " + dns + "\n")); err != nil { if _, err := f.Write([]byte("nameserver " + dns + "\n")); err != nil {
return nil, err return nil, err
} }

View File

@ -33,6 +33,11 @@ func main() {
bridgeName := flag.String("b", "", "Attach containers to a pre-existing network bridge") bridgeName := flag.String("b", "", "Attach containers to a pre-existing network bridge")
pidfile := flag.String("p", "/var/run/docker.pid", "File containing process PID") pidfile := flag.String("p", "/var/run/docker.pid", "File containing process PID")
flHost := flag.String("H", fmt.Sprintf("%s:%d", host, port), "Host:port to bind/connect to") flHost := flag.String("H", fmt.Sprintf("%s:%d", host, port), "Host:port to bind/connect to")
flags := flag.NewFlagSet("docker", flag.ContinueOnError)
var flDns docker.ListOpts
flags.Var(&flDns, "dns", "Set custom dns servers")
flag.Parse() flag.Parse()
if *bridgeName != "" { if *bridgeName != "" {
docker.NetworkBridgeIface = *bridgeName docker.NetworkBridgeIface = *bridgeName
@ -65,7 +70,7 @@ func main() {
flag.Usage() flag.Usage()
return return
} }
if err := daemon(*pidfile, host, port, *flAutoRestart); err != nil { if err := daemon(*pidfile, host, port, *flAutoRestart, flDns); err != nil {
log.Fatal(err) log.Fatal(err)
os.Exit(-1) os.Exit(-1)
} }
@ -104,7 +109,7 @@ func removePidFile(pidfile string) {
} }
} }
func daemon(pidfile, addr string, port int, autoRestart bool) error { func daemon(pidfile, addr string, port int, autoRestart bool, flDns docker.ListOpts) error {
if addr != "127.0.0.1" { if addr != "127.0.0.1" {
log.Println("/!\\ DON'T BIND ON ANOTHER IP ADDRESS THAN 127.0.0.1 IF YOU DON'T KNOW WHAT YOU'RE DOING /!\\") log.Println("/!\\ DON'T BIND ON ANOTHER IP ADDRESS THAN 127.0.0.1 IF YOU DON'T KNOW WHAT YOU'RE DOING /!\\")
} }
@ -122,7 +127,7 @@ func daemon(pidfile, addr string, port int, autoRestart bool) error {
os.Exit(0) os.Exit(0)
}() }()
server, err := docker.NewServer(autoRestart) server, err := docker.NewServer(autoRestart, flDns)
if err != nil { if err != nil {
return err return err
} }

View File

@ -32,6 +32,7 @@ type Runtime struct {
autoRestart bool autoRestart bool
volumes *Graph volumes *Graph
srv *Server srv *Server
Dns []string
} }
var sysInitPath string var sysInitPath string
@ -245,11 +246,12 @@ func (runtime *Runtime) UpdateCapabilities(quiet bool) {
} }
// FIXME: harmonize with NewGraph() // FIXME: harmonize with NewGraph()
func NewRuntime(autoRestart bool) (*Runtime, error) { func NewRuntime(autoRestart bool, dns []string) (*Runtime, error) {
runtime, err := NewRuntimeFromDirectory("/var/lib/docker", autoRestart) runtime, err := NewRuntimeFromDirectory("/var/lib/docker", autoRestart)
if err != nil { if err != nil {
return nil, err return nil, err
} }
runtime.Dns = dns
if k, err := utils.GetKernelVersion(); err != nil { if k, err := utils.GetKernelVersion(); err != nil {
log.Printf("WARNING: %s\n", err) log.Printf("WARNING: %s\n", err)

View File

@ -869,11 +869,11 @@ func (srv *Server) ImageInspect(name string) (*Image, error) {
return nil, fmt.Errorf("No such image: %s", name) return nil, fmt.Errorf("No such image: %s", name)
} }
func NewServer(autoRestart bool) (*Server, error) { func NewServer(autoRestart bool, dns ListOpts) (*Server, error) {
if runtime.GOARCH != "amd64" { if runtime.GOARCH != "amd64" {
log.Fatalf("The docker runtime currently only supports amd64 (not %s). This will change in the future. Aborting.", runtime.GOARCH) log.Fatalf("The docker runtime currently only supports amd64 (not %s). This will change in the future. Aborting.", runtime.GOARCH)
} }
runtime, err := NewRuntime(autoRestart) runtime, err := NewRuntime(autoRestart, dns)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -585,7 +585,7 @@ func (sf *StreamFormatter) FormatStatus(format string, a ...interface{}) []byte
sf.used = true sf.used = true
str := fmt.Sprintf(format, a...) str := fmt.Sprintf(format, a...)
if sf.json { if sf.json {
b, err := json.Marshal(&JSONMessage{Status:str}); b, err := json.Marshal(&JSONMessage{Status: str})
if err != nil { if err != nil {
return sf.FormatError(err) return sf.FormatError(err)
} }