def run_code_in_container(cli, image, code, mount, entrypoint):
    """ Run `code` in a container, returning its ID
    """
    kwargs = {
        'image': image,
    }

    if entrypoint:
        kwargs['entrypoint'] = '/bin/bash'
        kwargs['command'] = '-c {}'.format(quote(code))
    else:
        kwargs['command'] = '/bin/bash -c {}'.format(quote(code))

    if mount:
        binds = []
        volumes = []

        for m in mount:
            part = m.split(':')

            if len(part) == 3:
                pass
            elif len(part) == 2:
                part.append('rw')
            else:
                raise

            src, target, mode = part
            src = os.path.abspath(os.path.expanduser(src))

            binds.append('{}:{}:{}'.format(src, target, mode))
            volumes.append(target)

        kwargs['host_config'] = cli.create_host_config(binds=binds)
        kwargs['volumes'] = volumes

    container = cli.create_container(**kwargs)
    container_id = container['Id']

    cli.start(container=container_id)

    return container_id
def run_code_in_container ( cli , image , code , mount , entrypoint ) : kwargs = { 'image' : image , } if entrypoint : kwargs [ 'entrypoint' ] = '/bin/bash' kwargs [ 'command' ] = '-c {}' . format ( quote ( code ) ) else : kwargs [ 'command' ] = '/bin/bash -c {}' . format ( quote ( code ) ) if mount : binds = [ ] volumes = [ ] for m in mount : part = m . split ( ':' ) if len ( part ) == 3 : pass elif len ( part ) == 2 : part . append ( 'rw' ) else : raise src , target , mode = part src = os . path . abspath ( os . path . expanduser ( src ) ) binds . append ( '{}:{}:{}' . format ( src , target , mode ) ) volumes . append ( target ) kwargs [ 'host_config' ] = cli . create_host_config ( binds = binds ) kwargs [ 'volumes' ] = volumes container = cli . create_container ( ** kwargs ) container_id = container [ 'Id' ] cli . start ( container = container_id ) return container_id
Run `code` in a container, returning its ID