mirror of
https://github.com/docker/compose.git
synced 2025-07-24 14:14:35 +02:00
Add recreate_containers method to service
This commit is contained in:
parent
a8e275a432
commit
3c5e334d9d
@ -87,6 +87,13 @@ class Project(object):
|
|||||||
if len(service.containers(stopped=True)) == 0:
|
if len(service.containers(stopped=True)) == 0:
|
||||||
service.create_container()
|
service.create_container()
|
||||||
|
|
||||||
|
def recreate_containers(self, service_names):
|
||||||
|
"""
|
||||||
|
For each service, create or recreate their containers.
|
||||||
|
"""
|
||||||
|
for service in self.get_services(service_names):
|
||||||
|
service.recreate_containers()
|
||||||
|
|
||||||
def start(self, service_names=None, **options):
|
def start(self, service_names=None, **options):
|
||||||
for service in self.get_services(service_names):
|
for service in self.get_services(service_names):
|
||||||
service.start(**options)
|
service.start(**options)
|
||||||
|
@ -73,6 +73,25 @@ class Service(object):
|
|||||||
return Container.create(self.client, **container_options)
|
return Container.create(self.client, **container_options)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
def recreate_containers(self, **override_options):
|
||||||
|
"""
|
||||||
|
If a container for this service doesn't exist, create one. If there are
|
||||||
|
any, stop, remove and recreate them.
|
||||||
|
"""
|
||||||
|
containers = self.containers(stopped=True)
|
||||||
|
if len(containers) == 0:
|
||||||
|
return [self.create_container(**override_options)]
|
||||||
|
else:
|
||||||
|
new_containers = []
|
||||||
|
for old_container in containers:
|
||||||
|
if old_container.is_running:
|
||||||
|
old_container.stop()
|
||||||
|
options = dict(override_options)
|
||||||
|
options['volumes_from'] = old_container.id
|
||||||
|
new_containers.append(self.create_container(**options))
|
||||||
|
old_container.remove()
|
||||||
|
return new_containers
|
||||||
|
|
||||||
def start_container(self, container=None, **override_options):
|
def start_container(self, container=None, **override_options):
|
||||||
if container is None:
|
if container is None:
|
||||||
container = self.create_container(**override_options)
|
container = self.create_container(**override_options)
|
||||||
|
@ -102,6 +102,13 @@ class ServiceTest(DockerClientTestCase):
|
|||||||
container = db.create_container(one_off=True)
|
container = db.create_container(one_off=True)
|
||||||
self.assertEqual(container.name, 'figtest_db_run_1')
|
self.assertEqual(container.name, 'figtest_db_run_1')
|
||||||
|
|
||||||
|
def test_recreate_containers(self):
|
||||||
|
service = self.create_service('db')
|
||||||
|
container = service.create_container()
|
||||||
|
new_container = service.recreate_containers()[0]
|
||||||
|
self.assertEqual(len(service.containers(stopped=True)), 1)
|
||||||
|
self.assertNotEqual(container.id, new_container.id)
|
||||||
|
|
||||||
def test_start_container_passes_through_options(self):
|
def test_start_container_passes_through_options(self):
|
||||||
db = self.create_service('db')
|
db = self.create_service('db')
|
||||||
db.start_container(environment={'FOO': 'BAR'})
|
db.start_container(environment={'FOO': 'BAR'})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user