Container

Container represents abstract API for container managers. It is now implemented by conu.backend.docker.image.DockerContainer

class conu.apidefs.container.Container(image, container_id, name)

Container class definition which contains abstract methods. The instances should call the constructor

__init__(image, container_id, name)
Parameters:
  • image – Image instance
  • container_id – str, unique identifier of this container
  • container_id – str, pretty container name
copy_from(src, dest)

copy a file or a directory from container to host system

Parameters:
  • src – str, path to a file or a directory within container or image
  • dest – str, path to a file or a directory on host system
Returns:

None

copy_to(src, dest)

copy a file or a directory from host system to a container

Parameters:
  • src – str, path to a file or a directory on host system
  • dest – str, path to a file or a directory within container
Returns:

None

delete(force=False, **kwargs)

remove this container; kwargs indicate that some container runtimes might accept more parameters

Parameters:force – bool, if container engine supports this, force the functionality
Returns:None
execute(command, **kwargs)

execute a command in this container

Parameters:
  • command – list of str, command to execute in the container
  • kwargs – specific parameters for container engines exec methods
Returns:

str (output) or iterator

exit_code()

get exit code of container. Return value is 0 for running and created containers

Returns:int
get_IPv4s()

Return all known IPv4 addresses of this container. It may be possible that the container has disabled networking: in that case, the list is empty

Returns:list of str
get_IPv6s()

Return all known IPv6 addresses of this container. It may be possible that the container has disabled networking: in that case, the list is empty

Returns:list of str
get_id()

get unique identifier of this container

Returns:str
get_image_name()

return name of the container image

Returns:str
get_metadata()

return general metadata for container

Returns:ContainerMetadata
get_pid()

get process identifier of the root process in the container

Returns:int
get_ports()

get ports specified in container metadata

Returns:list of str
get_status()

Get status of container

Returns:Status of container
http_client(host=None, port=None)

allow requests in context – e.g.:

with container.http_client(port="80", ...) as c:
    assert c.get("/api/...")
Parameters:
  • host – str, if None, set self.get_IPv4s()[0]
  • port – str or int, if None, set to self.get_ports()[0]
Returns:

instance of conu.utils.http_client.HttpClient

http_request(path='/', method='GET', host=None, port=None, json=False, data=None)

perform a HTTP request

Parameters:
  • path – str, path within the reqest, e.g. “/api/version”
  • method – str, HTTP method
  • host – str, if None, set self.get_IPv4s()[0]
  • port – str or int, if None, set to self.get_ports()[0]
  • json – bool, should we expect json?
  • data – data to send (can be dict, list, str)
Returns:

dict

inspect(refresh=False)

return cached metadata by default

Parameters:refresh – bool, returns up to date metadata if set to True
Returns:dict
is_port_open(port, timeout=10)

check if given port is open and receiving connections

Parameters:
  • port – int
  • timeout – int, how many seconds to wait for connection; defaults to 2
Returns:

True if the connection has been established inside timeout, False otherwise

is_running()

returns True if the container is running, this method should always ask the API and should not use a cached value

Returns:bool
kill(signal=None)

send a signal to this container (bear in mind that the process won’t have time to shutdown properly and your service may end up in an inconsistent state)

Parameters:signal – str or int, signal to use for killing the container (SIGKILL by default)
Returns:None
logs(follow=False)

Get logs from this container.

Parameters:follow – bool, provide new logs as they come
Returns:iterator
mount(mount_point=None)

mount container filesystem

Parameters:mount_point – str, directory where the filesystem will be mounted
Returns:instance of Filesystem
name()

Return name of this container.

Returns:str
open_connection(port=None)

open a TCP connection to service running in the container, if port is None and container exposes only a single port, connect to it, otherwise raise an exception

Parameters:port – int or None
Returns:socket
start()

start current container - the container has to be created

Returns:None
status()

Provide current, up-to-date status of this container. This method should not use cached value. Implementation of this method should clearly state list of possible values to get from this method

Returns:str
stop()

stop this container

Returns:None
wait(timeout)

Block until the container stops, then return its exit code.

Parameters:timeout – int, Request timeout
Returns:int, exit code