'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.
-u, --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]'

def exec_command(self, options):

    index = int(options.get(u'--index'))
    service = self.project.get_service(options[u'SERVICE'])
    detach = options[u'-d']
    try:
        container = service.get_container(number=index)
    except ValueError as e:
        raise UserError(str(e))
    command = ([options[u'COMMAND']] + options[u'ARGS'])
    tty = (not options[u'-T'])
    if (IS_WINDOWS_PLATFORM and (not detach)):
        args = [u'exec']
        if options[u'-d']:
            args += [u'--detach']
        else:
            args += [u'--interactive']
        if (not options[u'-T']):
            args += [u'--tty']
        if options[u'--privileged']:
            args += [u'--privileged']
        if options[u'--user']:
            args += [u'--user', options[u'--user']]
        args += [container.id]
        args += command
        sys.exit(call_docker(args))
    create_exec_options = {u'privileged': options[u'--privileged'], u'user': options[u'--user'], u'tty': tty, u'stdin': tty}
    exec_id = container.create_exec(command, **create_exec_options)
    if detach:
        container.start_exec(exec_id, tty=tty, stream=True)
        return
    signals.set_signal_handler_to_shutdown()
    try:
        operation = ExecOperation(self.project.client, exec_id, interactive=tty)
        pty = PseudoTerminal(self.project.client, operation)
        pty.start()
    except signals.ShutdownException:
        log.info(u'received    shutdown    exception:    closing')
    exit_code = self.project.client.exec_inspect(exec_id).get(u'ExitCode')
    sys.exit(exit_code)
