Skip to content

Commit c5f4c7e

Browse files
committed
Merge pull request #485 from docker/pid_mode
Support for PID mode
2 parents 22dd8d7 + cab37b6 commit c5f4c7e

File tree

5 files changed

+56
-10
lines changed

5 files changed

+56
-10
lines changed

docker/client.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,8 @@ def create_container(self, image, command=None, hostname=None, user=None,
550550
config = self._container_config(
551551
image, command, hostname, user, detach, stdin_open, tty, mem_limit,
552552
ports, environment, dns, volumes, volumes_from, network_disabled,
553-
entrypoint, cpu_shares, working_dir, domainname,
554-
memswap_limit, cpuset, host_config, mac_address
553+
entrypoint, cpu_shares, working_dir, domainname, memswap_limit,
554+
cpuset, host_config, mac_address
555555
)
556556
return self.create_container_from_config(config, name)
557557

@@ -957,7 +957,7 @@ def start(self, container, binds=None, port_bindings=None, lxc_conf=None,
957957
publish_all_ports=False, links=None, privileged=False,
958958
dns=None, dns_search=None, volumes_from=None, network_mode=None,
959959
restart_policy=None, cap_add=None, cap_drop=None, devices=None,
960-
extra_hosts=None, read_only=None):
960+
extra_hosts=None, read_only=None, pid_mode=None):
961961

962962
if utils.compare_version('1.10', self._version) < 0:
963963
if dns is not None:
@@ -969,19 +969,23 @@ def start(self, container, binds=None, port_bindings=None, lxc_conf=None,
969969
'volumes_from is only supported for API version >= 1.10'
970970
)
971971

972-
if utils.compare_version('1.17', self._version) < 0 and \
973-
read_only is not None:
974-
raise errors.InvalidVersion(
975-
'read_only is only supported for API version >= 1.17'
976-
)
972+
if utils.compare_version('1.17', self._version) < 0:
973+
if read_only is not None:
974+
raise errors.InvalidVersion(
975+
'read_only is only supported for API version >= 1.17'
976+
)
977+
if pid_mode is not None:
978+
raise errors.InvalidVersion(
979+
'pid_mode is only supported for API version >= 1.17'
980+
)
977981

978982
start_config = utils.create_host_config(
979983
binds=binds, port_bindings=port_bindings, lxc_conf=lxc_conf,
980984
publish_all_ports=publish_all_ports, links=links, dns=dns,
981985
privileged=privileged, dns_search=dns_search, cap_add=cap_add,
982986
cap_drop=cap_drop, volumes_from=volumes_from, devices=devices,
983987
network_mode=network_mode, restart_policy=restart_policy,
984-
extra_hosts=extra_hosts, read_only=read_only
988+
extra_hosts=extra_hosts, read_only=read_only, pid_mode=pid_mode
985989
)
986990

987991
if isinstance(container, dict):

docker/utils/utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,17 @@ def create_host_config(
309309
publish_all_ports=False, links=None, privileged=False,
310310
dns=None, dns_search=None, volumes_from=None, network_mode=None,
311311
restart_policy=None, cap_add=None, cap_drop=None, devices=None,
312-
extra_hosts=None, read_only=None
312+
extra_hosts=None, read_only=None, pid_mode=None
313313
):
314314
host_config = {}
315315

316+
if pid_mode not in (None, 'host'):
317+
raise errors.DockerException(
318+
'Invalid value for pid param: {0}'.format(pid_mode)
319+
)
320+
elif pid_mode:
321+
host_config['PidMode'] = pid_mode
322+
316323
if privileged:
317324
host_config['Privileged'] = privileged
318325

docs/api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,8 @@ from. Optionally a single string joining container id's with commas
674674
* cap_add (list of str): See note above
675675
* cap_drop (list of str): See note above
676676
* extra_hosts (dict): custom host-to-IP mappings (host:ip)
677+
* pid_mode (str): if set to "host", use the host PID namespace inside the
678+
container
677679

678680
```python
679681
>>> from docker import Client

docs/hostconfig.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ for example:
8383
* cap_drop (list of str): Drop kernel capabilities
8484
* extra_hosts (dict): custom host-to-IP mappings (host:ip)
8585
* read_only (bool): mount the container's root filesystem as read only
86+
* pid_mode (str): if set to "host", use the host PID namespace inside the
87+
container
8688

8789
**Returns** (dict) HostConfig dictionary
8890

tests/integration_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,37 @@ def runTest(self):
10601060
self.assertEqual(state['Paused'], False)
10611061

10621062

1063+
class TestCreateContainerWithHostPidMode(BaseTestCase):
1064+
def runTest(self):
1065+
ctnr = self.client.create_container(
1066+
'busybox', 'true', host_config=create_host_config(
1067+
pid_mode='host'
1068+
)
1069+
)
1070+
self.assertIn('Id', ctnr)
1071+
self.tmp_containers.append(ctnr['Id'])
1072+
self.client.start(ctnr)
1073+
inspect = self.client.inspect_container(ctnr)
1074+
self.assertIn('HostConfig', inspect)
1075+
host_config = inspect['HostConfig']
1076+
self.assertIn('PidMode', host_config)
1077+
self.assertEqual(host_config['PidMode'], 'host')
1078+
1079+
1080+
class TestStartContainerWithHostPidMode(BaseTestCase):
1081+
def runTest(self):
1082+
ctnr = self.client.create_container(
1083+
'busybox', 'true'
1084+
)
1085+
self.assertIn('Id', ctnr)
1086+
self.tmp_containers.append(ctnr['Id'])
1087+
self.client.start(ctnr, pid_mode='host')
1088+
inspect = self.client.inspect_container(ctnr)
1089+
self.assertIn('HostConfig', inspect)
1090+
host_config = inspect['HostConfig']
1091+
self.assertIn('PidMode', host_config)
1092+
self.assertEqual(host_config['PidMode'], 'host')
1093+
10631094
#################
10641095
# LINKS TESTS #
10651096
#################

0 commit comments

Comments
 (0)