mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Add -dns to docker daemon
This commit is contained in:
parent
068076f775
commit
84d68007cb
5 changed files with 30 additions and 15 deletions
12
builder.go
12
builder.go
|
@ -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 {
|
if len(config.Dns) > 0 || len(builder.runtime.Dns) > 0 {
|
||||||
|
var dns []string
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,7 +70,7 @@ type progressReader struct {
|
||||||
readProgress int // How much has been read so far (bytes)
|
readProgress int // How much has been read so far (bytes)
|
||||||
lastUpdate int // How many bytes read at least update
|
lastUpdate int // How many bytes read at least update
|
||||||
template string // Template to print. Default "%v/%v (%v)"
|
template string // Template to print. Default "%v/%v (%v)"
|
||||||
sf *StreamFormatter
|
sf *StreamFormatter
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *progressReader) Read(p []byte) (n int, err error) {
|
func (r *progressReader) Read(p []byte) (n int, err error) {
|
||||||
|
@ -103,7 +103,7 @@ func (r *progressReader) Close() error {
|
||||||
return io.ReadCloser(r.reader).Close()
|
return io.ReadCloser(r.reader).Close()
|
||||||
}
|
}
|
||||||
func ProgressReader(r io.ReadCloser, size int, output io.Writer, template []byte, sf *StreamFormatter) *progressReader {
|
func ProgressReader(r io.ReadCloser, size int, output io.Writer, template []byte, sf *StreamFormatter) *progressReader {
|
||||||
tpl := string(template)
|
tpl := string(template)
|
||||||
if tpl == "" {
|
if tpl == "" {
|
||||||
tpl = string(sf.FormatProgress("", "%v/%v (%v)"))
|
tpl = string(sf.FormatProgress("", "%v/%v (%v)"))
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -597,7 +597,7 @@ func (sf *StreamFormatter) FormatStatus(format string, a ...interface{}) []byte
|
||||||
func (sf *StreamFormatter) FormatError(err error) []byte {
|
func (sf *StreamFormatter) FormatError(err error) []byte {
|
||||||
sf.used = true
|
sf.used = true
|
||||||
if sf.json {
|
if sf.json {
|
||||||
if b, err := json.Marshal(&JSONMessage{Error:err.Error()}); err == nil {
|
if b, err := json.Marshal(&JSONMessage{Error: err.Error()}); err == nil {
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
return []byte("{\"error\":\"format error\"}")
|
return []byte("{\"error\":\"format error\"}")
|
||||||
|
@ -608,10 +608,10 @@ func (sf *StreamFormatter) FormatError(err error) []byte {
|
||||||
func (sf *StreamFormatter) FormatProgress(action, str string) []byte {
|
func (sf *StreamFormatter) FormatProgress(action, str string) []byte {
|
||||||
sf.used = true
|
sf.used = true
|
||||||
if sf.json {
|
if sf.json {
|
||||||
b, err := json.Marshal(&JSONMessage{Status: action, Progress:str})
|
b, err := json.Marshal(&JSONMessage{Status: action, Progress: str})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
return []byte(action + " " + str + "\r")
|
return []byte(action + " " + str + "\r")
|
||||||
|
|
Loading…
Add table
Reference in a new issue