Current section

Files

Jump to
testcontainers lib connection docker_host_strategy docker_socket_path.ex
Raw

lib/connection/docker_host_strategy/docker_socket_path.ex

# SPDX-License-Identifier: MIT
defmodule Testcontainers.DockerSocketPathStrategy do
@moduledoc false
@rootless_docker_socket_paths [
System.get_env("XDG_RUNTIME_DIR"),
Path.expand("~/.docker/run/docker.sock"),
Path.expand("~/.docker/desktop/docker.sock"),
"/run/user/#{:os.getpid()}/docker.sock"
]
defstruct socket_paths: @rootless_docker_socket_paths
defimpl Testcontainers.DockerHostStrategy do
alias Testcontainers.DockerUrl
def execute(strategy, _input) do
Enum.reduce_while(
strategy.socket_paths,
{:error, docker_socket_path: :docker_socket_not_found},
fn path, _acc ->
if path != nil && File.exists?(path) do
path_with_scheme = "unix://" <> path
case DockerUrl.test_docker_host(path_with_scheme) do
:ok ->
{:halt, {:ok, path_with_scheme}}
{:error, reason} ->
{:cont, {:error, docker_socket_path: reason}}
end
else
{:cont, {:error, docker_socket_path: :docker_socket_not_found}}
end
end
)
end
end
end