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?

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.