From 73bd4aca74e86650ffbb43331d08e0f58afff37a Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Fri, 8 Aug 2014 10:59:08 -0700 Subject: [PATCH 1/3] Use hostnames everywhere in docs, add YAML note and deprecate env.md Signed-off-by: Aanand Prasad --- docs/cli.md | 2 +- docs/env.md | 2 ++ docs/wordpress.md | 2 +- docs/yml.md | 14 ++++++++++++-- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/docs/cli.md b/docs/cli.md index a697ab7ae..21545da7a 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -51,7 +51,7 @@ One-off commands are started in new containers with the same config as a normal 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" + $ fig run db psql -h db_1 -U postgres If you do not want linked containers to be started when running the one-off command, specify the `--no-deps` flag: diff --git a/docs/env.md b/docs/env.md index e6c383000..d9379bb42 100644 --- a/docs/env.md +++ b/docs/env.md @@ -6,6 +6,8 @@ title: Fig environment variables reference Environment variables reference =============================== +**Note:** Environment variables are no longer the recommended method for connecting to linked services. Instead, you should use the link name (by default, name_1) as the hostname to connect to. See the [fig.yml documentation](yml.html#links) for details. + Fig uses [Docker links] to expose services' containers to one another. Each linked container injects a set of environment variables, each of which begins with the uppercase name of the container. To see what environment variables are available to a service, run `fig run SERVICE env`. diff --git a/docs/wordpress.md b/docs/wordpress.md index 701659255..c06eda676 100644 --- a/docs/wordpress.md +++ b/docs/wordpress.md @@ -44,7 +44,7 @@ Two supporting files are needed to get this working - first up, `wp-config.php` define('DB_NAME', 'wordpress'); define('DB_USER', 'root'); define('DB_PASSWORD', ''); -define('DB_HOST', getenv("DB_1_PORT_3306_TCP_ADDR") . ":" . getenv("DB_1_PORT_3306_TCP_PORT")); +define('DB_HOST', "db_1:3306"); define('DB_CHARSET', 'utf8'); define('DB_COLLATE', ''); diff --git a/docs/yml.md b/docs/yml.md index 060aaa05e..ea1dd4205 100644 --- a/docs/yml.md +++ b/docs/yml.md @@ -36,10 +36,10 @@ Override the default command. command: bundle exec thin -p 3000 ``` + ### links - -Link to containers in another service. Optionally specify an alternate name for the link, which will determine how environment variables are prefixed, e.g. `db` -> `DB_1_PORT`, `db:database` -> `DATABASE_1_PORT` +Link to containers in another service. Optionally specify an alternate name for the link, which will determine how environment variables are prefixed, e.g. `db` -> `DB_1_PORT`, `db:database` -> `DATABASE_PORT` ``` links: @@ -48,6 +48,16 @@ links: - redis ``` +An entry with the alias' name will be created in `/etc/hosts` inside containers for this service, e.g: + +``` +172.17.2.186 db_1 +172.17.2.186 database +172.17.2.187 redis_1 +``` + +Environment variables will also be created - see the [environment variable reference](env.html) for details. + ### ports Expose ports. Either specify both ports (`HOST:CONTAINER`), or just the container port (a random host port will be chosen). From 62a4d214e8bebf94e43f01be7ee7c997fc34da79 Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Mon, 28 Jul 2014 18:37:17 -0700 Subject: [PATCH 2/3] Default link alias which is just the service name Closes #37. Signed-off-by: Aanand Prasad --- fig/service.py | 4 +-- tests/integration/service_test.py | 51 +++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/fig/service.py b/fig/service.py index 65bcf5197..2059024af 100644 --- a/fig/service.py +++ b/fig/service.py @@ -282,12 +282,12 @@ class Service(object): links = [] for service, link_name in self.links: for container in service.containers(): - if link_name: - links.append((container.name, link_name)) + links.append((container.name, link_name or service.name)) links.append((container.name, container.name)) links.append((container.name, container.name_without_project)) if link_to_self: for container in self.containers(): + links.append((container.name, self.name)) links.append((container.name, container.name)) links.append((container.name, container.name_without_project)) return links diff --git a/tests/integration/service_test.py b/tests/integration/service_test.py index 75275d8a7..7860e3023 100644 --- a/tests/integration/service_test.py +++ b/tests/integration/service_test.py @@ -171,29 +171,62 @@ class ServiceTest(DockerClientTestCase): def test_start_container_creates_links(self): db = self.create_service('db') web = self.create_service('web', links=[(db, None)]) + + db.start_container() db.start_container() web.start_container() - self.assertIn('figtest_db_1', web.containers()[0].links()) - self.assertIn('db_1', web.containers()[0].links()) + + self.assertEqual( + set(web.containers()[0].links()), + set([ + 'figtest_db_1', 'db_1', + 'figtest_db_2', 'db_2', + 'db', + ]), + ) def test_start_container_creates_links_with_names(self): db = self.create_service('db') web = self.create_service('web', links=[(db, 'custom_link_name')]) + + db.start_container() db.start_container() web.start_container() - self.assertIn('custom_link_name', web.containers()[0].links()) + + self.assertEqual( + set(web.containers()[0].links()), + set([ + 'figtest_db_1', 'db_1', + 'figtest_db_2', 'db_2', + 'custom_link_name', + ]), + ) def test_start_normal_container_does_not_create_links_to_its_own_service(self): db = self.create_service('db') - c1 = db.start_container() - c2 = db.start_container() - self.assertNotIn(c1.name, c2.links()) + + db.start_container() + db.start_container() + + c = db.start_container() + self.assertEqual(set(c.links()), set([])) def test_start_one_off_container_creates_links_to_its_own_service(self): db = self.create_service('db') - c1 = db.start_container() - c2 = db.start_container(one_off=True) - self.assertIn(c1.name, c2.links()) + + db.start_container() + db.start_container() + + c = db.start_container(one_off=True) + + self.assertEqual( + set(c.links()), + set([ + 'figtest_db_1', 'db_1', + 'figtest_db_2', 'db_2', + 'db', + ]), + ) def test_start_container_builds_images(self): service = Service( From 59e31ff5448a2b197503a320b470014dc92da83b Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Fri, 8 Aug 2014 11:00:10 -0700 Subject: [PATCH 3/3] Update docs to remove numeric suffix Signed-off-by: Aanand Prasad --- docs/cli.md | 2 +- docs/django.md | 2 +- docs/env.md | 12 ++++++------ docs/index.md | 2 +- docs/rails.md | 2 +- docs/wordpress.md | 2 +- docs/yml.md | 6 +++--- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/docs/cli.md b/docs/cli.md index 21545da7a..8e1c50464 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -51,7 +51,7 @@ One-off commands are started in new containers with the same config as a normal 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 psql -h db_1 -U postgres + $ fig run db psql -h db -U docker If you do not want linked containers to be started when running the one-off command, specify the `--no-deps` flag: diff --git a/docs/django.md b/docs/django.md index 52b9cd2ec..e2bfbde17 100644 --- a/docs/django.md +++ b/docs/django.md @@ -59,7 +59,7 @@ First thing we need to do is set up the database connection. Replace the `DATABA 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'postgres', 'USER': 'postgres', - 'HOST': 'db_1', + 'HOST': 'db', 'PORT': 5432, } } diff --git a/docs/env.md b/docs/env.md index d9379bb42..ee5c9c1c8 100644 --- a/docs/env.md +++ b/docs/env.md @@ -6,26 +6,26 @@ title: Fig environment variables reference Environment variables reference =============================== -**Note:** Environment variables are no longer the recommended method for connecting to linked services. Instead, you should use the link name (by default, name_1) as the hostname to connect to. See the [fig.yml documentation](yml.html#links) for details. +**Note:** Environment variables are no longer the recommended method for connecting to linked services. Instead, you should use the link name (by default, the name of the linked service) as the hostname to connect to. See the [fig.yml documentation](yml.html#links) for details. Fig uses [Docker links] to expose services' containers to one another. Each linked container injects a set of environment variables, each of which begins with the uppercase name of the container. To see what environment variables are available to a service, run `fig run SERVICE env`. name\_PORT
-Full URL, e.g. `DB_1_PORT=tcp://172.17.0.5:5432` +Full URL, e.g. `DB_PORT=tcp://172.17.0.5:5432` name\_PORT\_num\_protocol
-Full URL, e.g. `DB_1_PORT_5432_TCP=tcp://172.17.0.5:5432` +Full URL, e.g. `DB_PORT_5432_TCP=tcp://172.17.0.5:5432` name\_PORT\_num\_protocol\_ADDR
-Container's IP address, e.g. `DB_1_PORT_5432_TCP_ADDR=172.17.0.5` +Container's IP address, e.g. `DB_PORT_5432_TCP_ADDR=172.17.0.5` name\_PORT\_num\_protocol\_PORT
-Exposed port number, e.g. `DB_1_PORT_5432_TCP_PORT=5432` +Exposed port number, e.g. `DB_PORT_5432_TCP_PORT=5432` name\_PORT\_num\_protocol\_PROTO
-Protocol (tcp or udp), e.g. `DB_1_PORT_5432_TCP_PROTO=tcp` +Protocol (tcp or udp), e.g. `DB_PORT_5432_TCP_PROTO=tcp` name\_NAME
Fully qualified container name, e.g. `DB_1_NAME=/myapp_web_1/myapp_db_1` diff --git a/docs/index.md b/docs/index.md index 496dea958..ef0a77f0c 100644 --- a/docs/index.md +++ b/docs/index.md @@ -59,7 +59,7 @@ from flask import Flask from redis import Redis import os app = Flask(__name__) -redis = Redis(host="redis_1", port=6379) +redis = Redis(host='redis', port=6379) @app.route('/') def hello(): diff --git a/docs/rails.md b/docs/rails.md index a4e950457..5ead07e58 100644 --- a/docs/rails.md +++ b/docs/rails.md @@ -73,7 +73,7 @@ Open up your newly-generated `database.yml`. Replace its contents with the follo pool: 5 username: postgres password: - host: db_1 + host: db test: <<: *default diff --git a/docs/wordpress.md b/docs/wordpress.md index c06eda676..47e8e443a 100644 --- a/docs/wordpress.md +++ b/docs/wordpress.md @@ -44,7 +44,7 @@ Two supporting files are needed to get this working - first up, `wp-config.php` define('DB_NAME', 'wordpress'); define('DB_USER', 'root'); define('DB_PASSWORD', ''); -define('DB_HOST', "db_1:3306"); +define('DB_HOST', "db:3306"); define('DB_CHARSET', 'utf8'); define('DB_COLLATE', ''); diff --git a/docs/yml.md b/docs/yml.md index ea1dd4205..15709f9a8 100644 --- a/docs/yml.md +++ b/docs/yml.md @@ -39,7 +39,7 @@ command: bundle exec thin -p 3000 ### links -Link to containers in another service. Optionally specify an alternate name for the link, which will determine how environment variables are prefixed, e.g. `db` -> `DB_1_PORT`, `db:database` -> `DATABASE_PORT` +Link to containers in another service. Either specify both the service name and the link alias (`SERVICE:ALIAS`), or just the service name (which will also be used for the alias). ``` links: @@ -51,9 +51,9 @@ links: An entry with the alias' name will be created in `/etc/hosts` inside containers for this service, e.g: ``` -172.17.2.186 db_1 +172.17.2.186 db 172.17.2.186 database -172.17.2.187 redis_1 +172.17.2.187 redis ``` Environment variables will also be created - see the [environment variable reference](env.html) for details.