Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose configured bollard client #740

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion testcontainers/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use self::{
mod image;

pub(crate) mod async_drop;
pub(crate) mod client;
pub mod client;
pub(crate) mod containers;
pub(crate) mod copy;
pub(crate) mod env;
Expand Down
2 changes: 2 additions & 0 deletions testcontainers/src/core/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ mod bollard_client;
mod exec;
mod factory;

pub use factory::docker_client_instance;

static IN_A_CONTAINER: OnceCell<bool> = OnceCell::const_new();

// See https://github.com/docker/docker/blob/a9fa38b1edf30b23cae3eade0be48b3d4b1de14b/daemon/initlayer/setup_unix.go#L25
Expand Down
12 changes: 11 additions & 1 deletion testcontainers/src/core/client/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ static DOCKER_CLIENT: OnceLock<Mutex<Weak<Client>>> = OnceLock::new();

impl Client {
/// Returns a client instance, reusing already created or initializing a new one.
// We don't expose this function to the public API for now. We can do it later if needed.
pub(crate) async fn lazy_client() -> Result<Arc<Client>, ClientError> {
let mut guard = DOCKER_CLIENT
.get_or_init(|| Mutex::new(Weak::new()))
Expand All @@ -29,3 +28,14 @@ impl Client {
}
}
}

/// Returns a configured Docker client instance.
///
/// This function provides access to the underlying Docker client ([`bollard`]).
/// While this method is publicly exposed, it is not intended for frequent use.
/// It can be useful in scenarios where you need to interact with the Docker API directly using an already configured client.
///
/// This method returns a lazily-created client, reusing an existing one if available.
pub async fn docker_client_instance() -> Result<bollard::Docker, ClientError> {
Client::lazy_client().await.map(|c| c.bollard.clone())
}
Loading