From e4e9f0bc19625a603b1f69c08f87164f2465a837 Mon Sep 17 00:00:00 2001 From: Ben Firshman Date: Sun, 19 Jan 2014 20:43:31 +0000 Subject: [PATCH] Link services to themselves E.g. `fig run db ...` will be able to access the db service. --- docs/cli.md | 4 ++++ fig/service.py | 3 +++ tests/service_test.py | 9 +++++++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/docs/cli.md b/docs/cli.md index 2e65e5ca4..9eb87f1f8 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -49,6 +49,10 @@ Note that this will not start any services that the command's service links to. One-off commands are started in new containers with the same config as a normal container for that service, so volumes, links, etc will all be created as expected. The only thing different to a normal container is the command will be overridden with the one specified and no ports will be created in case they collide. +Links are also created between one-off commands and the other containers for that service so you can do stuff like this: + + $ fig run db /bin/sh -c "psql -h \$DB_1_PORT_5432_TCP_ADDR -U docker" + ## scale Set number of containers to run for a service. diff --git a/fig/service.py b/fig/service.py index 2e7a89d01..eea70dc21 100644 --- a/fig/service.py +++ b/fig/service.py @@ -212,6 +212,9 @@ class Service(object): for container in service.containers(): links.append((container.name, container.name)) links.append((container.name, container.name_without_project)) + for container in self.containers(): + links.append((container.name, container.name)) + links.append((container.name, container.name_without_project)) return links def _get_container_options(self, override_options, one_off=False): diff --git a/tests/service_test.py b/tests/service_test.py index 740d1f5e0..72101b4d7 100644 --- a/tests/service_test.py +++ b/tests/service_test.py @@ -155,8 +155,13 @@ class ServiceTest(DockerClientTestCase): web.start_container() self.assertIn('figtest_db_1', web.containers()[0].links()) self.assertIn('db_1', web.containers()[0].links()) - db.stop(timeout=1) - web.stop(timeout=1) + + def test_start_container_creates_links_to_its_own_service(self): + db1 = self.create_service('db') + db2 = self.create_service('db') + db1.start_container() + db2.start_container() + self.assertIn('db_1', db2.containers()[0].links()) def test_start_container_builds_images(self): service = Service(