Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli change to pass driver specific options to docker run #156

Closed
wants to merge 0 commits into from

Conversation

abhi
Copy link
Contributor

@abhi abhi commented Jun 5, 2017

The commit contains cli changes to support driver options for a network in docker run and docker network connect cli's.
The driver-opt, aliases is now supported in the form of csv as per network option in service commands in swarm mode since #62 . This commit extends this support to docker run command as well.
For docker connect command --driver-opt is added to pass driver specific options for the network the container is connecting to.

Following is supported:

docker run -itd --net name=docknet,alias=web1.0,driver-opt=field1=value1 alpine
docker run -itd --net name=docknet,driver-opt=field1=value1 --net-alias=web1.0 alpine
docker run -itd --net name=docknet --net-alias web1 alpine
docker connect network --driver-opt field1=value1 --net-alias=web2.0 docknet2 dockcontainer

Signed-off-by: Abhinandan Prativadi [email protected]

cc @mavenugo

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/opts"
"github.com/docker/docker/api/types/network"
"github.com/spf13/cobra"
"golang.org/x/net/context"
"strings"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put fmt and strings first in the list, then a blank line before the non-stdlib imports.

parts := strings.SplitN(opt, "=", 2)
if len(parts) != 2 {
err := fmt.Errorf("invalid key value pair format in driver options")
return nil, err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return nil, fmt.Errorf("invalid key/value pair format in driver options")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps use errors.New("invalid key/value pair format in driver options"), instead of fmt.Errorf()

@@ -899,3 +921,12 @@ func validateAttach(val string) (string, error) {
}
return val, errors.Errorf("valid streams are STDIN, STDOUT and STDERR")
}

func convertToNetworkmode(netmode opts.NetworkOpt) container.NetworkMode {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding a NetworkMode method to opts.NetworkOpt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be useful but opts.NetworkOpt is shared between services and container (docker run) and involves different conversion methods.

networkIDOrName := "default"
for _, net := range netmode.Value() {
networkIDOrName = net.Target
break
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would instead write this as:

netOptVal := netmode.Value()
if len(netOptVal) > 0 {
        networkIDOrName = netOptVal[0].Target
}

But is it correct to silently ignore everything beyond the first value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docker run does not support multiple networks . if user does give multiple networks only the first network is considered.
So in the above code I decided to do the same as well to keep it in sync.
And if no network options is given then the ID would be "default"

Copy link
Member

@thaJeztah thaJeztah Jul 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docker run does not support multiple networks . if user does give multiple networks only the first network is considered.

The reason for only supporting a single network, was that there was no way to pass options per-network (also, --network-alias would be applied to all networks that you attached the container to) - also see moby/moby#31964, and issues linked from that.

If the API supports multiple networks on create, perhaps we should change that (can be a follow-up to this PR), as this should now be possible from a UX perspective;

docker run \
  --network name=docknet,alias=web1.0,driver-opt=field1=value1 \
  --network name=second-net,alias=foobar,driver-opt=hello=world \
  nginx:alpine

(edit: removed --driver-opt because there's no such thing 😄)

@fcrisciani
Copy link

@abhinandanpb can you address @aaronlehmann comments?

return nil, err
}
key := strings.TrimSpace(strings.ToLower(parts[0]))
value := strings.TrimSpace(strings.ToLower(parts[1]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are all options and values lowercase? If not needed, I'd rather not transform options that were passed by the user (in case some particular driver has options that are case sensitive)

Better to have the daemon return an error if an option is not valid

(I realize I didn't check the implementation for services, we may want to consider doing the same there)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I thought of that as well. But I saw that the other options like port were converting to lower case. So I decided to maintain the uniformity.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For port we probably have more control over the available options (although I'd be in favour of moving validation to the daemon as well); for plugins/drivers we have no knowledge whatsoever what's supported.

It should be easy to add this back in future if we want, more difficult to remove ("Docker 17.07 supported opt=foObAr and now produces an error!")

@thaJeztah
Copy link
Member

Following is supported:

docker run -itd --net name=docknet,alias=web1.0,driver-opt=field1=value1 alpine
docker run -itd --net name=docknet,driver-opt=field1=value1 --net-alias=web1.0 alpine
docker run -itd --net name=docknet --net-alias web1 alpine

Looks like I didn't look closely at those examples, some remarks;

  1. we "deprecated" --net in favour of --network. Although --net still works (for backward compatibility), would it be possible to only accept the csv syntax when using the --network flag, and report an error when using it for the --net flag? Trying to avoid adding new features to a "frozen" flag

  2. I wonder if we should disallow combining --net-alias and --network (csv notation), as the outcome may be ambiguous, thinking of what happens with;

docker run -itd --network name=docknet,alias=web1.0 --network-alias=foobar alpine

Would it be both web1.0 and foobar as alias? One of them? Perhaps it's okay, just wanted to put it out here for discussion

@abhi
Copy link
Contributor Author

abhi commented Jul 7, 2017

1. we "deprecated" --net in favour of --network. Although --net still works (for backward compatibility), would it be possible to only accept the csv syntax when using the --network flag, and report an error when using it for the --net flag? Trying to avoid adding new features to a "frozen" flag

Yes thats doable and makes sense as well. I can update the PR with the change.

I wonder if we should disallow combining --net-alias and --network (csv notation), as the outcome may be ambiguous, thinking of what happens with;

yes its disallowed. And I believe we should to avoid ambiguity.

@thaJeztah ^

@abhi
Copy link
Contributor Author

abhi commented Jul 10, 2017

@thaJeztah looks like it got closed because of another PR referring to it. Can you please reopen this one ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants