Merge pull request #5709 from mnottale/exec-workdir

Add '--workdir' option to 'exec'.
This commit is contained in:
Joffrey F 2018-03-02 15:28:07 -08:00 committed by GitHub
commit 38f7fdfe89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -459,6 +459,7 @@ class TopLevelCommand(object):
instances of a service [default: 1] instances of a service [default: 1]
-e, --env KEY=VAL Set environment variables (can be used multiple times, -e, --env KEY=VAL Set environment variables (can be used multiple times,
not supported in API < 1.25) not supported in API < 1.25)
-w, --workdir DIR Path to workdir directory for this command.
""" """
environment = Environment.from_env_file(self.project_dir) environment = Environment.from_env_file(self.project_dir)
use_cli = not environment.get_boolean('COMPOSE_INTERACTIVE_NO_CLI') use_cli = not environment.get_boolean('COMPOSE_INTERACTIVE_NO_CLI')
@ -467,7 +468,12 @@ class TopLevelCommand(object):
detach = options.get('--detach') detach = options.get('--detach')
if options['--env'] and docker.utils.version_lt(self.project.client.api_version, '1.25'): 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: try:
container = service.get_container(number=index) container = service.get_container(number=index)
@ -487,6 +493,7 @@ class TopLevelCommand(object):
"user": options["--user"], "user": options["--user"],
"tty": tty, "tty": tty,
"stdin": True, "stdin": True,
"workdir": options["--workdir"],
} }
if docker.utils.version_gte(self.project.client.api_version, '1.25'): 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"]: for env_variable in options["--env"]:
args += ["--env", env_variable] args += ["--env", env_variable]
if options["--workdir"]:
args += ["--workdir", options["--workdir"]]
args += [container_id] args += [container_id]
args += command args += command
return args return args

View File

@ -1559,6 +1559,16 @@ class CLITestCase(DockerClientTestCase):
assert stdout == "operator\n" assert stdout == "operator\n"
assert stderr == "" 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() @v2_2_only()
def test_exec_service_with_environment_overridden(self): def test_exec_service_with_environment_overridden(self):
name = 'service' name = 'service'