mirror of
https://github.com/docker/compose.git
synced 2025-07-23 21:54:40 +02:00
Merge pull request #143 from orchardup/expose-option
Support 'expose' config option, like docker's --expose
This commit is contained in:
commit
a058c40dfb
@ -28,8 +28,6 @@ Simple enough. Finally, this is all tied together with a file called `fig.yml`.
|
|||||||
|
|
||||||
db:
|
db:
|
||||||
image: orchardup/postgresql
|
image: orchardup/postgresql
|
||||||
ports:
|
|
||||||
- "5432"
|
|
||||||
web:
|
web:
|
||||||
build: .
|
build: .
|
||||||
command: python manage.py runserver 0.0.0.0:8000
|
command: python manage.py runserver 0.0.0.0:8000
|
||||||
|
@ -33,8 +33,6 @@ web:
|
|||||||
- .:/code
|
- .:/code
|
||||||
db:
|
db:
|
||||||
image: orchardup/mysql
|
image: orchardup/mysql
|
||||||
ports:
|
|
||||||
- "3306:3306"
|
|
||||||
environment:
|
environment:
|
||||||
MYSQL_DATABASE: wordpress
|
MYSQL_DATABASE: wordpress
|
||||||
```
|
```
|
||||||
|
@ -44,6 +44,12 @@ ports:
|
|||||||
- "8000:8000"
|
- "8000:8000"
|
||||||
- "49100:22"
|
- "49100:22"
|
||||||
|
|
||||||
|
-- Expose ports without publishing them to the host machine - they'll only be
|
||||||
|
-- accessible to linked services. Only the internal port can be specified.
|
||||||
|
expose:
|
||||||
|
- "3000"
|
||||||
|
- "8000"
|
||||||
|
|
||||||
-- Map volumes from the host machine (HOST:CONTAINER).
|
-- Map volumes from the host machine (HOST:CONTAINER).
|
||||||
volumes:
|
volumes:
|
||||||
- cache/:/tmp/cache
|
- cache/:/tmp/cache
|
||||||
|
@ -70,6 +70,8 @@ class Container(object):
|
|||||||
for private, public in list(self.dictionary['NetworkSettings']['Ports'].items()):
|
for private, public in list(self.dictionary['NetworkSettings']['Ports'].items()):
|
||||||
if public:
|
if public:
|
||||||
ports.append('%s->%s' % (public[0]['HostPort'], private))
|
ports.append('%s->%s' % (public[0]['HostPort'], private))
|
||||||
|
else:
|
||||||
|
ports.append(private)
|
||||||
return ', '.join(ports)
|
return ', '.join(ports)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -39,7 +39,7 @@ class Service(object):
|
|||||||
if 'image' in options and 'build' in options:
|
if 'image' in options and 'build' in options:
|
||||||
raise ConfigError('Service %s has both an image and build path specified. A service can either be built to image or use an existing image, not both.' % name)
|
raise ConfigError('Service %s has both an image and build path specified. A service can either be built to image or use an existing image, not both.' % name)
|
||||||
|
|
||||||
supported_options = DOCKER_CONFIG_KEYS + ['build']
|
supported_options = DOCKER_CONFIG_KEYS + ['build', 'expose']
|
||||||
|
|
||||||
for k in options:
|
for k in options:
|
||||||
if k not in supported_options:
|
if k not in supported_options:
|
||||||
@ -246,9 +246,10 @@ class Service(object):
|
|||||||
|
|
||||||
container_options['name'] = self.next_container_name(one_off)
|
container_options['name'] = self.next_container_name(one_off)
|
||||||
|
|
||||||
if 'ports' in container_options:
|
if 'ports' in container_options or 'expose' in self.options:
|
||||||
ports = []
|
ports = []
|
||||||
for port in container_options['ports']:
|
all_ports = container_options.get('ports', []) + self.options.get('expose', [])
|
||||||
|
for port in all_ports:
|
||||||
port = str(port)
|
port = str(port)
|
||||||
if ':' in port:
|
if ':' in port:
|
||||||
port = port.split(':')[-1]
|
port = port.split(':')[-1]
|
||||||
|
@ -209,25 +209,30 @@ class ServiceTest(DockerClientTestCase):
|
|||||||
def test_start_container_creates_ports(self):
|
def test_start_container_creates_ports(self):
|
||||||
service = self.create_service('web', ports=[8000])
|
service = self.create_service('web', ports=[8000])
|
||||||
container = service.start_container().inspect()
|
container = service.start_container().inspect()
|
||||||
self.assertEqual(list(container['HostConfig']['PortBindings'].keys()), ['8000/tcp'])
|
self.assertEqual(list(container['NetworkSettings']['Ports'].keys()), ['8000/tcp'])
|
||||||
self.assertNotEqual(container['HostConfig']['PortBindings']['8000/tcp'][0]['HostPort'], '8000')
|
self.assertNotEqual(container['NetworkSettings']['Ports']['8000/tcp'][0]['HostPort'], '8000')
|
||||||
|
|
||||||
|
def test_expose_does_not_publish_ports(self):
|
||||||
|
service = self.create_service('web', expose=[8000])
|
||||||
|
container = service.start_container().inspect()
|
||||||
|
self.assertEqual(container['NetworkSettings']['Ports'], {'8000/tcp': None})
|
||||||
|
|
||||||
def test_start_container_creates_port_with_explicit_protocol(self):
|
def test_start_container_creates_port_with_explicit_protocol(self):
|
||||||
service = self.create_service('web', ports=['8000/udp'])
|
service = self.create_service('web', ports=['8000/udp'])
|
||||||
container = service.start_container().inspect()
|
container = service.start_container().inspect()
|
||||||
self.assertEqual(list(container['HostConfig']['PortBindings'].keys()), ['8000/udp'])
|
self.assertEqual(list(container['NetworkSettings']['Ports'].keys()), ['8000/udp'])
|
||||||
|
|
||||||
def test_start_container_creates_fixed_external_ports(self):
|
def test_start_container_creates_fixed_external_ports(self):
|
||||||
service = self.create_service('web', ports=['8000:8000'])
|
service = self.create_service('web', ports=['8000:8000'])
|
||||||
container = service.start_container().inspect()
|
container = service.start_container().inspect()
|
||||||
self.assertIn('8000/tcp', container['HostConfig']['PortBindings'])
|
self.assertIn('8000/tcp', container['NetworkSettings']['Ports'])
|
||||||
self.assertEqual(container['HostConfig']['PortBindings']['8000/tcp'][0]['HostPort'], '8000')
|
self.assertEqual(container['NetworkSettings']['Ports']['8000/tcp'][0]['HostPort'], '8000')
|
||||||
|
|
||||||
def test_start_container_creates_fixed_external_ports_when_it_is_different_to_internal_port(self):
|
def test_start_container_creates_fixed_external_ports_when_it_is_different_to_internal_port(self):
|
||||||
service = self.create_service('web', ports=['8001:8000'])
|
service = self.create_service('web', ports=['8001:8000'])
|
||||||
container = service.start_container().inspect()
|
container = service.start_container().inspect()
|
||||||
self.assertIn('8000/tcp', container['HostConfig']['PortBindings'])
|
self.assertIn('8000/tcp', container['NetworkSettings']['Ports'])
|
||||||
self.assertEqual(container['HostConfig']['PortBindings']['8000/tcp'][0]['HostPort'], '8001')
|
self.assertEqual(container['NetworkSettings']['Ports']['8000/tcp'][0]['HostPort'], '8001')
|
||||||
|
|
||||||
def test_scale(self):
|
def test_scale(self):
|
||||||
service = self.create_service('web')
|
service = self.create_service('web')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user