lmrc-docker
Part of the LMRC Stack - Infrastructure-as-Code toolkit for building production-ready Rust applications
An ergonomic, comprehensive Docker client library for Rust with fluent builder APIs for managing containers, images, networks, volumes, and registries.
Features
- Fluent Builder APIs: Ergonomic, chainable methods for creating and configuring Docker resources
- Full Docker API Coverage: Complete support for containers, images, networks, and volumes
- Registry Management: Search, authenticate, and manage images across multiple registries (Docker Hub, GitHub Container Registry, custom registries)
- Async/Await: Built on Tokio for modern async Rust
- Type-Safe: Leverages Rust's type system for compile-time safety
- Comprehensive Error Handling: Detailed error types using
thiserror - Well Documented: Extensive documentation and examples
Installation
Add this to your Cargo.toml:
[]
= "0.1"
= { = "1.0", = ["full"] }
Quick Start
use ;
async
Examples
Container Management
use DockerClient;
let client = new?;
// Create a container with advanced configuration
let container = client.containers
.create
.name
.port
.env
.restart_always
.memory_limit // 512MB
.build
.await?;
// Start the container
container.start.await?;
// View logs
let logs = container.logs.await?;
println!;
// Execute a command
let exec_id = container.exec.await?;
// Stop and remove
container.stop.await?;
container.remove.await?;
Image Operations
use DockerClient;
let client = new?;
// Pull an image
client.images.pull.await?;
// Build an image from a Dockerfile
let image_id = client.images
.build
.tag
.dockerfile
.cache_from
.execute
.await?;
// Push to registry (requires authentication)
client.images.push.await?;
// List images
let images = client.images.list.await?;
for image in images
// Remove an image
client.images.remove.await?;
Registry Management
use ;
let client = new?;
let registry = client.registry;
// Search for images
let results = registry.search.await?;
for result in results
// Authenticate with Docker Hub
let config = docker_hub;
registry.login.await?;
// Authenticate with GitHub Container Registry
let config = github;
registry.login.await?;
// Check if an image exists locally
let exists = registry.image_exists_locally.await?;
// Tag an image
registry.tag_image.await?;
// Export/Import images
let tar_data = registry.export_image.await?;
let image_id = registry.import_image.await?;
// Prune unused images
let reclaimed = registry.prune_images.await?;
println!;
Network Management
use DockerClient;
let client = new?;
// Create a network
let network = client.networks
.create
.driver
.await?;
// Connect a container to a network
network.connect.await?;
// Disconnect a container
network.disconnect.await?;
// List networks
let networks = client.networks.list.await?;
// Remove network
network.remove.await?;
Volume Management
use DockerClient;
let client = new?;
// Create a volume
let volume = client.volumes
.create
.driver
.await?;
// List volumes
let volumes = client.volumes.list.await?;
// Remove volume
volume.remove.await?;
// Prune unused volumes
let pruned = client.volumes.prune.await?;
Configuration
Custom Docker Connection
use ;
// Connect via Unix socket
let client = connect_with_unix?;
// Connect via TCP
let client = connect_with_tcp?;
// Custom configuration
let config = new
.timeout
.api_version;
let client = with_config?;
Environment Variables
The client respects the DOCKER_HOST environment variable:
Error Handling
The library uses a comprehensive error type system:
use ;
match client.containers.get
Architecture
The library is organized into modular components:
client: Docker client configuration and connection managementcontainers: Container lifecycle operations withContainerBuilderimages: Image operations withImageBuilderfor building imagesnetworks: Network creation and managementvolumes: Volume operationsregistry: Registry authentication, search, and image managementerror: Comprehensive error types
Advanced Usage
Direct Bollard Access
For operations not covered by the high-level API:
let client = new?;
let bollard = client.inner;
// Use bollard directly
let info = bollard.info.await?;
Streaming Logs
use StreamExt;
let container = client.containers.get.await?;
let mut logs_stream = container.logs_stream.await?;
while let Some = logs_stream.next.await
Requirements
- Rust 1.70 or later
- Docker daemon running and accessible
Examples
See the examples directory for complete working examples:
basic_container.rs- Basic container lifecyclebuild_image.rs- Building Docker imageslist_resources.rs- Listing containers, images, networks, volumesregistry_operations.rs- Registry management operations
Run examples with:
Testing
The library includes integration tests that require a running Docker daemon:
# Run tests
# Run specific test
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
Part of the LMRC Stack project. Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Acknowledgments
Built on top of Bollard, the excellent Docker daemon API for Rust.
Related Projects
- bollard - Lower-level Docker API
- shiplift - Alternative Docker API client
- dockworker - Docker daemon API wrapper