Skip to content

Commit d824cb9

Browse files
committed
Add support for swappiness constraint
Run a service using `docker run --memory-swappiness=0` (see https://docs.docker.com/engine/reference/run/) refs #2383 Signed-off-by: Jean-François Roche <[email protected]>
1 parent 13bcd85 commit d824cb9

File tree

7 files changed

+31
-2
lines changed

7 files changed

+31
-2
lines changed

compose/config/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
'mac_address',
7070
'mem_limit',
7171
'memswap_limit',
72+
'mem_swappiness',
7273
'net',
7374
'oom_score_adj'
7475
'pid',

compose/config/config_schema_v1.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"mac_address": {"type": "string"},
8686
"mem_limit": {"type": ["number", "string"]},
8787
"memswap_limit": {"type": ["number", "string"]},
88+
"mem_swappiness": {"type": "integer"},
8889
"net": {"type": "string"},
8990
"pid": {"type": ["string", "null"]},
9091

compose/config/config_schema_v2.0.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
"mac_address": {"type": "string"},
140140
"mem_limit": {"type": ["number", "string"]},
141141
"memswap_limit": {"type": ["number", "string"]},
142+
"mem_swappiness": {"type": "integer"},
142143
"network_mode": {"type": "string"},
143144

144145
"networks": {

compose/service.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
'mem_limit',
5656
'memswap_limit',
5757
'oom_score_adj',
58+
'mem_swappiness',
5859
'pid',
5960
'privileged',
6061
'restart',
@@ -704,7 +705,8 @@ def _get_container_host_config(self, override_options, one_off=False):
704705
cpu_quota=options.get('cpu_quota'),
705706
shm_size=options.get('shm_size'),
706707
tmpfs=options.get('tmpfs'),
707-
oom_score_adj=options.get('oom_score_adj')
708+
oom_score_adj=options.get('oom_score_adj'),
709+
mem_swappiness=options.get('mem_swappiness')
708710
)
709711

710712
def build(self, no_cache=False, pull=False, force_rm=False):

docs/compose-file.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ then read-write will be used.
744744
> - container_name
745745
> - container_name:rw
746746
747-
### cpu\_shares, cpu\_quota, cpuset, domainname, hostname, ipc, mac\_address, mem\_limit, memswap\_limit, oom_score_adj, privileged, read\_only, restart, shm\_size, stdin\_open, tty, user, working\_dir
747+
### cpu\_shares, cpu\_quota, cpuset, domainname, hostname, ipc, mac\_address, mem\_limit, memswap\_limit, mem\_swappiness, oom\_score\_adj, privileged, read\_only, restart, shm\_size, stdin\_open, tty, user, working\_dir
748748

749749
Each of these is a single value, analogous to its
750750
[docker run](https://docs.docker.com/engine/reference/run/) counterpart.
@@ -763,6 +763,7 @@ Each of these is a single value, analogous to its
763763

764764
mem_limit: 1000000000
765765
memswap_limit: 2000000000
766+
mem_swappiness: 10
766767
privileged: true
767768

768769
restart: always

tests/integration/service_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,11 @@ def test_dns_list(self):
852852
container = create_and_start_container(service)
853853
self.assertEqual(container.get('HostConfig.Dns'), ['8.8.8.8', '9.9.9.9'])
854854

855+
def test_mem_swappiness(self):
856+
service = self.create_service('web', mem_swappiness=11)
857+
container = create_and_start_container(service)
858+
self.assertEqual(container.get('HostConfig.MemorySwappiness'), 11)
859+
855860
def test_restart_always_value(self):
856861
service = self.create_service('web', restart={'Name': 'always'})
857862
container = create_and_start_container(service)

tests/unit/config/config_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,24 @@ def test_oom_score_adj_option(self):
12711271
}
12721272
]
12731273

1274+
def test_swappiness_option(self):
1275+
actual = config.load(build_config_details({
1276+
'version': '2',
1277+
'services': {
1278+
'web': {
1279+
'image': 'alpine',
1280+
'mem_swappiness': 10,
1281+
}
1282+
}
1283+
}))
1284+
assert actual.services == [
1285+
{
1286+
'name': 'web',
1287+
'image': 'alpine',
1288+
'mem_swappiness': 10,
1289+
}
1290+
]
1291+
12741292
def test_merge_service_dicts_from_files_with_extends_in_base(self):
12751293
base = {
12761294
'volumes': ['.:/app'],

0 commit comments

Comments
 (0)