From e769d792c1bdaacb11350f4205be96f3b958ccc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Fri, 16 Feb 2024 11:44:38 -0600 Subject: [PATCH] Support tc.host Read `tc.host` property from `~/.testcontainers.properties` file. --- core/Gemfile.lock | 5 +++++ core/lib/testcontainers/docker_container.rb | 12 +++++++++++- core/testcontainers-core.gemspec | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/core/Gemfile.lock b/core/Gemfile.lock index d072dfc..44ff35f 100644 --- a/core/Gemfile.lock +++ b/core/Gemfile.lock @@ -3,6 +3,7 @@ PATH specs: testcontainers-core (0.2.0) docker-api (~> 2.2) + properties-ruby (~> 0.0.4) GEM remote: https://rubygems.org/ @@ -15,6 +16,7 @@ GEM json (2.6.3) language_server-protocol (3.17.0.3) lint_roller (1.0.0) + logeasy (0.0.8) minitest (5.18.0) minitest-hooks (1.5.0) minitest (> 5.3) @@ -22,6 +24,8 @@ GEM parallel (1.23.0) parser (3.2.2.1) ast (~> 2.4.1) + properties-ruby (0.0.4) + logeasy (>= 0.0.4) rainbow (3.1.1) rake (13.0.6) regexp_parser (2.8.0) @@ -63,6 +67,7 @@ PLATFORMS DEPENDENCIES minitest (~> 5.0) minitest-hooks (~> 1.5) + properties-ruby (~> 0.0.4) rake (~> 13.0) standard (~> 1.3) testcontainers-core! diff --git a/core/lib/testcontainers/docker_container.rb b/core/lib/testcontainers/docker_container.rb index be3dd8f..cb7b81e 100644 --- a/core/lib/testcontainers/docker_container.rb +++ b/core/lib/testcontainers/docker_container.rb @@ -1,3 +1,4 @@ +require 'properties-ruby' module Testcontainers # The DockerContainer class is used to manage Docker containers. # It provides an interface to create, start, stop, and manipulate containers @@ -472,7 +473,16 @@ def use # @raise [ConnectionError] If the connection to the Docker daemon fails. # @raise [NotFoundError] If Docker is unable to find the image. def start - Docker::Image.create({"fromImage" => @image}.merge(@image_create_options)) + properties = Utils::Properties.load_from_file(File.expand_path('~/.testcontainers.properties')) + tc_host = properties.get(:"tc.host") + + if tc_host && !tc_host.empty? + Docker.url = tc_host + end + + connection = Docker::Connection.new(Docker.url, Docker.options) + + Docker::Image.create({"fromImage" => @image}.merge(@image_create_options), connection) @_container ||= Docker::Container.create(_container_create_options) @_container.start diff --git a/core/testcontainers-core.gemspec b/core/testcontainers-core.gemspec index db633c2..dbb3816 100644 --- a/core/testcontainers-core.gemspec +++ b/core/testcontainers-core.gemspec @@ -31,6 +31,7 @@ Gem::Specification.new do |spec| # Uncomment to register a new dependency of your gem spec.add_dependency "docker-api", "~> 2.2" + spec.add_dependency "properties-ruby", "~> 0.0.4" spec.add_development_dependency "rake", "~> 13.0" spec.add_development_dependency "minitest", "~> 5.0"