Skip to content

Commit 9523950

Browse files
committed
add support to add or drop capabilities
1 parent e6af420 commit 9523950

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ Identical to the `docker search` command.
232232
```python
233233
c.start(container, binds=None, port_bindings=None, lxc_conf=None,
234234
publish_all_ports=False, links=None, privileged=False,
235-
dns=None, dns_search=None, volumes_from=None, network_mode=None, restart_policy=None)
235+
dns=None, dns_search=None, volumes_from=None, network_mode=None,
236+
restart_policy=None, cap_add=None, cap_drop=None)
236237
```
237238

238239
Similar to the `docker start` command, but doesn't support attach
@@ -278,6 +279,15 @@ for example:
278279
}
279280
```
280281

282+
`cap_add` and `cap_drop` are available since v1.2.0 and can be used to add or drop certain capabilities.
283+
The user may specify the capabilities as an array for example:
284+
```
285+
[
286+
"SYS_ADMIN",
287+
"MKNOD"
288+
]
289+
```
290+
281291

282292
```python
283293
c.stop(container, timeout=10)

docker/client.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ def search(self, term):
807807
def start(self, container, binds=None, port_bindings=None, lxc_conf=None,
808808
publish_all_ports=False, links=None, privileged=False,
809809
dns=None, dns_search=None, volumes_from=None, network_mode=None,
810-
restart_policy=None):
810+
restart_policy=None, cap_add=None, cap_drop=None):
811811
if isinstance(container, dict):
812812
container = container.get('Id')
813813

@@ -869,6 +869,12 @@ def start(self, container, binds=None, port_bindings=None, lxc_conf=None,
869869
if restart_policy:
870870
start_config['RestartPolicy'] = restart_policy
871871

872+
if cap_add:
873+
start_config['CapAdd'] = cap_add
874+
875+
if cap_drop:
876+
start_config['CapDrop'] = cap_drop
877+
872878
url = self._url("/containers/{0}/start".format(container))
873879
res = self._post_json(url, data=start_config)
874880
self._raise_for_status(res)

tests/test.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,56 @@ def test_start_container_with_restart_policy(self):
841841
docker.client.DEFAULT_TIMEOUT_SECONDS
842842
)
843843

844+
def test_start_container_with_added_capabilities(self):
845+
try:
846+
self.client.start(fake_api.FAKE_CONTAINER_ID,
847+
cap_add=['MKNOD'])
848+
except Exception as e:
849+
self.fail('Command should not raise exception: {0}'.format(e))
850+
args = fake_request.call_args
851+
self.assertEqual(
852+
args[0][0],
853+
url_prefix + 'containers/3cc2351ab11b/start'
854+
)
855+
self.assertEqual(
856+
json.loads(args[1]['data']),
857+
{"PublishAllPorts": False, "Privileged": False,
858+
"CapAdd": ["MKNOD"]}
859+
)
860+
self.assertEqual(
861+
args[1]['headers'],
862+
{'Content-Type': 'application/json'}
863+
)
864+
self.assertEqual(
865+
args[1]['timeout'],
866+
docker.client.DEFAULT_TIMEOUT_SECONDS
867+
)
868+
869+
def test_start_container_with_dropped_capabilities(self):
870+
try:
871+
self.client.start(fake_api.FAKE_CONTAINER_ID,
872+
cap_drop=['MKNOD'])
873+
except Exception as e:
874+
self.fail('Command should not raise exception: {0}'.format(e))
875+
args = fake_request.call_args
876+
self.assertEqual(
877+
args[0][0],
878+
url_prefix + 'containers/3cc2351ab11b/start'
879+
)
880+
self.assertEqual(
881+
json.loads(args[1]['data']),
882+
{"PublishAllPorts": False, "Privileged": False,
883+
"CapDrop": ["MKNOD"]}
884+
)
885+
self.assertEqual(
886+
args[1]['headers'],
887+
{'Content-Type': 'application/json'}
888+
)
889+
self.assertEqual(
890+
args[1]['timeout'],
891+
docker.client.DEFAULT_TIMEOUT_SECONDS
892+
)
893+
844894
def test_resize_container(self):
845895
try:
846896
self.client.resize(

0 commit comments

Comments
 (0)