From cc834aa5642f2648a18a035478e03a5ce42e8e3e Mon Sep 17 00:00:00 2001 From: Stephen Quebe Date: Mon, 15 Sep 2014 18:18:03 -0400 Subject: [PATCH] Added map service ports option for run command. When using the fig run command, ports defined in fig.yml can be mapped to the host computer using the -service-ports option. Signed-off-by: Stephen Quebe --- docs/cli.md | 5 ++++- fig/cli/main.py | 10 ++++++++-- tests/integration/cli_test.py | 37 +++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/docs/cli.md b/docs/cli.md index 822f1b780..74c82bede 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -77,7 +77,7 @@ For example: By default, linked services will be started, unless they are already running. -One-off commands are started in new containers with the same config as a normal container for that service, so volumes, links, etc will all be created as expected. The only thing different to a normal container is the command will be overridden with the one specified and no ports will be created in case they collide. +One-off commands are started in new containers with the same config as a normal container for that service, so volumes, links, etc will all be created as expected. The only thing different to a normal container is the command will be overridden with the one specified and by default no ports will be created in case they collide. Links are also created between one-off commands and the other containers for that service so you can do stuff like this: @@ -87,6 +87,9 @@ If you do not want linked containers to be started when running the one-off comm $ fig run --no-deps web python manage.py shell +If you want the service's ports to be created and mapped to the host, specify the `--service-ports` flag: + $ fig run --service-ports web python manage.py shell + ### scale Set number of containers to run for a service. diff --git a/fig/cli/main.py b/fig/cli/main.py index 2c6a04020..2ba411f71 100644 --- a/fig/cli/main.py +++ b/fig/cli/main.py @@ -278,6 +278,8 @@ class TopLevelCommand(Command): -e KEY=VAL Set an environment variable (can be used multiple times) --no-deps Don't start linked services. --rm Remove container after run. Ignored in detached mode. + --service-ports Run command with the service's ports enabled and mapped + to the host. -T Disable pseudo-tty allocation. By default `fig run` allocates a TTY. """ @@ -325,11 +327,15 @@ class TopLevelCommand(Command): insecure_registry=insecure_registry, **container_options ) + + service_ports = None + if options['--service-ports']: + service_ports = service.options['ports'] if options['-d']: - service.start_container(container, ports=None, one_off=True) + service.start_container(container, ports=service_ports, one_off=True) print(container.name) else: - service.start_container(container, ports=None, one_off=True) + service.start_container(container, ports=service_ports, one_off=True) dockerpty.start(project.client, container.id, interactive=not options['-T']) exit_code = container.wait() if options['--rm']: diff --git a/tests/integration/cli_test.py b/tests/integration/cli_test.py index f03d72d2d..cdeca9212 100644 --- a/tests/integration/cli_test.py +++ b/tests/integration/cli_test.py @@ -237,6 +237,43 @@ class CLITestCase(DockerClientTestCase): # make sure a value with a = don't crash out self.assertEqual('moto=bobo', container.environment['allo']) + @patch('dockerpty.start') + def test_run_service_without_map_ports(self, __): + # create one off container + self.command.base_dir = 'tests/fixtures/ports-figfile' + self.command.dispatch(['run', '-d', 'simple'], None) + container = self.project.get_service('simple').containers(one_off=True)[0] + + # get port information + port_random = container.get_local_port(3000) + port_assigned = container.get_local_port(3001) + + # close all one off containers we just created + container.stop() + + # check the ports + self.assertEqual(port_random, None) + self.assertEqual(port_assigned, None) + + @patch('dockerpty.start') + def test_run_service_with_map_ports(self, __): + # create one off container + self.command.base_dir = 'tests/fixtures/ports-figfile' + self.command.dispatch(['run', '-d', '--service-ports', 'simple'], None) + container = self.project.get_service('simple').containers(one_off=True)[0] + + # get port information + port_random = container.get_local_port(3000) + port_assigned = container.get_local_port(3001) + + # close all one off containers we just created + container.stop() + + # check the ports + self.assertNotEqual(port_random, None) + self.assertIn("0.0.0.0", port_random) + self.assertEqual(port_assigned, "0.0.0.0:9999") + def test_rm(self): service = self.project.get_service('simple') service.create_container()