2015-03-13 153 views
0

當我啓動docker守護進程時,我正在修改dns服務器,以便容器修改了/etc/resolv.conf。望着用法消息我看到:docker命令行參數

$ docker --help 
Usage: docker [OPTIONS] COMMAND [arg...] 

A self-sufficient runtime for linux containers. 

Options: 
    --api-enable-cors=false     Enable CORS headers in the remote API 
    -b, --bridge=""       Attach containers to a prexisting network bridge 
              use 'none' to disable container networking 
    --bip=""         Use this CIDR notation address for the network bridge's IP, not compatible with -b 
    -D, --debug=false       Enable debug mode 
    -d, --daemon=false       Enable daemon mode 
    --dns=[]         Force Docker to use specific DNS servers 
    --dns-search=[]       Force Docker to use specific DNS search domains 
    -e, --exec-driver="native"     Force the Docker runtime to use a specific exec driver 

... etc ... 

的--dns是我想傳遞的東西,它顯示了一個帶有[],其中大部分經過反覆試驗,我終於得到這個工作「清單」:

--dns 127.0.0.1 --dns 8.8.8.8 

其中存款:

nameserver 127.0.0.1 
nameserver 8.8.8.8 
到/etc/resolv.conf文件

這是向docker(大概是任何程序)提供列表的正確方法嗎?

+4

這是他們使用標誌包的方式。很多去程序使用標誌來做這樣的列表選項,但沒有理由他們不能使用引用列表或逗號分隔列表。 – JimB 2015-03-13 18:11:17

回答

0

這是一種將多個參數傳遞給Go程序的方法,但絕對不是唯一的方法。這是通過定義實現Value接口的類型來完成的。 flag.Parse()上的flag包通過與名稱匹配的參數列表迭代到登記的Value並在Value上調用Set(string)函數。您可以使用它將給定名稱的每個值附加到切片。

type numList []int 

func (l *numList) String() string { 
    return "[]" 
} 

func (l *numList) Set(value string) error { 
    number, err := strconv.Atoi(value) 

    if err != nil { 
     return fmt.Errorf("Unable to parse number from value \"%s\"", value) 
    } 

    *l = append(*l, number) 
    return nil 
} 

該新類型可以註冊爲標誌變量。在以下示例中,應用程序需要轉換爲整數並添加到列表中的命令行參數n num

var numbers numList 

func main() { 
    flag.Var(&numbers, "num", "A number to add to the summation" 
    flag.Parse() 

    sum := 0 
    for _, num := range numbers { 
     sum += num 
    } 

    fmt.Printf("The sum of your flag arguments is %d.\n", sum) 
} 

這可能很容易用字符串標記完成並讓用戶傳遞一個分隔列表。在Go語言中沒有建立慣例,每個應用程序都可以提供最適合的任何實現。