From 974fd3f21397e25e0a4227ef9d16cf2a93559ce1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarl=20Andr=C3=A9=20H=C3=BCbenthal?= Date: Sun, 17 Nov 2024 21:21:23 +0100 Subject: [PATCH] fix: use XDG_RUNTIME_DIR dynamically in default socket paths --- lib/connection/connection.ex | 3 +- .../docker_socket_path.ex | 34 +++++++++++++------ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/lib/connection/connection.ex b/lib/connection/connection.ex index ee2b3bf..328495e 100644 --- a/lib/connection/connection.ex +++ b/lib/connection/connection.ex @@ -14,7 +14,8 @@ defmodule Testcontainers.Connection do @timeout 300_000 def get_connection(options \\ []) do - {docker_host_url, docker_host} = get_docker_host_url() + {docker_host_url, docker_host} = + get_docker_host_url() |> IO.inspect(label: "Testcontainers using") Logger.log("Using docker host url: #{docker_host_url}") diff --git a/lib/connection/docker_host_strategy/docker_socket_path.ex b/lib/connection/docker_host_strategy/docker_socket_path.ex index d000986..f0fcc24 100644 --- a/lib/connection/docker_host_strategy/docker_socket_path.ex +++ b/lib/connection/docker_host_strategy/docker_socket_path.ex @@ -2,23 +2,37 @@ defmodule Testcontainers.DockerSocketPathStrategy do @moduledoc false - @docker_socket_paths [ - Path.expand("~/.docker/run/docker.sock"), - Path.expand("~/.docker/desktop/docker.sock"), - "/run/user/#{:os.getpid()}/podman/podman.sock", - "/run/user/#{:os.getpid()}/docker.sock", - "/var/run/docker.sock" - ] - - defstruct socket_paths: @docker_socket_paths + defstruct socket_paths: [] defimpl Testcontainers.DockerHostStrategy do alias Testcontainers.DockerUrl alias Testcontainers.Logger + defp default_socket_paths do + [ + Path.expand("~/.docker/run/docker.sock"), + Path.expand("~/.docker/desktop/docker.sock"), + "/var/run/docker.sock" + ] ++ + case System.get_env("XDG_RUNTIME_DIR") do + nil -> + [] + + path -> + [ + "#{path}/podman/podman.sock", + "#{path}/docker.sock" + ] + end + end + def execute(strategy, _input) do Enum.reduce_while( - strategy.socket_paths, + if length(strategy.socket_paths) == 0 do + default_socket_paths() + else + strategy.socket_paths + end, {:error, {:docker_socket_not_found, []}}, fn path, {:error, {:docker_socket_not_found, tried_paths}} -> if path != nil && File.exists?(path) do