diff --git a/docs/cli.md b/docs/cli.md
index a697ab7ae..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 /bin/sh -c "psql -h \$DB_1_PORT_5432_TCP_ADDR -U docker"
+ $ 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 e6c383000..ee5c9c1c8 100644
--- a/docs/env.md
+++ b/docs/env.md
@@ -6,24 +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, 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 701659255..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', getenv("DB_1_PORT_3306_TCP_ADDR") . ":" . getenv("DB_1_PORT_3306_TCP_PORT"));
+define('DB_HOST', "db:3306");
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
diff --git a/docs/yml.md b/docs/yml.md
index 5a9dbde23..742fdaf18 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. 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:
@@ -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
+172.17.2.186 database
+172.17.2.187 redis
+```
+
+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).
diff --git a/fig/service.py b/fig/service.py
index 7e0214369..bbb1bb669 100644
--- a/fig/service.py
+++ b/fig/service.py
@@ -273,12 +273,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 6dc551a8d..f8094bc2e 100644
--- a/tests/integration/service_test.py
+++ b/tests/integration/service_test.py
@@ -175,29 +175,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(