def output_command(info, image_info, pretty=False):
    '''output as docker-run command format'''

    sep = pretty and ' \\\n    ' or ' '

    short_options = ''
    options = []

    container = info['Name'][1:]
    conf = info['Config']
    hconf = info['HostConfig']

    options.append("--name={name}".format(name=container))

    if not conf['AttachStdout']:
        short_options += 'd'

    if conf['OpenStdin']:
        short_options += 'i'

    if conf['Tty']:
        short_options += 't'

    if len(short_options) > 0:
        options.append('-' + short_options)

    options.append("-h {hostname}".format(hostname=conf['Hostname']))

    # Volumes
    if 'Binds' in hconf and isinstance(hconf['Binds'], list):
        for volume in hconf['Binds']:
            options.append("-v {volume}".format(volume=volume))

    # PortBindings
    if 'PortBindings' in hconf and isinstance(hconf['PortBindings'], dict):
        for port, hosts in hconf['PortBindings'].items():
            for host in hosts:
                portbinding = ''
                if 'HostIp' in host and host['HostIp']:
                    portbinding += host['HostIp'] + ':'
                if 'HostPort' in host and host['HostPort']:
                    portbinding += host['HostPort'] + ':'
                portbinding += port
                options.append("-p {portbinding}".format(portbinding=portbinding))

    # Devices
    if 'Devices' in hconf and isinstance(hconf['Devices'], list):
        for device in hconf['Devices']:
            options.append("--device={device}".format(device=device))

    # RestartPolicy
    if 'RestartPolicy' in hconf and hconf['RestartPolicy']['Name']:
        policy = hconf['RestartPolicy']['Name']
        if hconf['RestartPolicy']['MaximumRetryCount'] > 0:
            policy += ':' + str(hconf['RestartPolicy']['MaximumRetryCount'])
        options.append("--restart={policy}".format(policy=policy))

    # Privileged
    if hconf['Privileged']:
        options.append('--privileged')

    # DNS
    if 'Dns' in hconf and isinstance(hconf['Dns'], list):
        for dns in hconf['Dns']:
            options.append("-dns={dns}".format(dns=dns))

    # ExposedPorts
    if 'ExposedPorts' in conf and isinstance(conf['ExposedPorts'], dict):
        for port, _ in conf['ExposedPorts'].items():
            if ('ExposedPorts' not in image_info['Config'] or
                    port not in image_info['Config']['ExposedPorts']):
                options.append("--expose={port}".format(port=port))

    # Env
    if isinstance(conf['Env'], list):
        for env in conf['Env']:
            if env not in image_info['Config']['Env']:
                options.append("-e {env}".format(env=pipes.quote(env)))

    # EntryPoint
    if conf['Entrypoint'] != image_info['Config']['Entrypoint']:
        entry = []
        if isinstance(conf['Entrypoint'], list):
            for entry_item in conf['Entrypoint']:
                entry.append(pipes.quote(entry_item))
        elif isinstance(conf['Entrypoint'], str):
            entry.append(pipes.quote(conf['Entrypoint']))
        if len(entry) > 0:
            options.append("--entrypoint={entry}".format(entry=pipes.quote(' '.join(entry))))

    # WorkingDir
    if image_info['Config']['WorkingDir'] != conf['WorkingDir']:
        options.append("-w {dir}".format(dir=pipes.quote(conf['WorkingDir'])))

    # User
    if conf['User'] and image_info['Config']['User'] != conf['User']:
        options.append("-u {user}".format(user=pipes.quote(conf['User'])))

    # Cmd
    cmd = []
    if conf['Cmd'] != image_info['Config']['Cmd']:
        if isinstance(conf['Cmd'], list):
            for cmd_item in conf['Cmd']:
                cmd.append(pipes.quote(cmd_item))
        elif isinstance(conf['Cmd'], str):
            cmd.append(pipes.quote(conf['Cmd']))

    print('# docker-run command for {container}'.format(container=container))
    cmd_str = 'docker run{sep}{options}{sep}{image}'.format(
        options=sep.join(options), sep=sep, image=conf['Image'])

    if len(cmd) > 0:
        cmd_str += ' ' + ' '.join(cmd)

    print(cmd_str)
    print()
def output_command ( info , image_info , pretty = False ) : sep = pretty and ' \\\n    ' or ' ' short_options = '' options = [ ] container = info [ 'Name' ] [ 1 : ] conf = info [ 'Config' ] hconf = info [ 'HostConfig' ] options . append ( "--name={name}" . format ( name = container ) ) if not conf [ 'AttachStdout' ] : short_options += 'd' if conf [ 'OpenStdin' ] : short_options += 'i' if conf [ 'Tty' ] : short_options += 't' if len ( short_options ) > 0 : options . append ( '-' + short_options ) options . append ( "-h {hostname}" . format ( hostname = conf [ 'Hostname' ] ) ) if 'Binds' in hconf and isinstance ( hconf [ 'Binds' ] , list ) : for volume in hconf [ 'Binds' ] : options . append ( "-v {volume}" . format ( volume = volume ) ) if 'PortBindings' in hconf and isinstance ( hconf [ 'PortBindings' ] , dict ) : for port , hosts in hconf [ 'PortBindings' ] . items ( ) : for host in hosts : portbinding = '' if 'HostIp' in host and host [ 'HostIp' ] : portbinding += host [ 'HostIp' ] + ':' if 'HostPort' in host and host [ 'HostPort' ] : portbinding += host [ 'HostPort' ] + ':' portbinding += port options . append ( "-p {portbinding}" . format ( portbinding = portbinding ) ) if 'Devices' in hconf and isinstance ( hconf [ 'Devices' ] , list ) : for device in hconf [ 'Devices' ] : options . append ( "--device={device}" . format ( device = device ) ) if 'RestartPolicy' in hconf and hconf [ 'RestartPolicy' ] [ 'Name' ] : policy = hconf [ 'RestartPolicy' ] [ 'Name' ] if hconf [ 'RestartPolicy' ] [ 'MaximumRetryCount' ] > 0 : policy += ':' + str ( hconf [ 'RestartPolicy' ] [ 'MaximumRetryCount' ] ) options . append ( "--restart={policy}" . format ( policy = policy ) ) if hconf [ 'Privileged' ] : options . append ( '--privileged' ) if 'Dns' in hconf and isinstance ( hconf [ 'Dns' ] , list ) : for dns in hconf [ 'Dns' ] : options . append ( "-dns={dns}" . format ( dns = dns ) ) if 'ExposedPorts' in conf and isinstance ( conf [ 'ExposedPorts' ] , dict ) : for port , _ in conf [ 'ExposedPorts' ] . items ( ) : if ( 'ExposedPorts' not in image_info [ 'Config' ] or port not in image_info [ 'Config' ] [ 'ExposedPorts' ] ) : options . append ( "--expose={port}" . format ( port = port ) ) if isinstance ( conf [ 'Env' ] , list ) : for env in conf [ 'Env' ] : if env not in image_info [ 'Config' ] [ 'Env' ] : options . append ( "-e {env}" . format ( env = pipes . quote ( env ) ) ) if conf [ 'Entrypoint' ] != image_info [ 'Config' ] [ 'Entrypoint' ] : entry = [ ] if isinstance ( conf [ 'Entrypoint' ] , list ) : for entry_item in conf [ 'Entrypoint' ] : entry . append ( pipes . quote ( entry_item ) ) elif isinstance ( conf [ 'Entrypoint' ] , str ) : entry . append ( pipes . quote ( conf [ 'Entrypoint' ] ) ) if len ( entry ) > 0 : options . append ( "--entrypoint={entry}" . format ( entry = pipes . quote ( ' ' . join ( entry ) ) ) ) if image_info [ 'Config' ] [ 'WorkingDir' ] != conf [ 'WorkingDir' ] : options . append ( "-w {dir}" . format ( dir = pipes . quote ( conf [ 'WorkingDir' ] ) ) ) if conf [ 'User' ] and image_info [ 'Config' ] [ 'User' ] != conf [ 'User' ] : options . append ( "-u {user}" . format ( user = pipes . quote ( conf [ 'User' ] ) ) ) cmd = [ ] if conf [ 'Cmd' ] != image_info [ 'Config' ] [ 'Cmd' ] : if isinstance ( conf [ 'Cmd' ] , list ) : for cmd_item in conf [ 'Cmd' ] : cmd . append ( pipes . quote ( cmd_item ) ) elif isinstance ( conf [ 'Cmd' ] , str ) : cmd . append ( pipes . quote ( conf [ 'Cmd' ] ) ) print ( '# docker-run command for {container}' . format ( container = container ) ) cmd_str = 'docker run{sep}{options}{sep}{image}' . format ( options = sep . join ( options ) , sep = sep , image = conf [ 'Image' ] ) if len ( cmd ) > 0 : cmd_str += ' ' + ' ' . join ( cmd ) print ( cmd_str ) print ( )
output as docker-run command format