Skip to content

Commit

Permalink
Merge pull request #414 from KMJ-007/master
Browse files Browse the repository at this point in the history
fix: improve socket error handling
  • Loading branch information
fussybeaver authored Jul 3, 2024
2 parents 9435d91 + d49bf14 commit 0c38ac2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
24 changes: 20 additions & 4 deletions src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use std::fs;
use std::future::Future;
#[cfg(feature = "ssl")]
use std::io;
use std::path::Path;
#[cfg(feature = "ssl")]
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::pin::Pin;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
Expand Down Expand Up @@ -641,12 +642,22 @@ impl Docker {
timeout: u64,
client_version: &ClientVersion,
) -> Result<Docker, Error> {
// Remove the scheme if present
let clean_path = path
.trim_start_matches("unix://")
.trim_start_matches("npipe://");

// Check if the socket file exists
if !std::path::Path::new(clean_path).exists() {
return Err(Error::SocketNotFoundError(clean_path.to_string()));
}

#[cfg(unix)]
let docker = Docker::connect_with_unix(path, timeout, client_version);
let docker = Docker::connect_with_unix(path, timeout, client_version)?;
#[cfg(windows)]
let docker = Docker::connect_with_named_pipe(path, timeout, client_version);
let docker = Docker::connect_with_named_pipe(path, timeout, client_version)?;

docker
Ok(docker)
}

/// Connect using a Unix socket, a Windows named pipe, or via HTTP.
Expand Down Expand Up @@ -749,6 +760,11 @@ impl Docker {
) -> Result<Docker, Error> {
let client_addr = path.replacen("unix://", "", 1);

// check if the socket file exists and is accessible
if !Path::new(&client_addr).exists() {
return Err(Error::SocketNotFoundError(client_addr));
}

let unix_connector = UnixConnector;

let mut client_builder = Client::builder(TokioExecutor::new());
Expand Down
3 changes: 3 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,7 @@ pub enum Error {
/// The URI that was attempted to be connected to
uri: String,
},
/// Error emitted when the Docker socket file is not found at the expected location.
#[error("Socket not found: {0}")]
SocketNotFoundError(String),
}
2 changes: 1 addition & 1 deletion src/grpc/driver/docker_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl Service<tonic::transport::Uri> for DockerContainer {
///
/// ## Examples
///
/// ```rust
/// ```rust,no_run
/// use bollard::grpc::driver::docker_container::DockerContainerBuilder;
/// use bollard::Docker;
///
Expand Down

0 comments on commit 0c38ac2

Please sign in to comment.