mirror of https://github.com/docker/compose.git
Merge pull request #364 from docker/non-numeric-link-alias
Non-numeric link alias
This commit is contained in:
commit
2827786886
|
@ -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:
|
||||
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
12
docs/env.md
12
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`.
|
||||
|
||||
<b><i>name</i>\_PORT</b><br>
|
||||
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`
|
||||
|
||||
<b><i>name</i>\_PORT\_<i>num</i>\_<i>protocol</i></b><br>
|
||||
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`
|
||||
|
||||
<b><i>name</i>\_PORT\_<i>num</i>\_<i>protocol</i>\_ADDR</b><br>
|
||||
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`
|
||||
|
||||
<b><i>name</i>\_PORT\_<i>num</i>\_<i>protocol</i>\_PORT</b><br>
|
||||
Exposed port number, e.g. `DB_1_PORT_5432_TCP_PORT=5432`
|
||||
Exposed port number, e.g. `DB_PORT_5432_TCP_PORT=5432`
|
||||
|
||||
<b><i>name</i>\_PORT\_<i>num</i>\_<i>protocol</i>\_PROTO</b><br>
|
||||
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`
|
||||
|
||||
<b><i>name</i>\_NAME</b><br>
|
||||
Fully qualified container name, e.g. `DB_1_NAME=/myapp_web_1/myapp_db_1`
|
||||
|
|
|
@ -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():
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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', '');
|
||||
|
||||
|
|
14
docs/yml.md
14
docs/yml.md
|
@ -36,10 +36,10 @@ Override the default command.
|
|||
command: bundle exec thin -p 3000
|
||||
```
|
||||
|
||||
<a name="links"></a>
|
||||
### 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).
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in New Issue