go - docker 命令行列表参数

标签 go docker

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

$ 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(可能还有任何 go)程序提供列表的正确方法吗?

最佳答案

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

这个新类型可以注册为标志变量。在以下示例中,应用程序采用 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 语言中没有既定的约定,每个应用程序都可以提供最适合的任何实现。

关于go - docker 命令行列表参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29039031/

相关文章:

go - 如何使用 golang 中的 api 编辑 GCP 云存储中的现有数据

go - 如何从客户端运行非事务管道。go-redis中的Watch函数

docker - 使用 --target 标志构建多阶段 Dockerfile 会构建所有阶段,而不仅仅是指定的阶段

docker - 从docker-compose容器内部访问主机VPN(laradock)

docker - 如何处理VSCode Remote dev容器中的多个 `network_mode`?

scala - 如何在测试开始前从 sbt 运行 docker 容器?

go - 构造奇怪的行为

go - 无法使用 dart 和 angular 2 客户端运行 Golang 后端

forms - Go - 用于多个文件的 formFile

linux - 不受信任的 Docker 容器会造成什么损害?