diff --git a/compose/project.py b/compose/project.py index c37175ae0..a13b8a1fb 100644 --- a/compose/project.py +++ b/compose/project.py @@ -6,7 +6,7 @@ from functools import reduce from docker.errors import APIError from .config import get_service_name_from_net, ConfigurationError -from .const import LABEL_PROJECT, LABEL_ONE_OFF +from .const import LABEL_PROJECT, LABEL_SERVICE, LABEL_ONE_OFF from .service import Service, check_for_legacy_containers from .container import Container @@ -276,6 +276,11 @@ class Project(object): all=stopped, filters={'label': self.labels(one_off=one_off)})] + def matches_service_names(container): + if not service_names: + return True + return container.labels.get(LABEL_SERVICE) in service_names + if not containers: check_for_legacy_containers( self.client, @@ -284,7 +289,7 @@ class Project(object): stopped=stopped, one_off=one_off) - return containers + return filter(matches_service_names, containers) def _inject_deps(self, acc, service): dep_names = service.get_dependency_names() diff --git a/tests/integration/project_test.py b/tests/integration/project_test.py index b6dcecbc6..6e315e84a 100644 --- a/tests/integration/project_test.py +++ b/tests/integration/project_test.py @@ -6,6 +6,29 @@ from .testcases import DockerClientTestCase class ProjectTest(DockerClientTestCase): + + def test_containers(self): + web = self.create_service('web') + db = self.create_service('db') + project = Project('composetest', [web, db], self.client) + + project.up() + + containers = project.containers() + self.assertEqual(len(containers), 2) + + def test_containers_with_service_names(self): + web = self.create_service('web') + db = self.create_service('db') + project = Project('composetest', [web, db], self.client) + + project.up() + + containers = project.containers(['web']) + self.assertEqual( + [c.name for c in containers], + ['composetest_web_1']) + def test_volumes_from_service(self): service_dicts = config.from_dictionary({ 'data': {