Skip to content

Commit

Permalink
Fix the socket not found error on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
mominul committed Aug 16, 2024
1 parent 3f30ac8 commit da02894
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion testcontainers/src/core/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl Client {
}

pub(crate) async fn docker_hostname(&self) -> Result<url::Host, ClientError> {
let docker_host = self.config.docker_host();
let docker_host = &self.config.docker_host();
let docker_host_url = Url::from_str(docker_host)
.map_err(|e| ConfigurationError::InvalidDockerHost(e.to_string()))?;

Expand Down
4 changes: 2 additions & 2 deletions testcontainers/src/core/client/bollard_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::core::env;
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(2 * 60);

pub(super) fn init(config: &env::Config) -> Result<Docker, bollard::errors::Error> {
let host = config.docker_host();
let host = &config.docker_host();
let host_url = Url::from_str(host)?;

match host_url.scheme() {
Expand Down Expand Up @@ -36,7 +36,7 @@ fn connect_with_ssl(config: &env::Config) -> Result<Docker, bollard::errors::Err
let cert_path = config.cert_path().expect("cert path not found");

Docker::connect_with_ssl(
config.docker_host(),
&config.docker_host(),
&cert_path.join("key.pem"),
&cert_path.join("cert.pem"),
&cert_path.join("ca.pem"),
Expand Down
16 changes: 14 additions & 2 deletions testcontainers/src/core/env/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{
borrow::Cow,
env::var,
path::{Path, PathBuf},
str::FromStr,
};
Expand Down Expand Up @@ -126,11 +128,21 @@ impl Config {
/// 2. `DOCKER_HOST` environment variable.
/// 3. Docker host from the `docker.host` property in the `~/.testcontainers.properties` file.
/// 4. Else, the default Docker socket will be returned.
pub(crate) fn docker_host(&self) -> &str {
pub(crate) fn docker_host(&self) -> Cow<'_, str> {
self.tc_host
.as_deref()
.or(self.host.as_deref())
.unwrap_or(DEFAULT_DOCKER_HOST)
.map(Cow::Borrowed)
.unwrap_or_else(|| {
// Docker Desktop on macOS creates the socket in the user's home directory
// from version 4.13.0.
// https://github.com/docker/for-mac/issues/6529
if cfg!(target_os = "macos") {
format!("unix://{}/.docker/run/docker.sock", var("HOME").unwrap()).into()
} else {
DEFAULT_DOCKER_HOST.into()
}
})
}

pub(crate) fn tls_verify(&self) -> bool {
Expand Down

0 comments on commit da02894

Please sign in to comment.