Repository: ansible/ansible-modules-core

Issue:
newly created containers always have default network even with purge_networks
##### ISSUE TYPE
- Bug Report
##### COMPONENT NAME

docker_container.py
##### ANSIBLE VERSION

<!--- Paste verbatim output from “ansible --version” between quotes below -->

```
root@nuid:/usr/local/nubeva/config# ansible --version
ansible 2.2.0
  config file =
  configured module search path = Default w/o overrides
```
##### CONFIGURATION

default
##### OS / ENVIRONMENT

docker for mac
##### SUMMARY

When creating a completely new container instance with purge_networks set to "yes", the container is created with the default bridge.  The second time that the same playbook is run, the playbook updates the container and removes the default bridge.
##### STEPS TO REPRODUCE

With the following playbook snippet:

```
- name: test container
  docker_container:
    name: test_ubuntu
    image: "{{ registry }}/ubuntu:latest"
    detach: True
    networks:
      - name: trafficentry
      - name: trafficexit
    purge_networks: yes
```

Run it once on a blank system (or after docker rm -f test_ubuntu).  You will see the container is created, but inspecting it shows it is connected to three networks, bridge, trafficentry and trafficexit.
Run it a second time and you will see that the container is now connected to just the trafficentry and trafficexit networks as expected.
##### EXPECTED RESULTS

Newly created containers, not just updated containers, should not have the default network connected when purge_networks is configured.
##### ACTUAL RESULTS

After initial run:

```
docker inspect test_ubuntu
<snip>
     "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "bef4d5559dfcf8bc98756caea2d3a47c906718f5c0ea3475ea7e4ab70f68c3ea",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/bef4d5559dfc",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "c93709aa1119526627933435e47352230ecaa73445ddf0556d8ef1ae6211d351",
            "Gateway": "172.18.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.18.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:12:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "239e364714b16413115d71582c96e05e78b7d135572e393f4fbf3c7c14052511",
                    "EndpointID": "c93709aa1119526627933435e47352230ecaa73445ddf0556d8ef1ae6211d351",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:02"
                },
                "trafficentry": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "540bab56ef98"
                    ],
                    "NetworkID": "c87e984a0a94aa11408d6f709e1cc8d35e402323e1de216b2a7958aeb7b7cc53",
                    "EndpointID": "abc3a2634ba561229f9050b05714b2ecc76b78442ddec48d76fd998d05ca659b",
                    "Gateway": "172.20.0.1",
                    "IPAddress": "172.20.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:14:00:02"
                },
                "trafficexit": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "540bab56ef98"
                    ],
                    "NetworkID": "25c50de46ae3a63ddc36d71e8c24a789052dd0e6bf9487d70f310c00f5688c38",
                    "EndpointID": "6c7d18d74f66a8e58c4c46c3f0845fb4c8cad30a7eaac14386b1ca7f1159b660",
                    "Gateway": "172.23.0.1",
                    "IPAddress": "172.23.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:17:00:02"
                }
            }
```

After second run:

```
docker inspect test_ubuntu
<snip>
            "Networks": {
                "trafficentry": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "540bab56ef98"
                    ],
                    "NetworkID": "c87e984a0a94aa11408d6f709e1cc8d35e402323e1de216b2a7958aeb7b7cc53",
                    "EndpointID": "abc3a2634ba561229f9050b05714b2ecc76b78442ddec48d76fd998d05ca659b",
                    "Gateway": "172.20.0.1",
                    "IPAddress": "172.20.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:14:00:02"
                },
                "trafficexit": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "540bab56ef98"
                    ],
                    "NetworkID": "25c50de46ae3a63ddc36d71e8c24a789052dd0e6bf9487d70f310c00f5688c38",
                    "EndpointID": "6c7d18d74f66a8e58c4c46c3f0845fb4c8cad30a7eaac14386b1ca7f1159b660",
                    "Gateway": "172.23.0.1",
                    "IPAddress": "172.23.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:17:00:02"
                }
            }
```



Discussion:
<!-- @gbannister: Thanks for filing this bug report! -->

@ansible ping, this issue is waiting for your response.
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)

<!--- boilerplate: issue_bug_new --->

Ok, so doing a bit of debug, I have found out that the id for the default 'bridge' network is not set when there is no existing container, but it _is_ set when the container exists.  Hence line 1770 succeeds the second run and is silently ignored the first run.


Resolving patch:
diff --git a/cloud/docker/docker_container.py b/cloud/docker/docker_container.py
--- a/cloud/docker/docker_container.py
+++ b/cloud/docker/docker_container.py
@@ -1767,9 +1767,9 @@ def _add_networks(self, container, differences):
     def _purge_networks(self, container, networks):
         for network in networks:
             self.results['actions'].append(dict(removed_from_network=network['name']))
-            if not self.check_mode and network.get('id'):
+            if not self.check_mode:
                 try:
-                    self.client.disconnect_container_from_network(container.Id, network['id'])
+                    self.client.disconnect_container_from_network(container.Id, network['name'])
                 except Exception as exc:
                     self.fail("Error disconnecting container from network %s - %s" % (network['name'],
                                                                                       str(exc)))


Test patch: