Skip to content
This repository has been archived by the owner on Jan 29, 2022. It is now read-only.

Commit

Permalink
[#812] Guarantee a minimum of 0.5 CPUs to our DockerCLIOperator jobs
Browse files Browse the repository at this point in the history
We also limit the memory usage to 350 MB. We were facing a few containers
starving, so I'm hoping these changes will fix that. I haven't added resource
limits to the "main" containers as well (worker, webserver, etc.) because I
couldn't find out how to do that in the Docker Cloud stack file.

opentrials/opentrials#812
  • Loading branch information
vitorbaptista committed Jul 21, 2017
1 parent 052530a commit 631ce40
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 6 additions & 0 deletions dags/operators/docker_cli_operator.py
Expand Up @@ -73,11 +73,17 @@ def _get_docker_run_command(self):
for key, value in self.environment.items()
if value is not None
]
resource_limits_params = [
'--memory=350m',
'--cpu-period=100000',
'--cpu-quota=50000',
]

docker_command = [
'docker',
'run',
'--rm',
] + resource_limits_params + [
] + env_params + [
self.image,
self.command,
Expand Down
8 changes: 6 additions & 2 deletions tests/dags/operators/test_docker_cli_operator.py
Expand Up @@ -4,6 +4,7 @@
import mock
import collections
import io
import re
import shlex
import subprocess
import pytest
Expand Down Expand Up @@ -102,11 +103,13 @@ def test_get_docker_run_command_works_without_environment(self):

docker_command = operator._get_docker_run_command()

assert docker_command == 'docker run --rm {image} {command}'.format(
command_regexp = r'docker run --rm.* {image} {command}'.format(
image=operator.image,
command=operator.command
)

assert re.match(command_regexp, docker_command, re.IGNORECASE)

def test_get_docker_run_command_works_with_environment(self):
environment = collections.OrderedDict([
('foo', 'bar baz'),
Expand All @@ -122,10 +125,11 @@ def test_get_docker_run_command_works_with_environment(self):

docker_command = operator._get_docker_run_command()

assert docker_command == 'docker run --rm --env "foo=$foo" --env "bar=$bar" {image} {command}'.format(
command_regexp = r'^docker run --rm.* --env "foo=\$foo" --env "bar=\$bar".* {image} {command}$'.format(
image=operator.image,
command=operator.command
)
assert re.match(command_regexp, docker_command, re.IGNORECASE)

@mock.patch('dags.operators.docker_cli_operator.DockerCLIOperator._run_command')
def test_pull_image_runs_the_correct_command(self, run_command_mock):
Expand Down

0 comments on commit 631ce40

Please sign in to comment.