DockerManager
The DockerManager module encapsulates functionalities related to Docker container management, including creating, starting, stopping, and managing Docker containers used by DOLOST.
- class DOLOST.services.docker_manager.ContainerStatsManager[source]
Bases:
DockerManagerA class for managing container statistics retrieval and calculations.
Inherits from DockerManager.
- logger
The logger instance for logging messages.
- Type:
DolostLogger, optional
- image_name
The name of the Docker image.
- Type:
str, optional
- decoy_files
The name of the service files directory.
- Type:
str, optional
- dockerfile_path
The path to the Dockerfile.
- Type:
str, optional
- fetch_container_stats(container_id)[source]
Fetches stats for a specific container identified by its ID.
- calculate_cpu_percentage(precpu_stats, cpu_stats)[source]
Calculates the CPU usage percentage based on the provided CPU stats.
- calculate_memory_percentage(memory_stats)[source]
Calculates the memory usage percentage based on the provided memory stats.
- parse_container_stats(stats)[source]
Parses the raw container stats and returns a dictionary containing calculated statistics.
- calculate_cpu_percentage(precpu_stats, cpu_stats)[source]
Calculates the CPU usage percentage based on the provided CPU stats.
- Parameters:
precpu_stats (dict) – The previous CPU stats.
cpu_stats (dict) – The current CPU stats.
- Returns:
The CPU usage percentage.
- Return type:
float
- calculate_memory_percentage(memory_stats)[source]
Calculates the memory usage percentage based on the provided memory stats.
- Parameters:
memory_stats (dict) – The memory stats.
- Returns:
The memory usage percentage.
- Return type:
float
- class DOLOST.services.docker_manager.DockerManager(docker_client=None)[source]
Bases:
objectA class for managing the interactions with docker’s API
- _instance
Instance of the class.
- Type:
cls
- build_context(source_service_folder, tmp_location, dockerfile_used)[source]
Copy files to a temporary folder for Docker build context.
- Parameters:
source_service_folder (str) – Path to the source directory.
tmp_location (str) – Temporary directory to store the Docker build context.
dockerfile_used (str) – Path to the Dockerfile to be used.
- Returns:
None
- build_image(image_name=None, decoy_files=None, dockerfile_path='.')[source]
Build a Docker image based on the specified Dockerfile and service files.
- Parameters:
image_name (str, optional) – Name to tag the built image. If None, a default name will be used.
decoy_files (str, optional) – Path to the service files.
dockerfile_path (str, optional) – Path to the Dockerfile. Defaults to ‘.’.
- Returns:
The image’s id.
- Return type:
str
- check_client_configuration(docker_client)[source]
Check before configuring Docker client based on the provided configuration.
- Parameters:
docker_client – Configuration for connecting to Docker client
- Returns:
None
- clean_container(name)[source]
Stop and remove a Docker container with the specified name.
- Parameters:
name (str) – The name of the Docker container to be cleaned.
- Returns:
None
- configure_client(docker_client=None)[source]
Configure Docker client based on the provided configuration.
- Parameters:
docker_client – Configuration for connecting to Docker client
- Returns:
None
- connect_to_network(container_name, network_name, ipv4_address, gateway)[source]
Connect a Docker container to a network with the specified name, IP address, and gateway.
- Parameters:
container_name (str) – The name of the container to connect to the network.
network_name (str) – The name of the network.
ipv4_address (str) – The IPv4 address to assign to the container.
gateway (str) – The gateway IP address for the container’s network.
- Returns:
None
- create_network(network_name, subnet, gateway)[source]
Create a Docker network with the specified name, subnet, and gateway if it does not already exist.
- Parameters:
network_name (str) – The name of the network.
subnet (str) – The subnet in CIDR notation (e.g., ‘10.0.0.0/24’).
gateway (str) – The gateway IP address for the network.
- Returns:
None
- get_containers(all: bool = True)[source]
Get the docker containers
- Parameters:
all (bool) – Retrieve all the containers, active or inactive ones.
- Returns:
The list of containers
- Return type:
containers (list)
- get_current_client_config()[source]
Get the current Docker client configuration.
- Returns:
- The current Docker client configuration loaded from the config file,
or None if the config file does not exist.
- Return type:
dict or None
- classmethod get_instance(docker_client=None)[source]
Get an instance of the DockerManager class.
If an instance does not exist, create a new one. If a docker_client is provided, configure the Docker client and check the connection.
- Parameters:
docker_client (docker.client.DockerClient, optional) – An instance of DockerClient to use for interaction.
- Returns:
An instance of the DockerManager class.
- Return type:
- run_container(image_name, hostname, name, network_name=None, ipv4_address=None, subnet=None, gateway=None, ports=None)[source]
Run a Docker container based on the built image, with custom hostname and container name.
- Parameters:
image_name (str) – Name of the Docker image to run the container from.
hostname (str) – The container’s decoy hostname.
name (str) – The container’s name, used for management purposes.
network_name (str, optional) – The name of the network to connect the container to.
ipv4_address (str, optional) – The IPv4 address to assign to the container.
subnet (str, optional) – The subnet address to assign to the container’s network.
gateway (str, optional) – The gateway IP address for the container’s network.
ports (dict) – The ports configuration for mapping to containers.
- Returns:
None