A high-performance Rust library for programmatic container orchestration, offering type-safe Docker and Docker Compose operations with advanced deployment capabilities.
Dockworker bridges the gap between static container configurations and dynamic runtime orchestration. It provides a robust, type-safe interface for programmatically managing containers, with first-class support for both Dockerfile and Docker Compose workflows.
- Compile-time Validation: Catch configuration errors before deployment with Rust's type system
- Safe API Abstractions: Memory-safe wrappers around Docker Engine API
- Error Recovery: Structured error handling with automatic cleanup and rollback capabilities
- Intelligent Build Context: Automatic dependency tracking and efficient layer caching
- Variable Substitution: Advanced environment variable interpolation with fallback support
- Resource Management: Fine-grained control over CPU, memory, and network resources
- Health Monitoring: Sophisticated health check system with customizable probes
- Declarative & Programmatic: Use YAML configs or build service definitions in code
- Dependency Resolution: Automatic service ordering based on dependency graphs
- Network Isolation: Automatic network creation and service discovery
- Volume Management: Type-safe volume mounting with automatic cleanup
- Async-first Design: Built on
tokio
for high-performance async I/O - Modular Architecture: Use only what you need with feature flags
- Comprehensive Logging: Detailed operation logging for debugging
- Testing Utilities: Rich testing infrastructure for container-based tests
Add to your Cargo.toml
:
[dependencies]
# For parsing only (no deployment features)
dockworker = { version = "0.1.0" }
# For full functionality including deployment (default)
dockworker = "0.1.0"
# Or explicitly with all features
dockworker = { version = "0.1.0", features = ["docker"] }
parser
- Enables Dockerfile and Docker Compose parsing functionality (minimal dependencies)deploy
- Enables deployment features using Bollard (includes parser features)
use dockworker::{DockerfileConfig, DockerfileParser, ComposeParser};
// Parse a Dockerfile
let dockerfile_content = std::fs::read_to_string("Dockerfile")?;
let config = DockerfileParser::parse(&dockerfile_content)?;
// Parse a Docker Compose file
let compose_content = std::fs::read_to_string("docker-compose.yml")?;
let compose_config = ComposeParser::parse(&compose_content)?;
When the deploy
feature is enabled:
use dockworker::DockerBuilder;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let builder = DockerBuilder::new()?;
// ... deployment functionality available
Ok(())
}
use dockworker::{DockerBuilder, DockerfileConfig, DockerCommand};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let builder = DockerBuilder::new()?;
// Parse existing Dockerfile
let config = builder.from_dockerfile("path/to/Dockerfile").await?;
// Or create programmatically
let config = DockerfileConfig {
base_image: "rust:1.70".to_string(),
commands: vec![
DockerCommand::Run {
command: "cargo build".to_string()
},
DockerCommand::Copy {
source: "./target".to_string(),
dest: "/app".to_string(),
chown: None,
},
],
};
// Deploy container
let container_id = builder.deploy_dockerfile(&config, "my-app:latest").await?;
Ok(())
}
use dockworker::{DockerBuilder, ComposeConfig};
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let builder = DockerBuilder::new()?;
// Parse compose file
let config = builder.from_compose("docker-compose.yml").await?;
// Deploy services
let container_ids: HashMap<String, String> = builder.deploy_compose(&config).await?;
Ok(())
}
use dockworker::{Service, ResourceLimits};
let service = Service {
image: Some("nginx:latest".to_string()),
requirements: Some(ResourceLimits {
cpu_limit: Some(0.5), // Half a CPU
memory_limit: Some("512M".to_string()), // 512MB memory limit
memory_swap: Some("1G".to_string()), // 1GB swap limit
memory_reservation: Some("256M".to_string()), // 256MB soft limit
cpus_shares: Some(512), // CPU shares (relative weight)
cpuset_cpus: Some("0,1".to_string()), // Run on CPUs 0 and 1
}),
..Default::default()
};
use dockworker::{Service, config::compose::HealthCheck};
let service = Service {
image: Some("nginx:latest".to_string()),
healthcheck: Some(HealthCheck {
test: vec![
"CMD-SHELL".to_string(),
"curl -f http://localhost/ || exit 1".to_string()
],
interval: Some(1_000_000_000), // 1 second
timeout: Some(3_000_000_000), // 3 seconds
retries: Some(3),
start_period: Some(2_000_000_000), // 2 seconds
start_interval: None,
}),
..Default::default()
};
use dockworker::DockerBuilder;
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let builder = DockerBuilder::new()?;
// Get container logs
let logs = builder.get_container_logs("container_id").await?;
// Execute command in container
let output = builder.exec_in_container(
"container_id",
vec!["ls", "-la"],
Some(HashMap::new()),
).await?;
// Create and manage volumes
builder.create_volume("my_volume").await?;
let volumes = builder.list_volumes().await?;
Ok(())
}
The library provides detailed error types for proper error handling:
use dockworker::DockerError;
match result {
Err(DockerError::FileError(e)) => println!("File error: {}", e),
Err(DockerError::YamlError(e)) => println!("YAML parsing error: {}", e),
Err(DockerError::DockerfileError(e)) => println!("Dockerfile parsing error: {}", e),
Err(DockerError::BollardError(e)) => println!("Docker API error: {}", e),
Err(DockerError::InvalidIpamConfig) => println!("Invalid network configuration"),
Err(DockerError::ContainerNotRunning(id)) => println!("Container {} not running", id),
Err(DockerError::NetworkCreationError(e)) => println!("Network creation failed: {}", e),
Err(DockerError::InvalidResourceLimit(e)) => println!("Invalid resource limit: {}", e),
Ok(_) => println!("Operation succeeded"),
}
This library is in active development. Current focus areas:
- Enhanced error handling with recovery strategies
- Comprehensive integration testing
- Additional Docker Compose features
- Improved resource management
- Better developer ergonomics
Contributions are welcome! Please feel free to submit a Pull Request. When contributing:
- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.