A Python library for Docker container management and GitHub Actions CI/CD integration
Project description
Docker CI/CD Manager Documentation
Overview
Docker CI/CD Manager is a Python library that provides a simple and powerful interface for managing Docker containers, building images, and integrating with GitHub Actions for automated CI/CD pipelines.
Features
- Container Management: Create, run, stop, and manage Docker containers
- Image Operations: Pull, build, and manage Docker images
- Test Automation: Create test containers for automated testing
- CI/CD Integration: GitHub Actions workflows for automated testing and deployment
- Error Handling: Comprehensive error handling and logging
- Cleanup Utilities: Automatic cleanup of test containers
Installation
Prerequisites
- Python 3.8 or higher
- Docker installed and running
- pip package manager
Install from source
git clone <repository-url>
cd docker-cicd-manager
pip install -r requirements.txt
pip install -e .
Quick Start
Basic Usage
from docker_cicd_manager import DockerManager
# Initialize Docker Manager
docker_manager = DockerManager()
# Create a test container
container = docker_manager.create_test_container(
image="ubuntu:latest",
command="echo 'Hello from Docker!'",
name="my-test-container"
)
# Get container logs
logs = docker_manager.get_container_logs(container.id)
print(logs)
# Stop the container
docker_manager.stop_container(container.id)
# Cleanup
docker_manager.close()
Advanced Usage
from docker_cicd_manager import DockerManager
docker_manager = DockerManager()
# Create multiple test containers
containers = []
for i in range(3):
container = docker_manager.create_test_container(
image="python:3.11-slim",
command=f"python -c 'print(\"Container {i+1} is working!\")'",
name=f"test-container-{i+1}"
)
containers.append(container)
# Wait for completion and check logs
import time
time.sleep(3)
for container in containers:
logs = docker_manager.get_container_logs(container.id)
print(f"Container {container.id}: {logs}")
# Cleanup all test containers
cleaned_count = docker_manager.cleanup_test_containers()
print(f"Cleaned up {cleaned_count} containers")
docker_manager.close()
API Reference
DockerManager Class
Constructor
DockerManager(base_url=None)
base_url(str, optional): Docker daemon URL. If None, uses default socket.
Methods
create_test_container(image, command, name=None, **kwargs)
Create a test container with the specified image.
Parameters:
image(str): Docker image to usecommand(str): Command to run in the containername(str, optional): Container name**kwargs: Additional container configuration options
Returns: Container object
Raises:
ImageError: If image not foundContainerError: If container creation fails
get_container_logs(container_id)
Get logs from a container.
Parameters:
container_id(str): Container ID or name
Returns: Container logs as string
Raises:
ContainerError: If container not found
stop_container(container_id)
Stop a running container.
Parameters:
container_id(str): Container ID or name
Returns: True if successful
Raises:
ContainerError: If container not found
list_containers(all_containers=False)
List Docker containers.
Parameters:
all_containers(bool): If True, include stopped containers
Returns: List of container objects
pull_image(image, tag="latest")
Pull a Docker image.
Parameters:
image(str): Image nametag(str): Image tag
Returns: Image object
Raises:
ImageError: If image pull fails
build_image(path, tag, dockerfile="Dockerfile")
Build a Docker image from a Dockerfile.
Parameters:
path(str): Path to build contexttag(str): Image tagdockerfile(str): Dockerfile name
Returns: Image object
Raises:
ImageError: If image build fails
cleanup_test_containers()
Clean up all test containers.
Returns: Number of containers cleaned up
get_docker_info()
Get Docker daemon information.
Returns: Dictionary with Docker daemon info
close()
Close the Docker client connection.
Testing
Running Tests
# Run all tests
python -m pytest tests/ -v
# Run specific test
python -m pytest tests/test_docker_manager.py::TestDockerManager::test_create_simple_test_container -v
# Run with coverage
python -m pytest tests/ -v --cov=docker_cicd_manager --cov-report=html
# Use the test runner script
python run_tests.py
Test Categories
- Unit Tests: Test individual methods and functions
- Integration Tests: Test Docker integration
- Error Handling Tests: Test error scenarios
CI/CD Integration
GitHub Actions
The library includes GitHub Actions workflows for:
- Continuous Integration: Automated testing on push/PR
- Code Quality: Linting, formatting, type checking
- Security Scanning: Bandit and Safety checks
- Docker Testing: Integration tests with Docker
- Release Management: Automated releases
Workflow Files
.github/workflows/ci.yml: Main CI pipeline.github/workflows/release.yml: Release automation
Examples
Basic Examples
See examples/basic_usage.py for simple usage examples.
Advanced Examples
See examples/advanced_usage.py for complex scenarios.
Error Handling
The library provides comprehensive error handling:
DockerManagerError: Base exceptionContainerError: Container-related errorsImageError: Image-related errors
Code Style
- Follow PEP 8
- Use Black for formatting
- Use Flake8 for linting
- Use MyPy for type checking
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
License
MIT License
Support
For issues and questions:
- Create an issue on GitHub
- Check the documentation
- Review the examples
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 dockepy-0.1.0.tar.gz.
File metadata
- Download URL: dockepy-0.1.0.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d93a485c866b32278ddfea15e91f1c43d618633f417d1e7591e7175ea5594b1
|
|
| MD5 |
29976fc9e032c64ba10555f9ea50f8d9
|
|
| BLAKE2b-256 |
8dde0d9d9f33babea53bf2f2295ff45fed267a78651e77746edf1eb10673d445
|
File details
Details for the file dockepy-0.1.0-py3-none-any.whl.
File metadata
- Download URL: dockepy-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
27fd6cfc72a394069fb842dd98cdc60efb9217e2c856e92d9477d630925619ec
|
|
| MD5 |
4e75d980990a3f0cc8abf828e691f111
|
|
| BLAKE2b-256 |
7f6355f1fbb5d1595f2d46592de45d644d831b7d0addbedab51e116425ba39ce
|