Repository: ansible/ansible-modules-core

Issue:
Memory limit not working docker 1.7.0
Hi there
Using docker Client version: 1.7.0 Client API version: 1.19
Using docker module for running a container and set memory limit like:
memory_limit: "256MB"
or:
memory_limit: 256MB

when I docker inspect the container the memory is not set.



Discussion:
cc: @cove @joshuaconner @softzilla @smashwilson

what version of ansible, os, python? could you fill in the template? https://github.com/ansible/ansible/blob/devel/ISSUE_TEMPLATE.md

Hi,
running from os x mavericks 10.9 to ubuntu1404
ansible 1.9.0.1
docker Client version: 1.7.0 Client API version: 1.19
ansible.cfg

```
[defaults]
host_key_checking = no
record_host_keys = no
jinja2_extensions = jinja2.ext.do
timeout = 15
gathering = smart

[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=30m
scp_if_ssh = True
pipelining = True
```

trying to:

```
- name: run container
  docker:
    name: sample
    image: "{{sample_image }}"
    state: started
    restart_policy: "{{ sample_restart_policy }}"
    ports:
    - "{{ sample_port }}:{{ sample_port }}"
    expose:
    - "{{ sample_port }}"
    net: "{{ sample_net }}"
    command: "{{ sample_command }}"
    volumes:
    - "/var/run/docker.sock:/tmp/docker.sock"
    memory_limit: "{{ sample_container_memory_limit }}"
```

when docker inspect got:

```
 "Memory": 0,
```

Same here, added

```
 memory_limit: "512MB"
```

and playbook runs, but docker inspect shows:

```
 "Memory": 0,
 "MemorySwap": 0,
```

Think must be due to I am using docker-py version=1.1.0 as per ansible docs http://docs.ansible.com/ansible/docker_module.html docker-py >= 0.3.0 is a requirement

I put in a pull request that fixes #1766 due to the wrong data structure reference which was causing container deployments with api v1.19 to break.

On a whim I added,

```
 memory_limit: "512MB"
```

to the playbook, and before where it would deploy successfully and a _docker inspect_ would show _"Memory": 0,_ - it now gives the error,

```
Traceback (most recent call last):
  File "/home/micheal/.ansible/tmp/ansible-tmp-1437509787.17-61083843422347/docker", line 3132, in <module>
    main()
  File "/home/micheal/.ansible/tmp/ansible-tmp-1437509787.17-61083843422347/docker", line 1496, in main
    reloaded(manager, containers, count, name)
  File "/home/micheal/.ansible/tmp/ansible-tmp-1437509787.17-61083843422347/docker", line 1377, in reloaded
    started(manager, containers, count, name)
  File "/home/micheal/.ansible/tmp/ansible-tmp-1437509787.17-61083843422347/docker", line 1354, in started
    created = manager.create_containers(delta)
  File "/home/micheal/.ansible/tmp/ansible-tmp-1437509787.17-61083843422347/docker", line 1227, in create_containers
    containers = do_create(count, params)
  File "/home/micheal/.ansible/tmp/ansible-tmp-1437509787.17-61083843422347/docker", line 1217, in do_create
    result = self.client.create_container(**params)
  File "/usr/lib/python2.6/site-packages/docker/client.py", line 237, in create_container
    volume_driver
  File "/usr/lib/python2.6/site-packages/docker/utils/utils.py", line 546, in create_container_config
    'mem_limit has been moved to host_config in API version 1.19'
docker.errors.InvalidVersion: mem_limit has been moved to host_config in API version 1.19
```

It looks like the method of setting a mem_limit is deprecated, and I poked around where it's coming from, [docker-py/docker/client.py](https://github.com/docker/docker-py/blob/master/docker/client.py#L216), but haven't come up with a fix.


Resolving patch:
diff --git a/cloud/docker/docker.py b/cloud/docker/docker.py
--- a/cloud/docker/docker.py
+++ b/cloud/docker/docker.py
@@ -1404,12 +1404,12 @@ def create_containers(self, count=1):
             mem_limit = _human_to_bytes(self.module.params.get('memory_limit'))
         except ValueError as e:
             self.module.fail_json(msg=str(e))
+        api_version = self.client.version()['ApiVersion']
 
         params = {'image':        self.module.params.get('image'),
                   'command':      self.module.params.get('command'),
                   'ports':        self.exposed_ports,
                   'volumes':      self.volumes,
-                  'mem_limit':    mem_limit,
                   'environment':  self.env,
                   'hostname':     self.module.params.get('hostname'),
                   'domainname':   self.module.params.get('domainname'),
@@ -1421,10 +1421,16 @@ def create_containers(self, count=1):
                   'host_config':  self.create_host_config(),
                   'user':         self.module.params.get('docker_user'),
                   }
-
         if self.ensure_capability('host_config', fail=False):
             params['host_config'] = self.get_host_config()
 
+        #For v1.19 API and above use HostConfig, otherwise use Config
+        if api_version < 1.19:
+            params['mem_limit'] = mem_limit
+        else:
+            params['host_config']['mem_limit'] = mem_limit
+
+
         def do_create(count, params):
             results = []
             for _ in range(count):


Test patch: