Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compose/cli/docopt_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def parse(self, argv, global_options):

def get_handler(self, command):
command = command.replace('-', '_')
# we certainly want to have "exec" command, since that's what docker client has
# but in python exec is a keyword
if command == "exec":
command = "exec_command"

if not hasattr(self, command):
raise NoSuchCommand(command, self)
Expand Down
54 changes: 53 additions & 1 deletion compose/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@


if not IS_WINDOWS_PLATFORM:
from dockerpty.pty import PseudoTerminal, RunOperation
from dockerpty.pty import PseudoTerminal, RunOperation, ExecOperation

log = logging.getLogger(__name__)
console_handler = logging.StreamHandler(sys.stderr)
Expand Down Expand Up @@ -152,6 +152,7 @@ class TopLevelCommand(DocoptCommand):
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
kill Kill containers
logs View output from containers
Expand Down Expand Up @@ -298,6 +299,57 @@ def json_format_event(event):
print(formatter(event))
sys.stdout.flush()

def exec_command(self, project, options):
"""
Execute a command in a running container

Usage: exec [options] SERVICE COMMAND [ARGS...]

Options:
-d Detached mode: Run command in the background.
--privileged Give extended privileges to the process.
--user USER Run the command as this user.
-T Disable pseudo-tty allocation. By default `docker-compose exec`
allocates a TTY.
--index=index index of the container if there are multiple
instances of a service [default: 1]
"""
index = int(options.get('--index'))
service = project.get_service(options['SERVICE'])
try:
container = service.get_container(number=index)
except ValueError as e:
raise UserError(str(e))
command = [options['COMMAND']] + options['ARGS']
tty = not options["-T"]

create_exec_options = {
"privileged": options["--privileged"],
"user": options["--user"],
"tty": tty,
"stdin": tty,
}

exec_id = container.create_exec(command, **create_exec_options)

if options['-d']:
container.start_exec(exec_id, tty=tty)
return

signals.set_signal_handler_to_shutdown()
try:
operation = ExecOperation(
project.client,
exec_id,
interactive=tty,
)
pty = PseudoTerminal(project.client, operation)
pty.start()
except signals.ShutdownException:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only raised if you register the signal handlers, but I don't see that happening here.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think even in the shutdown case you will want to use the exit code from the container, no?

log.info("received shutdown exception: closing")
exit_code = project.client.exec_inspect(exec_id).get("ExitCode")
sys.exit(exit_code)

def help(self, project, options):
"""
Get help on a command.
Expand Down
6 changes: 6 additions & 0 deletions compose/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ def restart(self, **options):
def remove(self, **options):
return self.client.remove_container(self.id, **options)

def create_exec(self, command, **options):
return self.client.exec_create(self.id, command, **options)

def start_exec(self, exec_id, **options):
return self.client.exec_start(exec_id, **options)

def rename_to_tmp_name(self):
"""Rename the container to a hopefully unique temporary container name
by prepending the short id.
Expand Down
29 changes: 29 additions & 0 deletions docs/reference/exec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!--[metadata]>
+++
title = "exec"
description = "exec"
keywords = ["fig, composition, compose, docker, orchestration, cli, exec"]
[menu.main]
identifier="exec.compose"
parent = "smn_compose_cli"
+++
<![end-metadata]-->

# exec

```
Usage: exec [options] SERVICE COMMAND [ARGS...]

Options:
-d Detached mode: Run command in the background.
--privileged Give extended privileges to the process.
--user USER Run the command as this user.
-T Disable pseudo-tty allocation. By default `docker-compose exec`
allocates a TTY.
--index=index index of the container if there are multiple
instances of a service [default: 1]
```

This is equivalent of `docker exec`. With this subcommand you can run arbitrary
commands in your services. Commands are by default allocating a TTY, so you can
do e.g. `docker-compose exec web sh` to get an interactive prompt.
18 changes: 18 additions & 0 deletions tests/acceptance/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,24 @@ def test_up_handles_abort_on_container_exit(self):
self.project.stop(['simple'])
wait_on_condition(ContainerCountCondition(self.project, 0))

def test_exec_without_tty(self):
self.base_dir = 'tests/fixtures/links-composefile'
self.dispatch(['up', '-d', 'console'])
self.assertEqual(len(self.project.containers()), 1)

stdout, stderr = self.dispatch(['exec', '-T', 'console', 'ls', '-1d', '/'])
self.assertEquals(stdout, "/\n")
self.assertEquals(stderr, "")

def test_exec_custom_user(self):
self.base_dir = 'tests/fixtures/links-composefile'
self.dispatch(['up', '-d', 'console'])
self.assertEqual(len(self.project.containers()), 1)

stdout, stderr = self.dispatch(['exec', '-T', '--user=operator', 'console', 'whoami'])
self.assertEquals(stdout, "operator\n")
self.assertEquals(stderr, "")

def test_run_service_without_links(self):
self.base_dir = 'tests/fixtures/links-composefile'
self.dispatch(['run', 'console', '/bin/true'])
Expand Down