首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >docker命令行列表参数

问docker命令行列表参数
EN

Stack Overflow用户
提问于 2015-03-14 02:04:29
回答 1查看 902关注 0票数 0

当我启动docker守护进程时,我正在修改dns服务器,以便容器具有修改后的/etc/resolv.conf。查看我看到的用法消息:

代码语言:javascript
复制
$ 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是我想要传递的,它显示了一个带有[]的'list‘,在经过多次尝试和错误之后,我终于让它起作用了:

代码语言:javascript
复制
--dns 127.0.0.1 --dns 8.8.8.8

哪些存款:

代码语言:javascript
复制
nameserver 127.0.0.1
nameserver 8.8.8.8

添加到/etc/resolv.conf文件中。

这是向docker (以及任何go)程序提供列表的正确方式吗?

EN

回答 1

Stack Overflow用户

发布于 2015-10-12 06:11:52

这是一种在Go中向程序传递多个参数的方法,但肯定不是唯一的方法。这是通过定义实现Value接口的类型来实现的。flag.Parse()上的flag包遍历参数列表,将名称与已注册的Value匹配,并在Value上调用Set(string)函数。您可以使用它将给定名称的每个值附加到切片。

代码语言:javascript
复制
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命令行参数,这些参数被转换为整数并添加到列表中。

代码语言:javascript
复制
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语言中没有既定的约定,每个应用程序都可以提供最佳匹配的任何实现。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29039031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档