Introduction to FluentDocker
FluentDocker is a sophisticated library designed to simplify interactions with Docker and Docker-Compose through a Fluent API. The library is compatible with multiple operating systems, including Linux, Windows, and Mac. Additionally, it offers support for legacy Docker-machine interactions, making it a versatile tool for developers working across different environments.
Key Features of FluentDocker
Fluent API for Docker Operations
FluentDocker provides a seamless way to manage Docker containers and services using a fluent syntax. A fluent API allows for writing code that is more readable and easier to understand, resembling natural language sentences. Developers can utilize this library to perform a variety of tasks, from building and starting Docker containers to waiting for specific processes or ports to become available.
Sample Code Snippet
The following is a simple example of how to use FluentDocker to start a PostgreSQL container:
using (
var container = new Builder().UseContainer()
.UseImage("kiasaki/alpine-postgres")
.ExposePort(5432)
.WithEnvironment("POSTGRES_PASSWORD=mysecretpassword")
.WaitForPort("5432/tcp", 30000 /*30s*/)
.Build()
.Start())
{
var config = container.GetConfiguration(true);
Assert.AreEqual(ServiceRunningState.Running, config.State.ToServiceState());
}
In this example, the library is used to build and start a PostgreSQL container, expose a port, and set environment variables. The container is set to wait until the specified port is ready before proceeding.
Docker-Compose Support
FluentDocker extends its capabilities to Docker-Compose, enabling users to manage complex environments with multiple containers. It's an efficient choice for those needing to orchestrate various services together.
var file = Path.Combine(Directory.GetCurrentDirectory(),
(TemplateString) "Resources/ComposeTests/WordPress/docker-compose.yml");
using (var svc = new Builder()
.UseContainer()
.UseCompose()
.FromFile(file)
.RemoveOrphans()
.WaitForHttp("wordpress", "http://localhost:8000/wp-admin/install.php")
.Build().Start())
{
var installPage = await "http://localhost:8000/wp-admin/install.php".Wget();
Assert.IsTrue(installPage.IndexOf("https://wordpress.org/", StringComparison.Ordinal) != -1);
}
In the above code, FluentDocker is utilized to manage a Docker-Compose file for a WordPress setup, ensuring the application and its dependencies are properly started and waiting for the WordPress installation page to be accessible.
Supported Platforms and Languages
The library is supported by .NET Framework 4.51 and later, as well as .NET Standard 1.6 and 2.0, making it accessible for a large number of .NET developers. The architecture of FluentDocker is structured into three layers:
-
Docker Binaries Interactions: This layer consists of static commands and interactions with the Docker environment.
-
Services Management: A thin layer for managing Docker machines, containers, etc.
-
Fluent API: The main API through which developers define and manage Docker services.
Contribution and Community Involvement
FluentDocker is open for contributions, encouraging the community to participate in its development. Contributors are urged to adhere to its .editorconfig
file to maintain code consistency, with promises to provide more comprehensive guidelines in the future.
Basic Commands and Their Usage
FluentDocker allows for a straightforward use of Docker commands, requiring a DockerUri
to identify the Docker daemon. This URI can be configured manually or discovered automatically for local instances.
Commands such as starting, stopping, and removing containers are made easy through FluentDocker, and users can interact with both Linux and Windows Docker daemons, switching between them as necessary.
Advanced Features
Logs and Data Streaming
The library supports streaming data for real-time logs and events, using a flexible approach that can handle cancellation tokens and be operated as background tasks.
Machine, Image, and Container Management
The highest level of the API allows users to design and execute complex setups involving load balancers, multiple nodes, and database servers, all within a few lines of code.
FluentDocker also provides utilities for managing ports, exposing them explicitly or letting Docker allocate them dynamically. This flexibility ensures that developers can cater to a wide range of scenarios, particularly in networked environments.
Filesystem Manipulation
Integrated features for file and directory management allow containers to interact with host filesystems seamlessly. Users can mount host directories within containers or copy files to and from containers as needed.
Process and Port Monitoring
FluentDocker adds robustness by monitoring specific processes or network ports, ensuring services are fully operational before initiating dependent actions.
In summary, FluentDocker is a powerful API that significantly simplifies Docker and Docker-Compose operations for .NET developers. Its fluent, natural syntax reduces the complexity of managing containers and services, facilitating streamlined DevOps workflows across diverse computing environments.