Ensure that a container with the given name exists;
    if not, build a new container from the specified image.
    (`docker run`)

    name
        Name for the container
    image
        Image from which to build this container
    entrypoint
        Entrypoint of the container
    command
        command to execute while starting
    hostname
        hostname of the container
    user
        user to run docker as
    detach
        daemon mode
    entrypoint
        entrypoint to the container
    stdin_open
        let stdin open
    tty
        attach ttys
    mem_limit:
        memory size limit
    cpu_shares:
        cpu shares authorized
    ports
        ports redirections ({'222': {}})
    environment
        environment variable mapping ({'foo':'BAR'})
    dns
        list of DNS servers
    ports
        List of ports definitions, either:
            - a port to map
            - a mapping of mapping portInHost : PortInContainer
    volumes
        List of volumes
    volumes_from

    Returns
        container

    .. note::
        This command does not verify that the named container
        is running the specified image.

def installed(config,
              name,
              image,
              entrypoint=None,
              command=None,
              hostname=None,
              user=None,
              detach=True,
              stdin_open=False,
              tty=False,
              mem_limit=0,
              cpu_shares=None,
              ports=None,
              environment=None,
              dns=None,
              volumes=None,
              volumes_from=None,
              force=True,
              *args, **kwargs):
    '''
    Ensure that a container with the given name exists;
    if not, build a new container from the specified image.
    (`docker run`)

    name
        Name for the container
    image
        Image from which to build this container
    entrypoint
        Entrypoint of the container
    command
        command to execute while starting
    hostname
        hostname of the container
    user
        user to run docker as
    detach
        daemon mode
    entrypoint
        entrypoint to the container
    stdin_open
        let stdin open
    tty
        attach ttys
    mem_limit:
        memory size limit
    cpu_shares:
        cpu shares authorized
    ports
        ports redirections ({'222': {}})
    environment
        environment variable mapping ({'foo':'BAR'})
    dns
        list of DNS servers
    ports
        List of ports definitions, either:
            - a port to map
            - a mapping of mapping portInHost : PortInContainer
    volumes
        List of volumes
    volumes_from

    Returns
        container

    .. note::
        This command does not verify that the named container
        is running the specified image.
    '''
    iinfos = _get_image_infos(config, image)
    if not iinfos:
        utils.error("Image not found.")
        return None
    cinfos = _get_container_infos(config, name)
    force = (config["force"] if config.get("force") != None else force)
    if cinfos and force:
        remove_container(config, container=name,force=True)
        print "Old container removed."
    elif cinfos and (not force):
        print "Container found: %s."%cinfos.get("Id")
        return cinfos

    dports, dvolumes, denvironment, de = {}, [], {}, {}
    if not ports:
        ports = []
    if not volumes:
        volumes = []
    if isinstance(environment, dict):
        print "Setting environment ..."
        for k in environment:
            denvironment[u'%s' % k] = u'%s' % environment[k]
    if isinstance(environment, list):
        print "Setting environment ..."
        for p in environment:
            if isinstance(p, dict):
                for k in p:
                    denvironment[u'%s' % k] = u'%s' % p[k]
    if ports:
        print "Setting ports mapping ..."
        for p in ports:
            if not isinstance(p, dict):
                dports[str(p)] = {}
            else:
                for k in p:
                    dports[str(p)] = {}
    if volumes:
        print "Setting volumes ..."
        for p in volumes:
            vals = []
            if not isinstance(p, dict):
                vals.append('%s' % p)
            else:
                for k in p:
                    vals.append('{0}:{1}'.format(k, p[k]))
            dvolumes.extend(vals)
    container = create_container(
        config,
        image=image,
        command=command,
        entrypoint=entrypoint,
        hostname=hostname,
        user=user,
        detach=detach,
        stdin_open=stdin_open,
        tty=tty,
        mem_limit=mem_limit,
        ports=dports,
        environment=denvironment,
        dns=dns,
        volumes=dvolumes,
        volumes_from=volumes_from,
        name=name)

    if container:
        print "Container created, id: %s"%(container.get("Id"))
    else:
        utils.error("Couldn't create container.")
    return container