def stop(config, container, timeout=10, *args, **kwargs): ''' Stop a running container :type container: string :param container: The container id to stop :type timeout: int :param timeout: Wait for a timeout to let the container exit gracefully before killing it :rtype: dict :returns: boolean ''' err = "Unknown" client = _get_client(config) try: dcontainer = _get_container_infos(config, container)['Id'] if is_running(config, dcontainer): client.stop(dcontainer, timeout=timeout) if not is_running(config, dcontainer): print "Container stopped." return True else: i = 0 while is_running(config, dcontainer): time.sleep(0.1) if i > 100: return kill(config,container) i += 1 return True else: return True except Exception as e: err = e utils.warning("Container not existing") return True
def running(config, containers, image, tag=None, entrypoint=None, command=None, environment=None, ports=None, volumes=None, mem_limit=0, cpu_shares=None, binds=None, publish_all_ports=False, links=None, port_bindings=None, force=True, hostname=None, *args, **kwargs): ''' Ensure that a container is running. (`docker inspect`) containers name of the containers to start image name of the image to start the container from entrypoint Entrypoint of the container command command to execute while starting environment environment variable mapping ({'foo':'BAR'}) ports List of ports definitions, either: - a port to map - a mapping of mapping portInHost : PortInContainer volumes List of volumes mem_limit: memory size limit cpu_shares: cpu shares authorized binds like -v of docker run command publish_all_ports links Link several container together port_bindings List of ports to expose on host system - a mapping port's guest, hostname's host and port's host. Returns Container Id ''' if not containers: utils.warning("No container specified") return [], True if ports and port_bindings: (ports,port_bindings) = _gen_ports(ports,port_bindings,len(containers)) if not ports or not port_bindings: utils.error("Unable to generate port bindings (is there enough space between each allocation required?)") return [], True containers_out = [] failure = False if tag: image = "%s:%s"%(image,tag) for container in containers: port = (ports.pop() if ports else None) port_binding = (port_bindings.pop() if port_bindings else None) ret = installed(config, container,image,entrypoint=entrypoint,command=command,environment=environment,ports=port, volumes=volumes,mem_limit=mem_limit,cpu_shares=cpu_shares,force=force,hostname=hostname) if ret: started = start(config, container, binds=binds, port_bindings=port_binding, publish_all_ports=publish_all_ports, links=links) if is_running(config, container) and started: print "Container started, id: %s"%started.get("Id") containers_out.append(started) else: failure = True utils.error("Unable to run container: %s - can't start"%container) else: failure = True utils.error("Unable to run container: %s - can't install"%container) return containers_out, failure