From f1ac45556ea3c3efb94aa36c6946ea1bfadef947 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Thu, 22 Feb 2024 12:09:30 -0500 Subject: [PATCH] Try to connect to DOCKER_HOST and if that fails try toconnect to docker desktop default socket --- Cargo.lock | 1 + cmd/soroban-cli/Cargo.toml | 1 + .../src/commands/network/shared.rs | 31 +++++++++++++------ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a1ea3b32d..7599861bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3602,6 +3602,7 @@ dependencies = [ "gix", "heck 0.4.1", "hex", + "home", "http 0.2.11", "hyper", "hyper-tls", diff --git a/cmd/soroban-cli/Cargo.toml b/cmd/soroban-cli/Cargo.toml index 087d8b473..0d67b0a0c 100644 --- a/cmd/soroban-cli/Cargo.toml +++ b/cmd/soroban-cli/Cargo.toml @@ -108,6 +108,7 @@ toml_edit = "0.21.0" rust-embed = { version = "8.2.0", features = ["debug-embed"] } bollard = "0.15.0" futures-util = "0.3.30" +home = "0.5.9" # For hyper-tls [target.'cfg(unix)'.dependencies] openssl = { version = "=0.10.55", features = ["vendored"] } diff --git a/cmd/soroban-cli/src/commands/network/shared.rs b/cmd/soroban-cli/src/commands/network/shared.rs index 070588096..7260b2cbf 100644 --- a/cmd/soroban-cli/src/commands/network/shared.rs +++ b/cmd/soroban-cli/src/commands/network/shared.rs @@ -2,6 +2,7 @@ use core::fmt; use bollard::{ClientVersion, Docker}; use clap::ValueEnum; +use home::home_dir; pub const DOCKER_HOST_HELP: &str = "Optional argument to override the default docker host. This is useful when you are using a non-standard docker host path for your Docker-compatible container runtime, e.g. Docker Desktop defaults to $HOME/.docker/run/docker.sock instead of /var/run/docker.sock"; @@ -42,16 +43,12 @@ pub async fn connect_to_docker( .clone() .unwrap_or(DEFAULT_DOCKER_HOST.to_string()); - let connection = match host { - // if tcp or http use connect_with_http_defaults - // if windows and host starts with "npipe://" use connect_with_named_pipe + let connection = match host.clone() { + // if tcp or http, use connect_with_http_defaults + // if windows and host starts with "npipe://", use connect_with_named_pipe // if unix and host starts with "unix://" use connect_with_unix // else default to connect_with_unix h if h.starts_with("tcp://") || h.starts_with("http://") => { - #[cfg(feature = "ssl")] - if env::var("DOCKER_TLS_VERIFY").is_ok() { - return Docker::connect_with_ssl_defaults(); - } Docker::connect_with_http_defaults() } #[cfg(unix)] @@ -68,8 +65,24 @@ pub async fn connect_to_docker( } }?; - check_docker_connection(&connection).await?; - Ok(connection) + match check_docker_connection(&connection).await { + Ok(_) => return Ok(connection), + // If we aren't able to connect with the defaults, or with the provided docker_host, we try with the default docker desktop socket since that is a common use case for developers + Err(_) => { + let default_docker_desktop_host = + format!("{}/.docker/run/docker.sock", home_dir().unwrap().display()); + println!("Failed to connect to DOCKER_HOST: {host}.\nTrying to connect to the default Docker Desktop socket at {default_docker_desktop_host}."); + let connection = Docker::connect_with_unix( + &default_docker_desktop_host, + DEFAULT_TIMEOUT, + API_DEFAULT_VERSION, + )?; + match check_docker_connection(&connection).await { + Ok(_) => return Ok(connection), + Err(err) => return Err(err), + } + } + } } // When bollard is not able to connect to the docker daemon, it returns a generic ConnectionRefused error