Skip to content

Commit

Permalink
Enhance docker network rm to delete multi net
Browse files Browse the repository at this point in the history
This commit enhance `docker network rm` command to allow user to delete
multi networks at the same time.

Signed-off-by: Zhang Wei <[email protected]>
  • Loading branch information
WeiZhang555 committed Nov 21, 2015
1 parent d8ea32c commit e7eb668
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 18 deletions.
27 changes: 18 additions & 9 deletions api/client/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,28 @@ func (cli *DockerCli) CmdNetworkCreate(args ...string) error {
return nil
}

// CmdNetworkRm deletes a network
// CmdNetworkRm deletes one or more networks
//
// Usage: docker network rm <NETWORK-NAME | NETWORK-ID>
// Usage: docker network rm NETWORK-NAME|NETWORK-ID [NETWORK-NAME|NETWORK-ID...]
func (cli *DockerCli) CmdNetworkRm(args ...string) error {
cmd := Cli.Subcmd("network rm", []string{"NETWORK"}, "Deletes a network", false)
cmd.Require(flag.Exact, 1)
cmd := Cli.Subcmd("network rm", []string{"NETWORK [NETWORK...]"}, "Deletes one or more networks", false)
cmd.Require(flag.Min, 1)
err := cmd.ParseFlags(args, true)
if err != nil {
return err
}
_, _, err = readBody(cli.call("DELETE", "/networks/"+cmd.Arg(0), nil, nil))
if err != nil {
return err

status := 0
for _, net := range cmd.Args() {
_, _, err = readBody(cli.call("DELETE", "/networks/"+net, nil, nil))
if err != nil {
fmt.Fprintf(cli.err, "%s\n", err)
status = 1
continue
}
}
if status != 0 {
return Cli.StatusError{StatusCode: status}
}
return nil
}
Expand Down Expand Up @@ -193,7 +202,7 @@ func (cli *DockerCli) CmdNetworkLs(args ...string) error {
//
// Usage: docker network inspect [OPTIONS] <NETWORK> [NETWORK...]
func (cli *DockerCli) CmdNetworkInspect(args ...string) error {
cmd := Cli.Subcmd("network inspect", []string{"NETWORK [NETWORK...]"}, "Displays detailed information on a network", false)
cmd := Cli.Subcmd("network inspect", []string{"NETWORK [NETWORK...]"}, "Displays detailed information on one or more networks", false)
cmd.Require(flag.Min, 1)
err := cmd.ParseFlags(args, true)
if err != nil {
Expand All @@ -208,7 +217,7 @@ func (cli *DockerCli) CmdNetworkInspect(args ...string) error {
if strings.Contains(err.Error(), "not found") {
fmt.Fprintf(cli.err, "Error: No such network: %s\n", name)
} else {
fmt.Fprintf(cli.err, "%s", err)
fmt.Fprintf(cli.err, "%s\n", err)
}
status = 1
continue
Expand Down
21 changes: 18 additions & 3 deletions docs/reference/commandline/network_rm.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,33 @@ parent = "smn_cli"

# network rm

Usage: docker network rm [OPTIONS] NAME | ID
Usage: docker network rm [OPTIONS] NETWORK [NETWORK...]

Deletes a network
Deletes one or more networks

--help=false Print usage

Removes a network by name or identifier. To remove a network, you must first disconnect any containers connected to it.
Removes one or more networks by name or identifier. To remove a network,
you must first disconnect any containers connected to it.
To remove the network named 'my-network':

```bash
$ docker network rm my-network
```

To delete multiple networks in a single `docker network rm` command, provide
multiple network names or id's. The following example deletes a network with id
`3695c422697f` and a network named `my-network`:

```bash
$ docker network rm 3695c422697f my-network
```

When you specify multiple networks, the command attempts to delete each in turn.
If the deletion of one network fails, the command continues to the next on the
list and tries to delete that. The command reports success or failure for each
deletion.

## Related information

* [network disconnect ](network_disconnect.md)
Expand Down
23 changes: 23 additions & 0 deletions integration-cli/docker_cli_network_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,29 @@ func (s *DockerSuite) TestDockerNetworkDeleteNotExists(c *check.C) {
c.Assert(err, checker.NotNil, check.Commentf("%v", out))
}

func (s *DockerSuite) TestDockerNetworkDeleteMultiple(c *check.C) {
dockerCmd(c, "network", "create", "testDelMulti0")
assertNwIsAvailable(c, "testDelMulti0")
dockerCmd(c, "network", "create", "testDelMulti1")
assertNwIsAvailable(c, "testDelMulti1")
dockerCmd(c, "network", "create", "testDelMulti2")
assertNwIsAvailable(c, "testDelMulti2")
out, _ := dockerCmd(c, "run", "-d", "--net", "testDelMulti2", "busybox", "top")
waitRun(strings.TrimSpace(out))

// delete three networks at the same time, since testDelMulti2
// contains active container, it's deletion should fail.
out, _, err := dockerCmdWithError("network", "rm", "testDelMulti0", "testDelMulti1", "testDelMulti2")
// err should not be nil due to deleting testDelMulti2 failed.
c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
// testDelMulti2 should fail due to network has active endpoints
c.Assert(out, checker.Contains, "has active endpoints")
assertNwNotAvailable(c, "testDelMulti0")
assertNwNotAvailable(c, "testDelMulti1")
// testDelMulti2 can't be deleted, so it should exists
assertNwIsAvailable(c, "testDelMulti2")
}

func (s *DockerSuite) TestDockerInspectMultipleNetwork(c *check.C) {
out, _ := dockerCmd(c, "network", "inspect", "host", "none")
networkResources := []types.NetworkResource{}
Expand Down
27 changes: 21 additions & 6 deletions man/docker-network-rm.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,39 @@
% Docker Community
% OCT 2015
# NAME
docker-network-rm - remove a new network
docker-network-rm - remove one or more networks

# SYNOPSIS
**docker network rm**
**docker network rm**
[**--help**]
NETWORK
NETWORK [NETWORK...]

# DESCRIPTION

Removes a network by name or identifier. To remove a network, you must first disconnect any containers connected to it.
Removes one or more networks by name or identifier. To remove a network,
you must first disconnect any containers connected to it.
To remove the network named 'my-network':

```
```bash
$ docker network rm my-network
```

To delete multiple networks in a single `docker network rm` command, provide
multiple network names or id's. The following example deletes a network with id
`3695c422697f` and a network named `my-network`:

```bash
$ docker network rm 3695c422697f my-network
```

When you specify multiple networks, the command attempts to delete each in turn.
If the deletion of one network fails, the command continues to the next on the
list and tries to delete that. The command reports success or failure for each
deletion.

# OPTIONS
**NETWORK**
Specify network name
Specify network name or id

**--help**
Print usage statement
Expand Down

0 comments on commit e7eb668

Please sign in to comment.