chi.container

The chi.container module exposes a functional interface for interacting with application containers.

Important

Currently, only the CHI@Edge site support container operations.

chi.container.create_container(name: str, image: str = None, image_driver: str = 'docker', device_profiles: list[str] = None, environment: dict = None, exposed_ports: list[str] = [], runtime: str = None, nets: list[dict] = None, network_id: str = None, network_name: str = 'containernet1', reservation_id: str = None, start: bool = True, start_timeout: int = None, **kwargs) Container[source]

Create a container instance.

Parameters
  • name (str) – The name to give the container.

  • image (str) – The Docker image, with or without tag information. If no tag is provided, “latest” is assumed.

  • image_driver (str) – The image storage driver to use to retrieve the image. Defaults to “docker”, meaning the image is assumed to be a Docker registry repository. Specify “glance” to launch a snapshot image by passing the Glance Image ID in the image argument.

  • device_profiles (list[str]) – An optional list of device profiles to request be configured on the container when it is created. Edge devices may have differing sets of supported device profiles, so it is important to understand which profiles are supported by the target device for your container.

  • environment (dict) – A set of environment variables to pass to the container.

  • exposed_ports (list[str]) – A list of ports to expose on the container. TCP or UDP can be provided with a slash prefix, e.g., “80/tcp” vs. “53/udp”. If no protocol is provided, TCP is assumed.

  • nets (list[dict]) – A set of network configurations. This is an advanced invocation; typically network_id or network_name should be enough, and is much simpler. Refer to the Zun documentation for information about this parameter.

  • network_id (str) – The ID of a network to launch the container on.

  • network_name (str) – The name of a network to launch the container on. This has no effect if network_id is already provided. Default “containernet1”.

  • host (str) – The Zun host to launch a container on. If not specified, the host is chosen by Zun.

  • runtime (str) – The container runtime to use. This should only be overridden when explicitly launching containers onto a host/platform requiring a separate runtime to, e.g., pass-through GPU devices, such as the “nvidia” runtime provided by NVIDIA Jetson Nano/TX2.

  • start (bool) – Whether to automatically start the container after it is created. Default True.

  • **kwargs – Additional keyword arguments to send to the Zun client’s container create call.

chi.container.destroy_container(container_ref: str)[source]

Delete the container.

This will automatically stop the container if it is currently running.

Parameters

container_ref (str) – The name or ID of the container.

chi.container.execute(container_ref: str, command: str) dict[source]

Execute a one-off process inside a running container.

Parameters
  • container_ref (str) – The name or ID of the container.

  • command (str) – The command to run.

Returns

A summary of the output of the command, with “output” and “exit_code”.

chi.container.get_container(container_ref: str) Container[source]

Get a container’s information.

Parameters
  • container_ref (str) – The name or ID of the container.

  • tag (str) – An optional version to tag the container image with. If not defined, defaults to “latest”.

Returns

The container, if found.

chi.container.get_logs(container_ref: str, stdout=True, stderr=True)[source]

Print all logs outputted by the container.

Parameters
  • container_ref (str) – The name or ID of the container.

  • stdout (bool) – Whether to include stdout logs. Default True.

  • stderr (bool) – Whether to include stderr logs. Default True.

Returns

A string containing all log output. Log lines will be delimited by

newline characters.

chi.container.list_containers() list[Container][source]

List all containers owned by this project.

Returns

A list of containers.

chi.container.snapshot_container(container_ref: str, repository: str, tag: str = 'latest') str[source]

Create a snapshot of a running container.

This will store the container’s file system in Glance as a new Image. You can then specify the Image ID in container create requests.

Parameters
  • container_ref (str) – The name or ID of the container.

  • repository (str) – The name to give the snapshot.

  • tag (str) – An optional version tag to give the snapshot. Defaults to “latest”.

chi.container.upload(container_ref: str, source: str, dest: str) dict[source]

Upload a file or directory to a running container.

Parameters
  • container_ref (str) – The name or ID of the container.

  • source (str) – The (local) path to the file or directory to upload.

  • dest (str) – The (container) path to upload the file or directory to.

chi.container.wait_for_active(container_ref: str, timeout: int = 120) Container[source]

Wait for a container to transition to the running state.

Parameters
  • container_ref (str) – The name or ID of the container.

  • timeout (int) – How long to wait before issuing a TimeoutError.

Raises

TimeoutError – if the timeout was reached before the container started.

Returns

The container representation.