Repository: docker/docker-py

Issue:
How to set the create_container --cpuset-cpus arameters？
VERSION
    1.2.1

create_container(..., cpuset=0) doesn't seem to work



Discussion:
Not implemented at the moment, since it never made it into the [Changelog](http://docs.docker.com/reference/api/docker_remote_api/) ... I'll look into adding it.

Thank you for your reply, but I still have some questions.

<h4>docker-py Version:</h4>

```
In [12]: import docker
In [13]: docker.version
Out[13]: '1.2.1'
```

<h4>Help create_container():</h4>

```
In [14]: help(docker.Client.create_container)
```

Help on method create_container in module docker.client:

```
create_container(self, image, command=None, hostname=None, user=None, detach=False, stdin_open=False, tty=False, mem_limit=0, ports=None, environment=None, dns=None, volumes=None, volumes_from=None, network_disabled=False, name=None, entrypoint=None, cpu_shares=None, working_dir=None, domainname=None, memswap_limit=0, <code style="background: #cc6666;">cpuset=None</code>, host_config=None, mac_address=None, labels=None) unbound docker.client.Client method
```

Use cpuset:

```
.create_container(cpuset=0, mem_limit="512m", memswap_limit=-1, ....)
```

inspect container content:

```
root@api:~# docker inspect 81
[{
    "AppArmorProfile": "",
    "Args": [],
    "Config": {
        "AttachStderr": false,
        "AttachStdin": false,
        "AttachStdout": false,
        "Cmd": [
            "bash"
        ],
        "CpuShares": 0,
        <code style="background: #cc6666;">"Cpuset": "0",</code>   ------------------------------- cpuset is Valid! -------------------------------
        "Domainname": "",
        "Entrypoint": null,
        "Env": [
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        ],
        "ExposedPorts": {
            "22/tcp": {},
            "80/tcp": {}
        },
        "Hostname": "81de5323a3c7",
        "Image": "longgeek/ubuntu-14.04.1:base",
        "Labels": {},
        "MacAddress": "",
        <code style="background: #cc6666;">"Memory": 536870912,
        "MemorySwap": -1,</code>    ------------------------------- mem is Valid! -------------------------------
        "NetworkDisabled": false,
        "OnBuild": null,
        "OpenStdin": true,
        "PortSpecs": null,
        "StdinOnce": false,
        "Tty": true,
        "User": "",
        "Volumes": null,
        "WorkingDir": ""
    },
    "Created": "2015-05-07T15:52:16.550464035Z",
    "Driver": "aufs",
    "ExecDriver": "native-0.2",
    "ExecIDs": [
        "2abcb14a851b3c246c1c4d0651f9b2596c07acd3cb8c02b09dc9a3fb43fe51cd"
    ],
    "HostConfig": {
        "Binds": null,
        "CapAdd": null,
        "CapDrop": null,
        "CgroupParent": "",
        "ContainerIDFile": "",
        "CpuShares": 0,
        <code style="background: #cc6666;">"CpusetCpus": "",</code>    ------------------------------- Didn't work? -------------------------------
        "Devices": null,
        "Dns": null,
        "DnsSearch": null,
        "ExtraHosts": null,
        "IpcMode": "",
        "Links": null,
        "LogConfig": {
            "Config": null,
            "Type": "json-file"
        },
        "LxcConf": null,
        <code style="background: #cc6666;">"Memory": 0,
        "MemorySwap": 0,</code>     ------------------------------- Didn't work? -------------------------------
        "NetworkMode": "",
        "PidMode": "",
        "PortBindings": null,
        "Privileged": false,
        "PublishAllPorts": true,
        "ReadonlyRootfs": false,
        "RestartPolicy": {
            "MaximumRetryCount": 0,
            "Name": ""
        },
        "SecurityOpt": null,
        "Ulimits": null,
        "VolumesFrom": null
    },
    "HostnamePath": "/var/lib/docker/containers/81de5323a3c7adff2d531f501e5b43706bf18077265bb4247ea2552fec18e982/hostname",
    "HostsPath": "/var/lib/docker/containers/81de5323a3c7adff2d531f501e5b43706bf18077265bb4247ea2552fec18e982/hosts",
    "Id": "81de5323a3c7adff2d531f501e5b43706bf18077265bb4247ea2552fec18e982",
    "Image": "fbefd02fb923f18815e65df583f484ec9522b2d99e7ddd97d62fb93f0c7608c5",
    "LogPath": "/var/lib/docker/containers/81de5323a3c7adff2d531f501e5b43706bf18077265bb4247ea2552fec18e982/81de5323a3c7adff2d531f501e5b43706bf18077265bb4247ea2552fec18e982-json.log",
    "MountLabel": "",
    "Name": "/c-153ik1jpw9cmby",
    "NetworkSettings": {
        "Bridge": "docker0",
        "Gateway": "172.17.42.1",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "IPAddress": "172.17.0.211",
        "IPPrefixLen": 16,
        "IPv6Gateway": "",
        "LinkLocalIPv6Address": "fe80::42:acff:fe11:d3",
        "LinkLocalIPv6PrefixLen": 64,
        "MacAddress": "02:42:ac:11:00:d3",
        "PortMapping": null,
        "Ports": {
            "22/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "37491"
                }
            ],
            "80/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "37492"
                }
            ]
        }
    },
    "Path": "bash",
    "ProcessLabel": "",
    "ResolvConfPath": "/var/lib/docker/containers/81de5323a3c7adff2d531f501e5b43706bf18077265bb4247ea2552fec18e982/resolv.conf",
    "RestartCount": 0,
    "State": {
        "Dead": false,
        "Error": "",
        "ExitCode": 0,
        "FinishedAt": "2015-05-07T17:42:12.139743371Z",
        "OOMKilled": false,
        "Paused": false,
        "Pid": 612,
        "Restarting": false,
        "Running": true,
        "StartedAt": "2015-05-07T17:55:53.788976399Z"
    },
    "Volumes": {},
    "VolumesRW": {}
}
]
```

<h4>Why mem and swap did not appear in the Hostconfig?</h4>

@shin- 
See http://docs.docker.com/reference/api/docker_remote_api_v1.18/#create-a-container

```
Cpuset - The same as CpusetCpus, but deprecated, please don't use.
CpusetCpus - String value containg the cgroups CpusetCpus to use.
```

What is your docker version and what OS is it running on? I tested it and it works for me.

```
In [9]: c.create_container('busybox', 'sleep 999', mem_limit='512m', memswap_limit=-1)
Out[9]: 
{u'Id': u'f5372b4faf62aa1b414b892f26d72fdac7f5b595ea890c3c657e1721b56b0441',
 u'Warnings': None}

In [10]: data = c.inspect_container(_)

In [12]: data['Config']['Memory']
Out[12]: 536870912

In [13]: data['HostConfig']['Memory']
Out[13]: 536870912

In [14]: data['HostConfig']['MemorySwap']
Out[14]: -1

In [15]: data['Config']['MemorySwap']
Out[15]: -1
```

My System:

```
root@api:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.1 LTS
Release:        14.04
Codename:       trusty
```

Docker Info:

```
root@api:~# docker info
Containers: 46
Images: 25
Storage Driver: aufs
 Root Dir: /var/lib/docker/aufs
 Backing Filesystem: extfs
 Dirs: 117
 Dirperm1 Supported: false
Execution Driver: native-0.2
Kernel Version: 3.13.0-44-generic
Operating System: Ubuntu 14.04.1 LTS
CPUs: 2
Total Memory: 1.955 GiB
Name: api
ID: 6YNH:I5E5:P5SO:4NT4:6CVI:X5LL:XD2N:OACO:ZI4V:3Z6V:QYGO:IWEE
```

Docker Version:

```
root@api:~# docker version
Client version: 1.6.0
Client API version: 1.18
Go version (client): go1.4.2
Git commit (client): 4749651
OS/Arch (client): linux/amd64
Server version: 1.6.0
Server API version: 1.18
Go version (server): go1.4.2
Git commit (server): 4749651
OS/Arch (server): linux/amd64
```

The following is my operation：

```
In [1]: from docker import Client

In [2]: c = Client(base_url="unix:///var/run/docker.sock")                                                                                                                

In [3]: c.create_container('longgeek/ubuntu-14.04.1:base', '/bin/bash', mem_limit='512m', memswap_limit=-1)                                                               
Out[3]: 
{u'Id': u'09fd71b4e9ba15a659db49f3b56748461c68a7aa5962371e9a95fa2a64553645',  u'Warnings': None}

In [4]: data = c.inspect_container('09fd')

In [5]: data['Config']['Memory']                                                                                                                                          
Out[5]: 536870912

In [6]: data['HostConfig']['Memory']
Out[6]: 536870912

In [7]: data['HostConfig']['MemorySwap']
Out[7]: -1

In [8]: c.start('09fd', publish_all_ports=True)

In [9]: data = c.inspect_container('09fd')

In [10]: data['HostConfig']['Memory']
Out[10]: 0

In [11]: data['HostConfig']['MemorySwap']
Out[11]: 0

In [12]: data['Config']['Memory']
Out[12]: 536870912

In [13]: data['Config']['MemorySwap']
Out[13]: -1
```

Try this instead:

```
container = c.create_container('longgeek/ubuntu-14.04.1:base', '/bin/bash', mem_limit='512m', memswap_limit=-1, host_config=docker.utils.create_host_config(publish_all_ports=True))
c.start(container)
```

[This is related to what I mentioned here](https://github.com/docker/docker-py/issues/578#issuecomment-97651026), we're going to deprecate passing host config in `start` because it's poorly supported in the docker engine

thanks @shin- :+1: 

```
container = c.create_container('longgeek/ubuntu-14.04.1:base', '/bin/bash', mem_limit='512m', memswap_limit=-1, cpuset=0, host_config=docker.utils.create_host_config(publish_all_ports=True))

c.start(container)
```

<h4>cpuset=0 valid.</h4>

```
"HostConfig": {
    "CpuShares": 0,
    "CpusetCpus": "0",
    "Memory": 536870912,
    "MemorySwap": -1,
```


Resolving patch:
diff --git a/docker/utils/utils.py b/docker/utils/utils.py
--- a/docker/utils/utils.py
+++ b/docker/utils/utils.py
@@ -577,6 +577,7 @@ def create_container_config(
         'Entrypoint': entrypoint,
         'CpuShares': cpu_shares,
         'Cpuset': cpuset,
+        'CpusetCpus': cpuset,
         'WorkingDir': working_dir,
         'MemorySwap': memswap_limit,
         'HostConfig': host_config,


Test patch:
diff --git a/tests/test.py b/tests/test.py
--- a/tests/test.py
+++ b/tests/test.py
@@ -489,6 +489,7 @@ def test_create_container_with_cpuset(self):
                              "StdinOnce": false,
                              "NetworkDisabled": false,
                              "Cpuset": "0,1",
+                             "CpusetCpus": "0,1",
                              "MemorySwap": 0}'''))
         self.assertEqual(args[1]['headers'],
                          {'Content-Type': 'application/json'})