Add '--workdir' option to 'exec'.

Signed-off-by: Matthieu Nottale <matthieu.nottale@docker.com>
This commit is contained in:
Matthieu Nottale 2018-02-23 16:33:01 +01:00
parent ad683b2d8d
commit 2fed399881
2 changed files with 21 additions and 1 deletions

View File

@ -435,6 +435,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')
@ -443,7 +444,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)
@ -460,6 +466,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'):
@ -1392,6 +1399,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

View File

@ -1521,6 +1521,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'