{"action":"created","issue":{"url":"https://api.github.com/repos/docker/docker/issues/27525","repository_url":"https://api.github.com/repos/docker/docker","labels_url":"https://api.github.com/repos/docker/docker/issues/27525/labels{/name}","comments_url":"https://api.github.com/repos/docker/docker/issues/27525/comments","events_url":"https://api.github.com/repos/docker/docker/issues/27525/events","html_url":"https://github.com/docker/docker/pull/27525","id":183864538,"number":27525,"title":"add `docker network prune`","user":{"login":"AkihiroSuda","id":9248427,"avatar_url":"https://avatars.githubusercontent.com/u/9248427?v=3","gravatar_id":"","url":"https://api.github.com/users/AkihiroSuda","html_url":"https://github.com/AkihiroSuda","followers_url":"https://api.github.com/users/AkihiroSuda/followers","following_url":"https://api.github.com/users/AkihiroSuda/following{/other_user}","gists_url":"https://api.github.com/users/AkihiroSuda/gists{/gist_id}","starred_url":"https://api.github.com/users/AkihiroSuda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AkihiroSuda/subscriptions","organizations_url":"https://api.github.com/users/AkihiroSuda/orgs","repos_url":"https://api.github.com/users/AkihiroSuda/repos","events_url":"https://api.github.com/users/AkihiroSuda/events{/privacy}","received_events_url":"https://api.github.com/users/AkihiroSuda/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/docker/docker/labels/status/0-triage","name":"status/0-triage","color":"EEEEEE"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-10-19T04:43:24Z","updated_at":"2016-10-19T04:45:12Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/docker/docker/pulls/27525","html_url":"https://github.com/docker/docker/pull/27525","diff_url":"https://github.com/docker/docker/pull/27525.diff","patch_url":"https://github.com/docker/docker/pull/27525.patch"},"body":"<!--\r\nPlease make sure you've read and understood our contributing guidelines;\r\nhttps://github.com/docker/docker/blob/master/CONTRIBUTING.md\r\n\r\n** Make sure all your commits include a signature generated with `git commit -s` **\r\n\r\nFor additional information on our contributing process, read our contributing\r\nguide https://docs.docker.com/opensource/code/\r\n\r\nIf this is a bug fix, make sure your description includes \"fixes #xxxx\", or\r\n\"closes #xxxx\"\r\n\r\nPlease provide the following information:\r\n-->\r\n\r\n**- What I did**\r\nAdded `docker network prune`.\r\n\r\n`docker network prune` prunes unused networks, including overlay ones.\r\n`docker system prune` also prunes unused networks.\r\n\r\n**- How I did it**\r\nPlease refer to the code of `daemon/prune.go`\r\n\r\nNote that pruning overlay networks is hacky because there is no reliable way to obtain the list of the  containers attached to a network (https://github.com/docker/docker/issues/24186, https://github.com/docker/docker/issues/24684#issuecomment-233029895)\r\n\r\n```go\r\n\tnetworkIsInUse := regexp.MustCompile(`network ([[:alnum:]]+) is in use`)\r\n\tfor _, nw := range networks {\r\n\t\tif nw.Name == \"ingress\" {\r\n\t\t\tcontinue\r\n\t\t}\r\n\t\t// https://github.com/docker/docker/issues/24186\r\n\t\t// `docker network inspect` unfortunately displays ONLY those containers that are local to that node.\r\n\t\t// So we try to remove it anyway and check the error\r\n\t\terr = cluster.RemoveNetwork(nw.ID)\r\n\t\tif err != nil {\r\n\t\t\t// we can safely ignore the \"network .. is in use\" error\r\n\t\t\tmatch := networkIsInUse.FindStringSubmatch(err.Error())\r\n\t\t\tif len(match) != 2 || match[1] != nw.ID {\r\n\t\t\t\treturn rep, err\r\n\t\t\t}\r\n                       continue\r\n\t\t}\r\n\t\trep.NetworksDeleted = append(rep.NetworksDeleted, nw.Name)\r\n\t}\r\n```\r\n\r\n**- How to verify it**\r\n\r\nCreate networks.\r\n```console\r\n$ docker network create n1\r\n$ docker network create n2\r\n$ docker network create n3 --driver overlay\r\n$ docker network create n4 --driver overlay\r\n$ docker network ls\r\nNETWORK ID          NAME                DRIVER              SCOPE\r\n3ebf59bfe2d6        bridge              bridge              local\r\nf135daf36ceb        docker_gwbridge     bridge              local\r\nf0d521d2acce        host                host                local\r\n84xh9knigj6z        ingress             overlay             swarm\r\n0b3f3ed7f8f3        n1                  bridge              local\r\nf822e084d89d        n2                  bridge              local\r\n5ozn7ioueub8        n3                  overlay             swarm\r\n4jgj4135fom4        n4                  overlay             swarm\r\n971d1ce89578        none                null                local\r\n```\r\n\r\nAttach a container to `n1`, a service to `n3`.\r\n```console\r\n$ docker run -d --name testprune --net n1 busybox top\r\n$ docker service create --name testprunesvc --network n3 busybox top\r\n```\r\n\r\nPrune unused networks (`n2`, `n4`). For pruning n4 (swarm), the command needs to be issued on a manager node.\r\n```console\r\n$ docker network prune\r\nWARNING! This will remove all networks not used by at least one container.\r\nAre you sure you want to continue? [y/N] y\r\nDeleted Networks:\r\nn4\r\nn2\r\n$ docker network ls\r\nNETWORK ID          NAME                DRIVER              SCOPE\r\n3ebf59bfe2d6        bridge              bridge              local\r\nf135daf36ceb        docker_gwbridge     bridge              local\r\nf0d521d2acce        host                host                local\r\n84xh9knigj6z        ingress             overlay             swarm\r\n0b3f3ed7f8f3        n1                  bridge              local\r\n5ozn7ioueub8        n3                  overlay             swarm\r\n971d1ce89578        none                null                local\r\n```\r\n\r\n**- Description for the changelog**\r\n<!--\r\nWrite a short (one line) summary that describes the changes in this\r\npull request for inclusion in the changelog:\r\n-->\r\nadd `docker network prune`\r\n\r\n**- A picture of a cute animal (not mandatory but encouraged)**\r\n![image](https://cloud.githubusercontent.com/assets/9248427/19505909/bb31906c-9601-11e6-9cfa-273d1ccfec79.png)\r\n\r\n\r\n\r\n\r\nSigned-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>\r\n"},"comment":{"url":"https://api.github.com/repos/docker/docker/issues/comments/254709226","html_url":"https://github.com/docker/docker/pull/27525#issuecomment-254709226","issue_url":"https://api.github.com/repos/docker/docker/issues/27525","id":254709226,"user":{"login":"AkihiroSuda","id":9248427,"avatar_url":"https://avatars.githubusercontent.com/u/9248427?v=3","gravatar_id":"","url":"https://api.github.com/users/AkihiroSuda","html_url":"https://github.com/AkihiroSuda","followers_url":"https://api.github.com/users/AkihiroSuda/followers","following_url":"https://api.github.com/users/AkihiroSuda/following{/other_user}","gists_url":"https://api.github.com/users/AkihiroSuda/gists{/gist_id}","starred_url":"https://api.github.com/users/AkihiroSuda/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AkihiroSuda/subscriptions","organizations_url":"https://api.github.com/users/AkihiroSuda/orgs","repos_url":"https://api.github.com/users/AkihiroSuda/repos","events_url":"https://api.github.com/users/AkihiroSuda/events{/privacy}","received_events_url":"https://api.github.com/users/AkihiroSuda/received_events","type":"User","site_admin":false},"created_at":"2016-10-19T04:45:12Z","updated_at":"2016-10-19T04:45:12Z","body":"cc @mlaventure @mavenugo "}}
{"actor":{"display_login":"AkihiroSuda"}}