diff --git a/compose/cli/main.py b/compose/cli/main.py index 5e077abe9..59f111184 100644 --- a/compose/cli/main.py +++ b/compose/cli/main.py @@ -459,6 +459,7 @@ class TopLevelCommand(object): instances of a service [default: 1] -e, --env KEY=VAL Set environment variables (can be used multiple times, not supported in API < 1.25) + -w, --workdir DIR Path to workdir directory for this command. """ environment = Environment.from_env_file(self.project_dir) use_cli = not environment.get_boolean('COMPOSE_INTERACTIVE_NO_CLI') @@ -467,7 +468,12 @@ class TopLevelCommand(object): detach = options.get('--detach') if options['--env'] and docker.utils.version_lt(self.project.client.api_version, '1.25'): - raise UserError("Setting environment for exec is not supported in API < 1.25'") + raise UserError("Setting environment for exec is not supported in API < 1.25 (%s)" + % self.project.client.api_version) + + if options['--workdir'] and docker.utils.version_lt(self.project.client.api_version, '1.35'): + raise UserError("Setting workdir for exec is not supported in API < 1.35 (%s)" + % self.project.client.api_version) try: container = service.get_container(number=index) @@ -487,6 +493,7 @@ class TopLevelCommand(object): "user": options["--user"], "tty": tty, "stdin": True, + "workdir": options["--workdir"], } if docker.utils.version_gte(self.project.client.api_version, '1.25'): @@ -1453,6 +1460,9 @@ def build_exec_command(options, container_id, command): for env_variable in options["--env"]: args += ["--env", env_variable] + if options["--workdir"]: + args += ["--workdir", options["--workdir"]] + args += [container_id] args += command return args diff --git a/tests/acceptance/cli_test.py b/tests/acceptance/cli_test.py index 7a0f22fc3..45e9a54e5 100644 --- a/tests/acceptance/cli_test.py +++ b/tests/acceptance/cli_test.py @@ -1559,6 +1559,16 @@ class CLITestCase(DockerClientTestCase): assert stdout == "operator\n" assert stderr == "" + @v3_only() + def test_exec_workdir(self): + self.base_dir = 'tests/fixtures/links-composefile' + os.environ['COMPOSE_API_VERSION'] = '1.35' + self.dispatch(['up', '-d', 'console']) + assert len(self.project.containers()) == 1 + + stdout, stderr = self.dispatch(['exec', '-T', '--workdir', '/etc', 'console', 'ls']) + assert 'passwd' in stdout + @v2_2_only() def test_exec_service_with_environment_overridden(self): name = 'service'