diff --git a/fig/cli/main.py b/fig/cli/main.py index 11230331cf5..8ffffde248d 100644 --- a/fig/cli/main.py +++ b/fig/cli/main.py @@ -215,6 +215,7 @@ def run(self, options): allocates a TTY. --rm Remove container after run. Ignored in detached mode. --no-deps Don't start linked services. + -w WORKDIR Working directory inside the container. """ service = self.project.get_service(options['SERVICE']) @@ -233,6 +234,8 @@ def run(self, options): if options['-d'] or options['-T'] or not sys.stdin.isatty(): tty = False + workdir = options['-w'] + if options['COMMAND']: command = [options['COMMAND']] + options['ARGS'] else: @@ -243,6 +246,8 @@ def run(self, options): 'tty': tty, 'stdin_open': not options['-d'], } + if workdir: + container_options['working_dir'] = workdir container = service.create_container(one_off=True, **container_options) if options['-d']: service.start_container(container, ports=None, one_off=True) diff --git a/tests/integration/cli_test.py b/tests/integration/cli_test.py index 92aed85fc4f..5f6b6108cd4 100644 --- a/tests/integration/cli_test.py +++ b/tests/integration/cli_test.py @@ -140,6 +140,22 @@ def test_run_does_not_recreate_linked_containers(self, __): self.assertEqual(old_ids, new_ids) + @patch('dockerpty.start') + def test_run_with_working_dir(self, __): + self.command.base_dir = 'tests/fixtures/links-figfile' + + for c in self.command.project.containers(stopped=True, one_off=True): + c.remove() + + self.command.dispatch(['run', '-w', '/tmp', 'web', '/bin/pwd'], None) + service = self.command.project.get_service('web') + containers = service.containers(stopped=True, one_off=True) + + self.assertEqual( + [c.inspect()['Config']['WorkingDir'] for c in containers], + [u'/tmp'], + ) + @patch('dockerpty.start') def test_run_without_command(self, __): self.command.base_dir = 'tests/fixtures/commands-figfile'