Skip to content

Commit

Permalink
fix: use XDG_RUNTIME_DIR dynamically in default socket paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jarlah committed Nov 17, 2024
1 parent 64f3b7c commit 974fd3f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/connection/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down
34 changes: 24 additions & 10 deletions lib/connection/docker_host_strategy/docker_socket_path.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 974fd3f

Please sign in to comment.