Docker control with python
Project description
Wcontainer
Wcontainer is a library for specific docker container operations. The library offers a number of modules that make using docker containers easier and simpler.
Description
Wcontainer is a library for specific docker container operations. The library offers a number of modules that make using docker containers easier and simpler.
Installation
To install the library, use pip:
pip install wcontainer
Description
The wcontainer library offers a number of modules that make using docker containers easier and simpler. The library is designed to be used in conjunction with the docker library, which is the official Python client for the Docker Engine API.
Examples
This directory contains a collection of examples that demonstrate the usage of various modules and functionalities in this project. Each subfolder corresponds to a specific module and includes example scripts to help you understand how to use that module.
Directory Structure
The examples are organized as follows:
examples/
4-monitoreo_RT/
monitoreo_tiempo_real.py
3-avanzados/
1_ajuste_dinamico_recursos.py
2_escalar_contenedores_segun_uso_recursos.py
3_escalar_antes_desplegar.py
4_autonamizar_generacion_informe_errores.py
5_usar_ia_detectar_fallos_para_reinicio_automatico.py
2-intermedios/
1_buscar_vulnerabilidades.py
2_read_metrics.py
3_informe de errores.py
1-basicos/
1_list_container.py
2_start_new_container.py
3_new_resources.py
How to Use
- Navigate to the module folder of interest, e.g.,
examples/module1/. - Open the
README.mdin that folder to get detailed information about the examples. - Run the scripts directly using:
python example1.py
Modules and Examples
1-basicos
Description
This module demonstrates specific functionalities.
- 1_list_container.py: Example demonstrating functionality.
# ✅ 1.1 Listar contenedores en ejecución
from wcontainer import Wcontainer
wc = Wcontainer(verbose=True)
# List only running containers
print(wc.list_containers())
# List all containers (including stopped ones)
print(wc.list_containers(all_containers=True))
- 2_start_new_container.py: Example demonstrating functionality.
# ✅ 1.2 Iniciar un contenedor con parámetros básicos
from wcontainer import Wcontainer
wc = Wcontainer(verbose=True)
# Start an Nginx container exposing port 8080
print(wc.start_container("nginx", "my_nginx", ports={"80/tcp": 8080}))
- 3_new_resources.py: Example demonstrating functionality.
# ✅ 1.3 Ajustar recursos de un contenedor en tiempo real
from wcontainer import Wcontainer
wc = Wcontainer(verbose=True)
# Limit a running container to 2 CPU cores and 1GB RAM
print(wc.adjust_container_resources("my_nginx", cpu_limit=2.0, memory_limit="1g"))
2-intermedios
Description
This module demonstrates specific functionalities.
- 1_buscar_vulnerabilidades.py: Example demonstrating functionality.
# ✅ 2.2 Escanear una imagen Docker en busca de vulnerabilidades con Trivy
from wcontainer import Wcontainer
wc = Wcontainer(verbose=True)
# Scan the latest Nginx image for security vulnerabilities
print(wc.scan_image_with_trivy("nginx"))
- 2_read_metrics.py: Example demonstrating functionality.
# ✅ 2.1 Obtener métricas de uso de CPU, RAM y GPU
from wcontainer import Wcontainer
wc = Wcontainer(verbose=True)
# Get system-wide CPU, RAM, and GPU usage
print(wc.get_resource_metrics())
- 3_informe de errores.py: Example demonstrating functionality.
# ✅ 2.3 Generar un informe de errores de un contenedor
from wcontainer import Wcontainer
wc = Wcontainer(verbose=True)
# Analyze logs and generate a JSON error report for the container
print(wc.generate_error_report("my_nginx", output_file="nginx_errors.json"))
3-avanzados
Description
This module demonstrates specific functionalities.
- 1_ajuste_dinamico_recursos.py: Example demonstrating functionality.
# ✅ 3.1 Autoajuste dinámico de recursos
import random
import time
from wcontainer import Wcontainer
wc = Wcontainer(verbose=True)
# Dynamically adjust CPU and RAM allocation every 10 seconds
for _ in range(5):
print(
wc.adjust_container_resources(
"my_nginx", cpu_limit=random.uniform(1.0, 4.0), memory_limit="512m"
)
)
time.sleep(10)
# 🔹 Explicación: Cada 10 segundos, el script cambia la cantidad de CPU y RAM asignada al contenedor.
- 2_escalar_contenedores_segun_uso_recursos.py: Example demonstrating functionality.
# ✅ 3.2 Detectar uso alto de recursos y escalar contenedores
from wcontainer import Wcontainer
wc = Wcontainer(verbose=True)
resource_metrics = wc.get_resource_metrics()
# If CPU usage is higher than 80%, start another instance
if float(resource_metrics["cpu_usage"].strip("%")) > 80:
print(wc.start_container("nginx", "nginx_replica"))
# 🔹 Explicación: Si el uso de CPU del sistema supera el 80%, lanza una nueva instancia del contenedor para distribuir la carga.
- 3_escalar_antes_desplegar.py: Example demonstrating functionality.
# ✅ 3.3 Escanear imágenes antes de desplegar en producción
from wcontainer import Wcontainer
wc = Wcontainer(verbose=True)
# Run a security check before deployment
scan_result = wc.scan_image_with_trivy("my_application_image")
# If vulnerabilities are found, abort deployment
if "error" not in scan_result and any(scan_result.get("Vulnerabilities", [])):
print("Deployment aborted: vulnerabilities detected.")
else:
print("No critical vulnerabilities found, proceeding with deployment...")
print(
wc.start_container(
"my_application_image", "production_app", ports={"5000/tcp": 5000}
)
)
# 🔹 Explicación: Antes de desplegar una imagen, el sistema revisa vulnerabilidades con Trivy. Si se encuentran fallos críticos, se cancela el despliegue.
- 4_autonamizar_generacion_informe_errores.py: Example demonstrating functionality.
# ✅ 3.4 Automatizar la generación de informes de errores
from wcontainer import Wcontainer
wc = Wcontainer(verbose=True)
# Check logs of multiple containers and generate error reports
containers = ["my_nginx", "backend_service", "database"]
for container in containers:
print(wc.generate_error_report(container, output_file=f"{container}_errors.json"))
# 🔹 Explicación: Revisa los logs de varios contenedores y genera informes de errores individuales en formato JSON.
- 5_usar_ia_detectar_fallos_para_reinicio_automatico.py: Example demonstrating functionality.
# ✅ 3.5 Detectar fallos en contenedores con IA y reiniciarlos automáticamente
from wcontainer import Wcontainer
wc = Wcontainer(verbose=True)
containers = ["my_nginx", "backend_service"]
for container in containers:
failure_prediction = wc.predict_container_failure(container)
print(failure_prediction)
if "ALERT" in failure_prediction:
print(f"Restarting {container} due to high failure probability...")
print(wc.start_container("nginx", f"{container}_restarted"))
# 🔹 Explicación: Usa la predicción de fallos con IA y reinicia automáticamente los contenedores con alta probabilidad de error.
4-monitoreo_RT
Description
This module demonstrates specific functionalities.
- monitoreo_tiempo_real.py: Example demonstrating functionality.
# ✅ 4️. Ejemplo Completo: Monitoreo en Tiempo Real
from wcontainer import Wcontainer
import time
wc = Wcontainer(verbose=True)
while True:
print("\n📊 Checking system resources...")
print(wc.get_resource_metrics())
print("\n🔍 Scanning image for vulnerabilities...")
print(wc.scan_image_with_trivy("nginx"))
print("\n📂 Checking error logs for running containers...")
containers = ["my_nginx", "backend_service"]
for container in containers:
print(wc.generate_error_report(container))
print("\n⚡ Adjusting container resources...")
print(wc.adjust_container_resources("my_nginx", cpu_limit=2.0, memory_limit="1g"))
print("\n🛑 Predicting failures in containers...")
for container in containers:
failure_prediction = wc.predict_container_failure(container)
print(failure_prediction)
if "ALERT" in failure_prediction:
print(f"Restarting {container} due to high failure probability...")
print(wc.start_container("nginx", f"{container}_restarted"))
print("\n⏳ Waiting 60 seconds before next check...\n")
time.sleep(60)
# 🔹 Explicación:
# ✅ Recolecta métricas del sistema cada 60 segundos
# ✅ Escanea la imagen Nginx con Trivy
# ✅ Analiza los logs de errores de todos los contenedores
# ✅ Ajusta dinámicamente la asignación de recursos
# ✅ Detecta fallos con IA y reinicia contenedores automáticamente
License
MIT
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wcontainer-0.1.1.tar.gz.
File metadata
- Download URL: wcontainer-0.1.1.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
741054edbd4d0b6d19346e57601332f163b5d682a5c668efb49425b23df66f45
|
|
| MD5 |
e7ad056c8e0dedf4ff8609770e996fb0
|
|
| BLAKE2b-256 |
45aaa0b6b67fc4f265014d69002912598dcaa13b9edae27ff4c784745683c8f7
|
File details
Details for the file wcontainer-0.1.1-py3-none-any.whl.
File metadata
- Download URL: wcontainer-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
64bd34b580bb40a8a272b021670be3ec6cc6ddb8cc074d1c796621d485ba9329
|
|
| MD5 |
f1854c44a8b0848783be97cc602db92f
|
|
| BLAKE2b-256 |
448c9509bb3616b140054d67dd35620900dcf325540de5869d00df68fb7ca588
|