Skip to content

Commit 2827786

Browse files
committed
Merge pull request #364 from docker/non-numeric-link-alias
Non-numeric link alias
2 parents 7ad91f3 + 59e31ff commit 2827786

9 files changed

Lines changed: 68 additions & 23 deletions

File tree

docs/cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ One-off commands are started in new containers with the same config as a normal
5151

5252
Links are also created between one-off commands and the other containers for that service so you can do stuff like this:
5353

54-
$ fig run db /bin/sh -c "psql -h \$DB_1_PORT_5432_TCP_ADDR -U docker"
54+
$ fig run db psql -h db -U docker
5555

5656
If you do not want linked containers to be started when running the one-off command, specify the `--no-deps` flag:
5757

docs/django.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ First thing we need to do is set up the database connection. Replace the `DATABA
5959
'ENGINE': 'django.db.backends.postgresql_psycopg2',
6060
'NAME': 'postgres',
6161
'USER': 'postgres',
62-
'HOST': 'db_1',
62+
'HOST': 'db',
6363
'PORT': 5432,
6464
}
6565
}

docs/env.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,26 @@ title: Fig environment variables reference
66
Environment variables reference
77
===============================
88

9+
**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.
10+
911
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.
1012

1113
To see what environment variables are available to a service, run `fig run SERVICE env`.
1214

1315
<b><i>name</i>\_PORT</b><br>
14-
Full URL, e.g. `DB_1_PORT=tcp://172.17.0.5:5432`
16+
Full URL, e.g. `DB_PORT=tcp://172.17.0.5:5432`
1517

1618
<b><i>name</i>\_PORT\_<i>num</i>\_<i>protocol</i></b><br>
17-
Full URL, e.g. `DB_1_PORT_5432_TCP=tcp://172.17.0.5:5432`
19+
Full URL, e.g. `DB_PORT_5432_TCP=tcp://172.17.0.5:5432`
1820

1921
<b><i>name</i>\_PORT\_<i>num</i>\_<i>protocol</i>\_ADDR</b><br>
20-
Container's IP address, e.g. `DB_1_PORT_5432_TCP_ADDR=172.17.0.5`
22+
Container's IP address, e.g. `DB_PORT_5432_TCP_ADDR=172.17.0.5`
2123

2224
<b><i>name</i>\_PORT\_<i>num</i>\_<i>protocol</i>\_PORT</b><br>
23-
Exposed port number, e.g. `DB_1_PORT_5432_TCP_PORT=5432`
25+
Exposed port number, e.g. `DB_PORT_5432_TCP_PORT=5432`
2426

2527
<b><i>name</i>\_PORT\_<i>num</i>\_<i>protocol</i>\_PROTO</b><br>
26-
Protocol (tcp or udp), e.g. `DB_1_PORT_5432_TCP_PROTO=tcp`
28+
Protocol (tcp or udp), e.g. `DB_PORT_5432_TCP_PROTO=tcp`
2729

2830
<b><i>name</i>\_NAME</b><br>
2931
Fully qualified container name, e.g. `DB_1_NAME=/myapp_web_1/myapp_db_1`

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ from flask import Flask
5959
from redis import Redis
6060
import os
6161
app = Flask(__name__)
62-
redis = Redis(host="redis_1", port=6379)
62+
redis = Redis(host='redis', port=6379)
6363
6464
@app.route('/')
6565
def hello():

docs/rails.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Open up your newly-generated `database.yml`. Replace its contents with the follo
7373
pool: 5
7474
username: postgres
7575
password:
76-
host: db_1
76+
host: db
7777

7878
test:
7979
<<: *default

docs/wordpress.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Two supporting files are needed to get this working - first up, `wp-config.php`
4444
define('DB_NAME', 'wordpress');
4545
define('DB_USER', 'root');
4646
define('DB_PASSWORD', '');
47-
define('DB_HOST', getenv("DB_1_PORT_3306_TCP_ADDR") . ":" . getenv("DB_1_PORT_3306_TCP_PORT"));
47+
define('DB_HOST', "db:3306");
4848
define('DB_CHARSET', 'utf8');
4949
define('DB_COLLATE', '');
5050

docs/yml.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ Override the default command.
3636
command: bundle exec thin -p 3000
3737
```
3838

39+
<a name="links"></a>
3940
### links
4041

41-
42-
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`
42+
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).
4343

4444
```
4545
links:
@@ -48,6 +48,16 @@ links:
4848
- redis
4949
```
5050

51+
An entry with the alias' name will be created in `/etc/hosts` inside containers for this service, e.g:
52+
53+
```
54+
172.17.2.186 db
55+
172.17.2.186 database
56+
172.17.2.187 redis
57+
```
58+
59+
Environment variables will also be created - see the [environment variable reference](env.html) for details.
60+
5161
### ports
5262

5363
Expose ports. Either specify both ports (`HOST:CONTAINER`), or just the container port (a random host port will be chosen).

fig/service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,12 @@ def _get_links(self, link_to_self):
273273
links = []
274274
for service, link_name in self.links:
275275
for container in service.containers():
276-
if link_name:
277-
links.append((container.name, link_name))
276+
links.append((container.name, link_name or service.name))
278277
links.append((container.name, container.name))
279278
links.append((container.name, container.name_without_project))
280279
if link_to_self:
281280
for container in self.containers():
281+
links.append((container.name, self.name))
282282
links.append((container.name, container.name))
283283
links.append((container.name, container.name_without_project))
284284
return links

tests/integration/service_test.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,29 +175,62 @@ def test_start_container_inherits_options_from_constructor(self):
175175
def test_start_container_creates_links(self):
176176
db = self.create_service('db')
177177
web = self.create_service('web', links=[(db, None)])
178+
179+
db.start_container()
178180
db.start_container()
179181
web.start_container()
180-
self.assertIn('figtest_db_1', web.containers()[0].links())
181-
self.assertIn('db_1', web.containers()[0].links())
182+
183+
self.assertEqual(
184+
set(web.containers()[0].links()),
185+
set([
186+
'figtest_db_1', 'db_1',
187+
'figtest_db_2', 'db_2',
188+
'db',
189+
]),
190+
)
182191

183192
def test_start_container_creates_links_with_names(self):
184193
db = self.create_service('db')
185194
web = self.create_service('web', links=[(db, 'custom_link_name')])
195+
196+
db.start_container()
186197
db.start_container()
187198
web.start_container()
188-
self.assertIn('custom_link_name', web.containers()[0].links())
199+
200+
self.assertEqual(
201+
set(web.containers()[0].links()),
202+
set([
203+
'figtest_db_1', 'db_1',
204+
'figtest_db_2', 'db_2',
205+
'custom_link_name',
206+
]),
207+
)
189208

190209
def test_start_normal_container_does_not_create_links_to_its_own_service(self):
191210
db = self.create_service('db')
192-
c1 = db.start_container()
193-
c2 = db.start_container()
194-
self.assertNotIn(c1.name, c2.links())
211+
212+
db.start_container()
213+
db.start_container()
214+
215+
c = db.start_container()
216+
self.assertEqual(set(c.links()), set([]))
195217

196218
def test_start_one_off_container_creates_links_to_its_own_service(self):
197219
db = self.create_service('db')
198-
c1 = db.start_container()
199-
c2 = db.start_container(one_off=True)
200-
self.assertIn(c1.name, c2.links())
220+
221+
db.start_container()
222+
db.start_container()
223+
224+
c = db.start_container(one_off=True)
225+
226+
self.assertEqual(
227+
set(c.links()),
228+
set([
229+
'figtest_db_1', 'db_1',
230+
'figtest_db_2', 'db_2',
231+
'db',
232+
]),
233+
)
201234

202235
def test_start_container_builds_images(self):
203236
service = Service(

0 commit comments

Comments
 (0)