idmtools_platform_local.infrastructure.base_service_container module

idmtools base service container.

This defined the base docker container. We use this amongst each service to build our containers as needed.

Copyright 2021, Bill & Melinda Gates Foundation. All rights reserved.

class idmtools_platform_local.infrastructure.base_service_container.BaseServiceContainer(container_name: Optional[str] = None, image: Optional[str] = None, client: Optional[docker.client.DockerClient] = None, config_prefix: Optional[str] = None, network: Optional[str] = None)

Bases: abc.ABC

Providers base abstract class for local platform container objects/managers.

container_name: str = None
image: str = None
client: docker.client.DockerClient = None
config_prefix: str = None
network: str = None
static get_common_config(container_name: str, image: str, network: str, port_bindings: Optional[Dict] = None, volumes: Optional[Dict] = None, mem_limit: Optional[str] = None, mem_reservation: Optional[str] = None, environment: Optional[List[str]] = None, extra_labels: Optional[Dict] = None, **extras) dict

Returns portions of docker container configs that are common between all the different containers used within our platform.

Parameters:
  • container_name – Container name

  • image – Image to use

  • network – Network to use

  • port_bindings – Port binding

  • volumes – Volume definitions

  • mem_limit – Memory limit

  • mem_reservation – Memory reservation

  • environment – Environment vars

  • extra_labels – Extra labels to use

  • **extras

Returns:

Common configuration object to use when creating a container.

Notes

Memory strings should match those used by docker. See –memory details at https://docs.docker.com/engine/reference/run/#runtime-constraints-on-resources

get() Optional[docker.models.containers.Container]

Get the container.

Returns:

Container if it is running.

get_or_create(spinner=None) docker.models.containers.Container

Get or Create a container.

Parameters:

spinner – Optional spinner to display

Returns:

Docker container object representing service container

ensure_container_is_running(container: docker.models.containers.Container, spinner=None) docker.models.containers.Container

Ensures is running.

Parameters:
  • container – container to check if it is running

  • spinner – Optional spinner to show we are busy while checking.

Returns:

Container

has_different_config(container, show_diff: bool = True)

Detect if the config is difference that running container.

Parameters:
  • container – Container

  • show_diff – Should we display diff

Returns:

True if there is differing configuration

get_running_config(container)

Fetches the config used to start a container.

Parameters:

container – Container to use

Returns:

The config from the running container.

static copy_config_to_container(container: docker.models.containers.Container, config: dict)

Copy the configuration used to create container to a container for future reference.

Parameters:
  • container – Target container

  • config – Config to copy

Returns:

None

create(spinner=None) docker.models.containers.Container

Create our container.

Parameters:

spinner – Optional spinner. When provided, we will use it to indicate we are busy during long-running tasks.

Returns:

Created Container.

Raises:
  • EnvironmentError - If a container tries to start with a port in use

  • ValueError - Unkown cause

Notes

  • TODO - Add Exception for environment error with doc_link

static wait_on_status(container, sleep_interval: float = 0.2, max_time: float = 2, statutes_to_wait_for: Optional[List[str]] = None)

Wait on a container to achieve a specific status.

Parameters:
  • container – Target container

  • sleep_interval – How long to wait between checks

  • max_time – Max time to wait(default to 2 seconds)

  • statutes_to_wait_for – List of statuss to wait for. When not set, we use starting and created.

Returns:

None

stop(remove=False, container: Optional[docker.models.containers.Container] = None)

Stop a container.

Parameters:
  • remove – When true, the container will be removed after being stopped

  • container – Container to stop

Returns:

None

restart(container: Optional[docker.models.containers.Container] = None)

Restart a container.

Parameters:

container – Container to restart.

Returns:

None

get_logs()

Get logs from container.

Returns:

Container logs as a string.

abstract get_configuration() Dict

Get configuration.

Each sub-class should define this to provide their own specific configuration.

Returns:

Configuration dict

__init__(container_name: Optional[str] = None, image: Optional[str] = None, client: Optional[docker.client.DockerClient] = None, config_prefix: Optional[str] = None, network: Optional[str] = None) None