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.
- 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
- Python 3.8 or higher
- Docker installed and running
- pip package manager
git clone <repository-url>
cd docker-cicd-manager
pip install -r requirements.txt
pip install -e .
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()
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()
DockerManager(base_url=None)
base_url
(str, optional): Docker daemon URL. If None, uses default socket.
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 logs from a container.
Parameters:
container_id
(str): Container ID or name
Returns: Container logs as string
Raises:
ContainerError
: If container not found
Stop a running container.
Parameters:
container_id
(str): Container ID or name
Returns: True if successful
Raises:
ContainerError
: If container not found
List Docker containers.
Parameters:
all_containers
(bool): If True, include stopped containers
Returns: List of container objects
Pull a Docker image.
Parameters:
image
(str): Image nametag
(str): Image tag
Returns: Image object
Raises:
ImageError
: If image pull fails
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
Clean up all test containers.
Returns: Number of containers cleaned up
Get Docker daemon information.
Returns: Dictionary with Docker daemon info
Close the Docker client connection.
# 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
- Unit Tests: Test individual methods and functions
- Integration Tests: Test Docker integration
- Error Handling Tests: Test error scenarios
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
.github/workflows/ci.yml
: Main CI pipeline.github/workflows/release.yml
: Release automation
See examples/basic_usage.py
for simple usage examples.
See examples/advanced_usage.py
for complex scenarios.
The library provides comprehensive error handling:
DockerManagerError
: Base exceptionContainerError
: Container-related errorsImageError
: Image-related errors
- Follow PEP 8
- Use Black for formatting
- Use Flake8 for linting
- Use MyPy for type checking
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
MIT License
For issues and questions:
- Create an issue on GitHub
- Check the documentation
- Review the examples