Hey there, Rustaceans! 🦀 Let’s talk containers.
In this article, we’re diving into three crates that are making waves in the Rust container ecosystem: shiplift, bollard, and dockworker. Whether you're a seasoned pro or just dipping your toes into the world of containers, these crates are like having a Swiss Army knife in your toolbox. Let's explore what makes them so cool, shall we? 🌊🚀
1. Shiplift: A Versatile Interface to Docker
Shiplift is a Rust crate designed to interact with the Docker daemon, providing a high-level API to manage Docker containers and images. It abstracts the complexities of Docker’s API, offering Rust developers a more accessible and idiomatic way to control their containerized environments.
Key Components
Shared Traits and Structures:
- Docker: The core structure in Shiplift. It serves as the entry point to interact with the Docker daemon.
- Image and Container Builders: These builders provide a fluent interface to create and manage Docker images and containers.
Code Example: Creating a Docker instance
use shiplift::Docker;
fn main() {
let docker = Docker::new();
// Now you can use `docker` to interact with the Docker daemon
}
Container Management:
- Starting and Stopping Containers: Shiplift allows you to start, stop, and manage the lifecycle of containers.
- Executing Commands: Run commands inside running containers.
Code Example: Starting a Container
use shiplift::{Docker, ContainerOptions};
#[tokio::main]
async fn main() {
let docker = Docker::new();
let container = docker.containers()
.create(&ContainerOptions::builder("hello-world").build())
.await.unwrap();
container.start().await.unwrap();
// The container starts executing here
}
Image Management:
- Pulling and Pushing Images: Handle Docker images, allowing you to pull from or push to registries.
- Building Images: Build Docker images directly from your Rust code.
Code Example: Pulling an Image
use shiplift::Docker;
#[tokio::main]
async fn main() {
let docker = Docker::new();
let images = docker.images();
images.pull(&Default::default()).await.unwrap();
// Pulls the specified image
}
Log Streaming:
- Stream Logs: Fetch and stream logs from a running container, crucial for debugging and monitoring.
Code Example: Streaming Container Logs
use shiplift::{Docker, LogsOptions};
#[tokio::main]
async fn main() {
let docker = Docker::new();
let logs = docker.containers()
.get("container_id")
.logs(&LogsOptions::builder().stdout(true).build())
.await.unwrap();
for line in logs {
println!("{}", line.unwrap());
}
}
Advanced Features
Network and Volume Management:
- Manage Docker networks and volumes, essential for complex container setups.
Interaction with Docker Compose:
- Although Shiplift doesn’t directly interface with Docker Compose, it can be used alongside it for more advanced container orchestration.
2. Navigating Container Operations in Rust with Bollard
Bollard leverages Rust’s asynchronous features to interact with the Docker API, offering a modern, efficient way to handle container operations. It’s designed for Rust applications that need to create, manage, or orchestrate Docker containers and images.
2. Key Components of Bollard
Docker API Client:
- The primary entry point for interacting with the Docker daemon.
- Facilitates various operations like container management, image operations, and more.
Code Example: Creating a Docker API Client
use bollard::Docker;
#[tokio::main]
async fn main() {
let docker = Docker::connect_with_unix_defaults().unwrap();
// Now you can use `docker` to interact with the Docker API
}
Container Management:
- Comprehensive functionalities for creating, starting, stopping, and inspecting containers.
- Includes advanced features like pausing, unpausing, and removing containers.


