douzi8548 2015-03-13 18:04
浏览 138

docker命令行列表参数

When I am starting the docker daemon I am modifying the dns server so that the containers have a modified /etc/resolv.conf. Looking at the usage message I see:

$ 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 ...

The --dns is what I want to pass, it shows a 'list' with the [], which after much trial and error I finally got this to work:

--dns 127.0.0.1 --dns 8.8.8.8

which deposits :

nameserver 127.0.0.1
nameserver 8.8.8.8

in to the /etc/resolv.conf file.

Is this the correct way to provide a list to docker (and presumably any go) program?

  • 写回答

1条回答 默认 最新

  • duanjucong3124 2015-10-11 22:11
    关注

    This is a way of passing multiple arguments to a program in Go but certainly not the only way. This is accomplished by defining a type that implements the Value interface. The flag package on flag.Parse() iterates though the argument list matching the name to a registered Value and calling the Set(string) function on the Value. You can use this to append each value of a given name to a slice.

    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
    }
    

    This new type can be registered as a flag variable. In the following example the application takes n num command line arguments that are converted to an integer and added to a list.

    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.
    ", sum)
    }
    

    This could have been easily done with a string flag and have users pass a delimited list. There is no established convention within the Go language and each application can provide whatever implementation of best fit.

    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料