Crates.io | docker-wrapper |
lib.rs | docker-wrapper |
version | 0.8.2 |
created_at | 2025-07-27 20:36:32.80896+00 |
updated_at | 2025-08-27 18:26:02.078965+00 |
description | A Docker CLI wrapper for Rust |
homepage | https://github.com/joshrotenberg/docker-wrapper |
repository | https://github.com/joshrotenberg/docker-wrapper |
max_upload_size | |
id | 1770406 |
size | 1,893,495 |
A comprehensive, type-safe Docker CLI wrapper for Rust applications.
Add to your Cargo.toml
:
[dependencies]
docker-wrapper = "0.5"
tokio = { version = "1", features = ["full"] }
Minimum Supported Rust Version (MSRV): 1.89.0
Enable Docker Compose support:
[dependencies]
docker-wrapper = { version = "0.5", features = ["compose"] }
use docker_wrapper::{DockerCommand, RunCommand};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Run a container
let output = RunCommand::new("nginx:latest")
.name("my-web-server")
.port(8080, 80)
.detach()
.execute()
.await?;
println!("Container started: {}", output.0);
Ok(())
}
Important: The
DockerCommand
trait must be imported to use the.execute()
method on any Docker command. This trait provides the core execution functionality for all commands.
use docker_wrapper::{DockerCommand, BuilderPruneCommand};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Clean up build cache
let result = BuilderPruneCommand::new()
.all()
.keep_storage("5GB")
.force()
.execute()
.await?;
println!("Reclaimed {} bytes of disk space",
result.space_reclaimed.unwrap_or(0));
Ok(())
}
Templates provide pre-configured containers with sensible defaults and best practices:
[dependencies]
docker-wrapper = { version = "0.8", features = ["templates"] }
Quick example:
use docker_wrapper::{RedisTemplate, PostgresTemplate, Template};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Start Redis with persistence and custom image
let redis = RedisTemplate::new("my-redis")
.port(6379)
.password("secret")
.with_persistence("redis-data")
.custom_image("redis", "7-alpine")
.platform("linux/amd64");
let redis_id = redis.start().await?;
// Start PostgreSQL with custom configuration
let postgres = PostgresTemplate::new("my-postgres")
.database("myapp")
.username("appuser")
.password("apppass")
.with_persistence("postgres-data");
let postgres_conn = postgres.start().await?;
println!("PostgreSQL URL: {}", postgres_conn.connection_url());
Ok(())
}
📖 Complete Template Documentation - Comprehensive guide with examples for all templates
use docker_wrapper::{DockerCommand, ContextCreateCommand, ContextUseCommand};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a context for remote Docker host
ContextCreateCommand::new("remote-docker")
.docker_host("ssh://user@remote-host")
.description("Remote development server")
.execute()
.await?;
// Switch to remote context
ContextUseCommand::new("remote-docker")
.execute()
.await?;
// Now all Docker commands will run on the remote host
Ok(())
}
Available templates:
RedisTemplate
, RedisSentinelTemplate
, RedisClusterTemplate
, RedisEnterpriseTemplate
, RedisInsightTemplate
PostgresTemplate
, MysqlTemplate
, MongodbTemplate
NginxTemplate
All templates support custom images, platforms, persistence, and resource limits. See the Template Documentation for complete usage examples.
docker-wrapper is ideal for:
Choosing between Docker Rust libraries? See our comprehensive Comparison Guide comparing docker-wrapper vs bollard vs testcontainers-rs.
For comprehensive documentation, examples, and API reference:
The examples/
directory contains practical examples:
basic_usage.rs
- Common Docker operationsbasic_docker_patterns.rs
- Essential Docker patterns and best practicesexec_examples.rs
- Container command executionlifecycle_commands.rs
- Container lifecycle managementrun_examples.rs
- Advanced run command usagestreaming.rs
- Real-time output streamingdocker_compose.rs
- Docker Compose usage (requires compose
feature)debugging_features.rs
- Debugging and inspection featuressystem_cleanup.rs
- System maintenance and cleanupcomplete_run_coverage.rs
- Comprehensive run command optionstemplate_usage.rs
- Container templates usage (requires templates
feature)network_volume_management.rs
- Network and volume managementredis_cluster.rs
- Redis cluster setup exampletest_sentinel.rs
- Redis Sentinel high availability exampleredis_enterprise_template.rs
- Redis Enterprise cluster with automatic initializationtesting_basics.rs
- Basic testing patterns with docker-wrappertest_fixtures.rs
- Reusable test fixtures for common servicesRun examples:
cargo run --example basic_usage
cargo run --example streaming
cargo run --features compose --example docker_compose
Contributions are welcome! Please see our Contributing Guide for details.
Licensed under the MIT license (LICENSE or http://opensource.org/licenses/MIT).