### Initializing and Tearing Down Redis Container with Testcontainers (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/docs/QuickStart.md This code defines the `setup` and `teardown` methods within a Minitest class to manage a Redis Docker container. The `setup` method initializes and starts a Redis container, exposing port 6379, while the `teardown` method ensures the container is stopped after tests, providing a clean environment for each test run. ```Ruby require "testcontainers" require_relative "redis_backed_cache" require "minitest/autorun" class RedisBackedCacheTest < Minitest::Test def setup @redis_container = Testcontainers::DockerContainer.new("redis:6.2-alpine").with_exposed_ports(6379) @redis_container.start end def teardown @redis_container.stop if @redis_container end end ``` -------------------------------- ### Connecting Application to Testcontainers Redis Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/docs/QuickStart.md This snippet extends the `setup` method to connect the `RedisBackedCache` application to the running Testcontainers Redis instance. It waits for the Redis TCP port to be available, then retrieves the container's dynamically assigned host and mapped port to initialize the `RedisBackedCache` instance, ensuring the application communicates with the test container. ```Ruby def setup @redis_container = Testcontainers::DockerContainer.new("redis:6.2-alpine").with_exposed_ports(6379) @redis_container.start @redis_container.wait_for_tcp_port(6379) host = @redis_container.host port = @redis_container.mapped_port(6379) @under_test = RedisBackedCache.new(host, port) end ``` -------------------------------- ### Complete Selenium Container Usage Example (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/selenium/README.md This comprehensive example demonstrates the full lifecycle: creating and starting a Selenium container, initializing a Selenium WebDriver client using the container's URL, navigating to a webpage, and taking a screenshot. ```ruby require "testcontainers/selenium" require "selenium-webdriver" container = Testcontainers::SeleniumContainer.new container.start driver = Selenium::WebDriver.for(:firefox, :url => @container.selenium_url) driver.navigate.to "https://www.google.com" driver.screenshot ``` -------------------------------- ### Setting Up Ruby Gem Development Environment Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/core/README.md This command runs the setup script provided with the gem, typically installing all necessary development dependencies. It's the first step after checking out the repository to prepare for development. ```Shell bin/setup ``` -------------------------------- ### Writing a Basic Test Method with Testcontainers (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/docs/QuickStart.md This example demonstrates a simple Minitest method for testing the `RedisBackedCache` class. It performs a `put` operation to store data in the Testcontainers-managed Redis instance and then a `get` operation to retrieve it, asserting that the retrieved value matches the original, thereby validating the cache's functionality. ```Ruby def test_simple_put_and_get @under_test.put("test", "example") retrieved = @under_test.get("test") assert_equal "example", retrieved end ``` -------------------------------- ### Complete MySQL Container Usage Example (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mysql/README.md This comprehensive example demonstrates the full lifecycle of a MySQL container: initializing, starting, connecting with `mysql2` gem using the generated URL, executing a simple query, and finally closing the client and stopping the container. ```ruby require 'testcontainers/mysql' require 'mysql2' container = Testcontainers::MysqlContainer.new container.start client = Mysql2::Client.new(url: container.database_url) result = client.query("SELECT 1") result.each do |row| puts row.inspect end client.close container.stop ``` -------------------------------- ### Complete Postgres Container Usage Example (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/postgres/README.md This comprehensive example demonstrates the full lifecycle of using `testcontainers-postgres`. It includes requiring necessary gems, creating and starting a container, connecting with the `pg` gem, executing a simple query, and finally closing the client and stopping the container. ```Ruby require "testcontainers/postgres" require "pg" container = Testcontainers::PostgresContainer.new container.start client = PG.connect(container.database_url) result = client.exec("SELECT 1 AS number") result.each do |row| puts row.inspect end client.close container.stop ``` -------------------------------- ### Complete MariaDB Container Usage Example with `mysql2` Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mariadb/README.md This comprehensive example demonstrates the full lifecycle of a MariaDB container: initializing, starting, connecting with the `mysql2` gem using the generated database URL, executing a simple query, and finally closing the client and stopping the container. It showcases typical integration for testing. ```ruby require "testcontainers/mariadb" require "mysql2" container = Testcontainers::MariadbContainer.new container.start client = Mysql2::Client.new(url: container.database_url) result = client.query("SELECT 1") result.each do |row| puts row.inspect end client.close container.stop ``` -------------------------------- ### Complete Elasticsearch container usage example (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/elasticsearch/README.md This comprehensive example demonstrates the full lifecycle: requiring libraries, creating and starting an Elasticsearch container, connecting with the `elasticsearch` gem, performing a ping, and finally stopping the container. ```Ruby require "testcontainers/elasticsearch" require "elasticsearch" container = Testcontainers::ElasticsearchContainer.new container.start client = Elasticsearch::Client.new(url: container.elasticsearch_url) client.ping #=> true container.stop ``` -------------------------------- ### Complete Redis Container Usage Example in Ruby Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/redis/README.md This comprehensive example illustrates the full lifecycle of using `testcontainers-redis`. It includes requiring necessary gems, creating and starting a default Redis container, connecting to it using the `redis` gem, performing a simple set and get operation, printing the result, and finally, gracefully quitting the client and stopping the container. ```Ruby require 'testcontainers/redis' require 'redis' container = Testcontainers::RedisContainer.new container.start client = Redis.new(url: container.redis_url) client.set("mykey", "hello world") value = client.get("mykey") puts value client.quit container.stop ``` -------------------------------- ### Complete MongoDB container usage example (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mongo/README.md This comprehensive example demonstrates the full lifecycle of using `testcontainers-mongo`: creating, starting, connecting to a MongoDB container, performing an insert and query using the `mongo` gem, and finally stopping the container. ```ruby require "testcontainers/mongo" require "mongo" container = Testcontainers::MongoContainer.new container.start client = Mongo::Client.new(container.database_url, auth_source: "admin") client[:artists].insert_one({:name => "FKA Twigs"}) client[:artists].find(:name => "FKA Twigs").each do |document| document.inspect end client.close container.stop ``` -------------------------------- ### Starting Postgres Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/postgres/README.md This snippet calls the `start` method on a `Testcontainers::PostgresContainer` instance. This action pulls the Docker image (if not already present), creates, and starts the container, making it ready for connections. ```Ruby container.start ``` -------------------------------- ### Publishing a Message to RabbitMQ Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/rabbitmq/README.md This comprehensive example demonstrates creating, starting, and connecting to a RabbitMQ container using `testcontainers-rabbitmq` and the `bunny` gem. It then publishes a 'Hello World!' message to a queue named 'hello', showcasing a full publish flow. ```ruby require "testcontainers/rabbitmq" require "bunny" container = Testcontainers::RabbitmqContainer.new container.start connection = Bunny.new(container.rabbitmq_url) connection.start channel = connection.create_channel queue = channel.queue('hello') channel.default_exchange.publish('Hello World!', routing_key: queue.name) connection.close ``` -------------------------------- ### Executing `bundle install` for Gem Installation (Bash) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/rabbitmq/README.md After adding the gem to the Gemfile, this command executes `bundle install` to download and install the specified gem and its dependencies. This is a standard step in Ruby projects to manage project dependencies. ```bash $ bundle install ``` -------------------------------- ### Installing `testcontainers-rabbitmq` Gem Directly (Bash) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/rabbitmq/README.md This command demonstrates how to install the `testcontainers-rabbitmq` gem directly using the `gem install` utility. This method is suitable for global installation or when not managing dependencies via a Gemfile. ```bash $ gem install testcontainers-rabbitmq ``` -------------------------------- ### Starting a Testcontainers Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/redpanda/README.md This snippet invokes the `start` method on a `container` object, which initiates the Docker container. This action pulls the necessary image (if not already present) and starts the Redpanda service, making it ready for connections. ```Ruby container.start ``` -------------------------------- ### Starting Testcontainers Selenium Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/selenium/README.md Once a `Testcontainers::SeleniumContainer` instance is created, call the `start` method to launch the container and make it ready for connections. ```ruby container.start ``` -------------------------------- ### Executing `bundle install` Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mariadb/README.md This command executes `bundle install` to resolve and install the gems specified in your Gemfile, including `testcontainers-mariadb`. It ensures all project dependencies are met. ```bash $ bundle install ``` -------------------------------- ### Starting MariaDB Container Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mariadb/README.md This method call starts the MariaDB container, making it ready to accept connections. It handles the underlying Docker operations to bring the container online. ```ruby container.start ``` -------------------------------- ### Installing testcontainers-nginx Gem (Bash) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/nginx/README.md Installs the `testcontainers-nginx` gem directly using the `gem install` command, useful for standalone installation without Bundler. ```Bash $ gem install testcontainers-nginx ``` -------------------------------- ### Starting Testcontainers MySQL Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mysql/README.md This method call initiates the startup process for the MySQL container, pulling the image if necessary and making the database service available for connections. ```ruby container.start ``` -------------------------------- ### Installing testcontainers-mysql Gem (Bash) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mysql/README.md This command directly installs the `testcontainers-mysql` gem globally or into the current Ruby environment, providing an alternative to Bundler for installation. ```bash $ gem install testcontainers-mysql ``` -------------------------------- ### Adding Testcontainers Gem Dependency (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/docs/QuickStart.md This snippet shows how to add the Testcontainers gem to your Ruby project's Gemfile. This is the first step to include the library as a dependency, allowing you to use its functionalities for container management in your tests. ```Ruby gem "testcontainers" ``` -------------------------------- ### Installing Testcontainers Gem Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/README.md These commands demonstrate how to install the Testcontainers Ruby gem. The first command uses Bundler to add the gem to a project's Gemfile, while the second command directly installs the gem if Bundler is not in use. ```Shell bundle add testcontainers ``` ```Shell gem install testcontainers ``` -------------------------------- ### Installing Testcontainers-Compose Gem (Bash) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/compose/README.md Installs the `testcontainers-compose` gem directly using the `gem install` command. This method is suitable for global installation or when not using Bundler. ```bash $ gem install testcontainers-compose ``` -------------------------------- ### Starting a Testcontainers RabbitMQ Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/rabbitmq/README.md This snippet invokes the `start` method on a `Testcontainers::RabbitmqContainer` instance. This action initiates the Docker container, making the RabbitMQ instance available for connections. ```ruby container.start ``` -------------------------------- ### Installing `testcontainers-mongo` via `gem install` (Bash) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mongo/README.md This command directly installs the `testcontainers-mongo` gem using the `gem install` utility, providing an alternative to Bundler for standalone installation. ```bash $ gem install testcontainers-mongo ``` -------------------------------- ### Executing Bundle Install (Bash) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/postgres/README.md This command executes `bundle install`, which reads the `Gemfile` and installs all specified gems, including `testcontainers-postgres`, along with their dependencies. This is a standard step after modifying the `Gemfile`. ```Bash $ bundle install ``` -------------------------------- ### Initializing and Starting Redis Container with Testcontainers Ruby Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/README.md This Ruby snippet demonstrates how to create and start a generic Docker container using Testcontainers. It initializes a Redis container, exposes port 6379, and then starts the container, making it available for testing. ```Ruby container = Testcontainers::DockerContainer.new("redis:6.2-alpine").with_exposed_port(6379) container.start ``` -------------------------------- ### Installing Testcontainers Redpanda Gem Directly (Bash) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/redpanda/README.md This command directly installs the `testcontainers-redpanda` gem using the `gem` command-line utility. This method is suitable for standalone installation without a `Gemfile` or for installing globally. ```Bash $ gem install testcontainers-redpanda ``` -------------------------------- ### Installing Ruby Gems with Bundler (Bash) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/redpanda/README.md This command executes `bundle install`, which reads the `Gemfile` and installs all specified Ruby gems and their dependencies. It's a standard step after modifying the `Gemfile` to ensure all required libraries are available. ```Bash $ bundle install ``` -------------------------------- ### Full Redpanda Container Usage with Rdkafka (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/redpanda/README.md This comprehensive example illustrates the complete lifecycle of a Redpanda container within a Ruby application. It demonstrates initializing and starting the container, configuring `rdkafka` clients using the container's connection URL, producing and consuming a message, and finally stopping the container. It requires `testcontainers-redpanda` and `rdkafka` gems. ```Ruby require "testcontainers/redpada" require "rdkafka" container = Testcontainers::RedpandaContainer.new container.start config = { "bootstrap.servers": container.connection_url, "group.id": "ruby-test" } consumer = Rdkafka::Config.new(config).consumer consumer.subscribe("ruby-test-topic") producer = Rdkafka::Config.new(config).producer producer.produce(payload: "Hello, Redpanda!", topic: "ruby-test-topic").wait message = consumer.each do |msg| break msg end puts msg.inspect container.stop ``` -------------------------------- ### Initializing Default Redis Container in Ruby Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/redis/README.md This snippet creates a new instance of `Testcontainers::RedisContainer` using its default constructor. This initializes a Redis container with the default Redis image and configuration, ready for further setup or starting. ```Ruby container = Testcontainers::RedisContainer.new ``` -------------------------------- ### Starting an Elasticsearch container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/elasticsearch/README.md This method call starts the previously created Elasticsearch container, pulling the image if necessary and making it ready for connections. ```Ruby container.start ``` -------------------------------- ### Installing testcontainers-elasticsearch via gem (Bash) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/elasticsearch/README.md This command directly installs the `testcontainers-elasticsearch` gem using the `gem install` utility, useful for standalone installation without a Gemfile. ```Bash $ gem install testcontainers-elasticsearch ``` -------------------------------- ### Installing `testcontainers-redis` Gem Globally Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/redis/README.md This command installs the `testcontainers-redis` gem globally on your system using the `gem install` utility. This method is an alternative to Bundler for installing the gem directly. ```Bash $ gem install testcontainers-redis ``` -------------------------------- ### Starting Docker Compose Services (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/compose/README.md Initiates the startup of all services defined in the Docker Compose file associated with the `compose` instance. This method brings up the containers as defined, making them ready for interaction. ```ruby compose.start ``` -------------------------------- ### Executing bundle install (Bash) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/elasticsearch/README.md This command executes `bundle install` to resolve and install the dependencies listed in the Gemfile, including `testcontainers-elasticsearch`. ```Bash $ bundle install ``` -------------------------------- ### Installing testcontainers-postgres via Gem (Bash) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/postgres/README.md This command directly installs the `testcontainers-postgres` gem using the `gem` command-line utility. This method is suitable for global installation or when not managing dependencies via a `Gemfile`. ```Bash $ gem install testcontainers-postgres ``` -------------------------------- ### Installing `testcontainers-mariadb` Gem Manually Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mariadb/README.md This command globally installs the `testcontainers-mariadb` gem using `gem install`. This method is suitable for standalone usage or when not managing dependencies via a Gemfile. ```bash $ gem install testcontainers-mariadb ``` -------------------------------- ### Starting a MongoDB container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mongo/README.md This method call starts the previously initialized `Testcontainers::MongoContainer`, launching the MongoDB instance within a Docker container. ```ruby container.start ``` -------------------------------- ### Executing `bundle install` (Bash) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mongo/README.md This command executes `bundle install` to resolve and install the gems listed in your Gemfile, including `testcontainers-mongo`, ensuring all dependencies are met. ```bash $ bundle install ``` -------------------------------- ### Installing Ruby Gems with Bundler (Bash) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/nginx/README.md Executes `bundle install` to install the gems listed in the Gemfile, including `testcontainers-nginx`, resolving dependencies. ```Bash $ bundle install ``` -------------------------------- ### Installing Testcontainers Selenium Gem Directly (Bash) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/selenium/README.md Alternatively, you can install the `testcontainers-selenium` gem directly from the command line using the `gem install` command, bypassing the Gemfile if preferred. ```bash $ gem install testcontainers-selenium ``` -------------------------------- ### Starting Nginx Testcontainer (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/nginx/README.md Initiates the Nginx Docker container managed by Testcontainers, making it ready to accept connections. This method blocks until the container is running and healthy. ```Ruby container.start ``` -------------------------------- ### Installing Ruby Gems with Bundler (Bash) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mysql/README.md This command executes Bundler to install all gems specified in the Gemfile, including `testcontainers-mysql`, ensuring all project dependencies are met. ```bash $ bundle install ``` -------------------------------- ### Installing Ruby Gems with Bundler (Bash) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/selenium/README.md After updating the Gemfile, execute this command in your terminal to install all specified gems, including `testcontainers-selenium`, using Bundler. ```bash $ bundle install ``` -------------------------------- ### Installing Ruby Gem Locally for Development Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/core/README.md This command installs the current version of the gem from the local repository onto your machine. It's useful for testing the gem as if it were installed from RubyGems.org before an official release. ```Shell bundle exec rake install ``` -------------------------------- ### Creating a Default RabbitMQ Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/rabbitmq/README.md This snippet initializes a new `Testcontainers::RabbitmqContainer` instance with default settings. It will use the default RabbitMQ image, user, password, and vhost, providing a quick way to get a basic container running. ```ruby container = Testcontainers::RabbitmqContainer.new ``` -------------------------------- ### Installing Ruby Gems with Bundler (Bash) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/compose/README.md Executes the `bundle install` command to install all gems listed in the Gemfile, including `testcontainers-compose`. This command resolves dependencies and sets up the project's Ruby environment. ```bash $ bundle install ``` -------------------------------- ### Installing Ruby Gems with Bundler Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/redis/README.md This command executes `bundle install`, which reads your Gemfile and installs all the specified gem dependencies, including `testcontainers-redis`, into your project. This step is necessary after modifying the Gemfile. ```Bash $ bundle install ``` -------------------------------- ### Initializing Default MariaDB Container Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mariadb/README.md This snippet creates a new instance of `Testcontainers::MariadbContainer` using default settings for the MariaDB image, user, password, and database. It's the simplest way to get a container instance. ```ruby container = Testcontainers::MariadbContainer.new ``` -------------------------------- ### Starting Redis Container in Ruby Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/redis/README.md This method call initiates the Redis container, pulling the necessary Docker image if not already present, and starting the Redis service. The container will then be accessible for connections. ```Ruby container.start ``` -------------------------------- ### Initializing Default MySQL Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mysql/README.md This snippet creates a new instance of `Testcontainers::MysqlContainer` using default settings for the MySQL image, user, password, and database. It's the simplest way to get a container ready for use. ```ruby container = Testcontainers::MysqlContainer.new ``` -------------------------------- ### Creating Default Postgres Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/postgres/README.md This snippet initializes a new `Testcontainers::PostgresContainer` instance with default settings. It uses the default Postgres image, user, password, and database, providing a quick way to get a container running. ```Ruby container = Testcontainers::PostgresContainer.new ``` -------------------------------- ### Initializing Compose with Specific Services (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/compose/README.md Initializes `Testcontainers::ComposeContainer` to manage only a subset of services defined in the Docker Compose file. By specifying `services: ["hub", "firefox"]`, only these two services will be started and managed by the instance. ```ruby services = ["hub","firefox"] compose = Testcontainers::ComposeContainer.new(filepath: Dir.getwd, services: services) ``` -------------------------------- ### Installing Ruby Gem Globally with Gem Command Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/core/README.md This command installs the specified Ruby gem directly into your system's gem repository. Use this method if you are not using Bundler to manage your project's dependencies or for global utility gems. ```Shell $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG ``` -------------------------------- ### Starting Interactive Ruby Console Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/core/README.md This command launches an interactive Ruby console (IRB) with the gem's code loaded. It allows developers to experiment with the gem's functionalities and debug issues in real-time. ```Shell bin/console ``` -------------------------------- ### Customizing RabbitMQ Container with Chained Methods (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/rabbitmq/README.md This snippet illustrates how to customize a RabbitMQ container using `with_` methods for vhost, username, and password. These methods allow for fluent configuration of container properties before starting it. ```ruby container.with_vhost("custom_vhost") container.with_username("custom_user") container.with_password("custom_pass") ``` -------------------------------- ### Installing testcontainers-postgres in Gemfile (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/postgres/README.md This snippet shows how to add the `testcontainers-postgres` gem to your application's `Gemfile` within the `:test` group. This ensures the gem is only loaded during testing, managing dependencies efficiently. ```Ruby group :test do gem "testcontainers-postgres" end ``` -------------------------------- ### Installing `testcontainers-mariadb` Gem in Gemfile Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mariadb/README.md This snippet adds the `testcontainers-mariadb` gem to the `test` group in your application's Gemfile, ensuring it's only loaded during testing. This is the recommended way to include the library as a development dependency. ```ruby group :test do gem "testcontainers-mariadb" end ``` -------------------------------- ### Customizing MariaDB Container with Chained Methods Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mariadb/README.md These methods allow for fluent customization of the MariaDB container's database name, username, and password after initialization but before starting. They return the container instance, enabling method chaining. ```ruby container.with_database("custom_db") container.with_username("custom_user") container.with_password("custom_pass") ``` -------------------------------- ### Integrating Testcontainers MySQL with RSpec (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mysql/README.md This RSpec configuration snippet shows how to manage a MySQL container across a test suite. It initializes and starts the container before all tests (`before(:suite)`), sets an environment variable for the database URL, and ensures the container is stopped and removed after all tests (`after(:suite)`). ```ruby RSpec.configure do |config| # This helps us to have access to the `RSpec.configuration.mysql_container` without using global variables. config.add_setting :mysql, default: nil config.before(:suite) do config.mysql_container = Testcontainers::MysqlContainer.new.start ENV["DATABASE_URL"] = config.mysql_container.database_url(protocol: "mysql2") # or you can expose it to a fixed port and use database.yml for configuration end config.after(:suite) do config.mysql_container&.stop config.mysql_container&.remove end end ``` -------------------------------- ### Installing `testcontainers-rabbitmq` Gem in Gemfile (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/rabbitmq/README.md This snippet shows how to add the `testcontainers-rabbitmq` gem to your application's Gemfile, specifically within the `:test` group. This ensures the gem is only loaded during testing environments, preventing unnecessary dependencies in production. ```ruby group :test do gem 'testcontainers-rabbitmq' end ``` -------------------------------- ### Customizing MongoDB container with methods (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mongo/README.md These methods allow for fluent customization of the MongoDB container's database name, username, and password before it is started, providing an alternative to constructor arguments. ```ruby container.with_database("custom_db") container.with_username("custom_user") container.with_password("custom_pass") ``` -------------------------------- ### Consuming Messages from RabbitMQ Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/rabbitmq/README.md This example illustrates how to set up a RabbitMQ container, connect to it with `bunny`, and consume messages from a queue named 'hello'. It uses a blocking subscribe call to process incoming messages and print their body, demonstrating a basic consumption pattern. ```ruby require "testcontainers/rabbitmq" require "bunny" container = Testcontainers::RabbitmqContainer.new container.start connection = Bunny.new(container.rabbitmq_url) connection.start channel = connection.create_channel queue = channel.queue('hello') queue.subscribe(block: true) do |_delivery_info, _properties, body| puts " [x] Received #{body}" end connection.close ``` -------------------------------- ### Customizing MySQL Container Properties (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mysql/README.md These methods allow for fluent customization of the MySQL container's properties, such as the database name, username, and password, before the container is started. ```ruby container.with_database("custom_db") container.with_username("custom_user") container.with_password("custom_pass") ``` -------------------------------- ### Installing `testcontainers-mongo` in Gemfile (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mongo/README.md This snippet shows how to add the `testcontainers-mongo` gem to your application's Gemfile, specifically within the `:test` group, to include it as a testing dependency. ```ruby group :test do gem "testcontainers-mongo" end ``` -------------------------------- ### Installing `testcontainers-redis` Gem in Ruby Gemfile Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/redis/README.md This snippet shows how to add the `testcontainers-redis` gem to your application's Gemfile, specifically within the `:test` group, to include it for testing purposes. This is a prerequisite for using the library. ```Ruby group :test do gem 'testcontainers-redis' end ``` -------------------------------- ### Integrating MariaDB Container with RSpec Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mariadb/README.md This RSpec configuration snippet shows how to manage a MariaDB container within your test suite. It initializes and starts the container before all tests (`before(:suite)`) and stops/removes it after all tests (`after(:suite)`), making the container available via `RSpec.configuration` and setting an environment variable for the database URL. ```ruby RSpec.configure do |config| # This helps us to have access to the `RSpec.configuration.mariadb_container` without using global variables. config.add_setting :mariadb_container, default: nil config.before(:suite) do config.mariadb_container = Testcontainers::MariadbContainer.new.start ENV["DATABASE_URL"] = config.mariadb_container.database_url(protocol: "mysql2") # or you can expose it to a fixed port and use database.yml for configuration end config.after(:suite) do config.mariadb_container&.stop config.mariadb_container&.remove end end ``` -------------------------------- ### Customizing Postgres Container Properties (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/postgres/README.md These methods allow for fluent customization of the Postgres container's properties before it starts. You can set the database name, username, and password, which will be applied when the container is initialized. ```Ruby container.with_database("custom_db") container.with_username("custom_user") container.with_password("custom_pass") ``` -------------------------------- ### Installing testcontainers-elasticsearch in Gemfile (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/elasticsearch/README.md This snippet shows how to add `testcontainers-elasticsearch` to your application's Gemfile, specifically within the `:test` group, to manage it as a test dependency. ```Ruby group :test do gem 'testcontainers-elasticsearch' end ``` -------------------------------- ### Generating MySQL Container Database URL (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mysql/README.md This method generates a complete database URL string for the running MySQL container, simplifying connection setup with client libraries that support URL-based configuration. ```ruby database_url = container.database_url ``` -------------------------------- ### Initializing Customized Redis Container in Ruby Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/redis/README.md This snippet demonstrates how to create a `Testcontainers::RedisContainer` instance with custom parameters. It specifies a particular Redis image version (`redis:6.0-alpine`) and sets a custom password for client authentication, allowing for tailored container setups. ```Ruby container = Testcontainers::RedisContainer.new("redis:6.0-alpine", password: "custom_pass") ``` -------------------------------- ### Integrating Postgres Container with RSpec (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/postgres/README.md This RSpec configuration snippet shows how to manage a `Testcontainers::PostgresContainer` across your test suite. It initializes and starts the container before all tests (`before(:suite)`) and stops and removes it after all tests (`after(:suite)`), ensuring a clean test environment. ```Ruby RSpec.configure do |config| # This helps us to have access to the `RSpec.configuration.postgres_container` without using global variables. config.add_setting :postgres_container, default: nil config.before(:suite) do config.postgres_container = Testcontainers::PostgresContainer.new.start ENV["DATABASE_URL"] = config.postgres_container.database_url # or you can expose it to a fixed port and use database.yml for configuration end config.after(:suite) do config.postgres_container&.stop config.postgres_container&.remove end end ``` -------------------------------- ### Accessing Container Host and Mapped Port in Testcontainers Ruby Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/README.md After a container is started, this Ruby code shows how to retrieve its dynamically assigned host and the mapped port for a specific exposed port. This information is crucial for connecting test clients to the containerized service. ```Ruby host = container.host port = container.mapped_port(6379) ``` -------------------------------- ### Generating MongoDB database URL (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mongo/README.md This method generates a complete database URL string for the running MongoDB container, simplifying connection setup with client libraries. ```ruby database_url = container.database_url ``` -------------------------------- ### Generating RabbitMQ Connection URL (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/rabbitmq/README.md This snippet demonstrates how to generate a complete RabbitMQ connection URL directly from the container instance. This URL includes all necessary details like host, port, username, password, and vhost, simplifying connection setup. ```ruby broker_url = container.rabbitmq_url ``` -------------------------------- ### Integrating Redpanda Container Lifecycle with RSpec (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/redpanda/README.md This snippet shows how to integrate the Redpanda container's lifecycle management into RSpec's test suite. It uses `before(:suite)` to initialize and start the container once for all tests, and `after(:suite)` to stop and remove it, ensuring efficient resource management and a clean state across test runs. It adds a custom setting to RSpec configuration for container access. ```Ruby RSpec.configure do |config| # This helps us to have access to the `RSpec.configuration.redpanda_container` without using global variables. config.add_setting :redpanda_container, default: nil config.before(:suite) do config.redpanda_container = Testcontainers::RedpandaContainer.new.start # Use the container connection details in your tests end config.after(:suite) do config.redpanda_container&.stop config.redpanda_container&.remove end end ``` -------------------------------- ### Setting Redis Container Password in Ruby Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/redis/README.md This method allows setting a custom password for the Redis container before it starts. This password will be required for client authentication when connecting to the Redis instance. ```Ruby container.with_password("custom_pass") ``` -------------------------------- ### Retrieving Redpanda Container Connection URL (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/redpanda/README.md This snippet shows an alternative way to get connection details by calling `container.connection_url`. This method provides a formatted URL string suitable for direct use with Kafka client libraries, simplifying the connection process. ```Ruby connection_url = container.connection_url ``` -------------------------------- ### Styling a Bootstrap Example Divider in CSS Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/examples/fixtures/web_page/index.html Defines the visual properties for a custom divider element, including its width, height, background color, border, and box shadow. This creates a visually distinct horizontal separator within the layout. ```CSS .b-example-divider { width: 100%; height: 3rem; background-color: rgba(0, 0, 0, .1); border: solid rgba(0, 0, 0, .15); border-width: 1px 0; box-shadow: inset 0 .5em 1.5em rgba(0, 0, 0, .1), inset 0 .125em .5em rgba(0, 0, 0, .15); } ``` -------------------------------- ### Styling a Bootstrap Example Vertical Rule in CSS Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/examples/fixtures/web_page/index.html Sets the styling for a vertical rule element, ensuring it shrinks to fit, has a fixed width, and occupies the full viewport height. This class is used for creating vertical visual separations. ```CSS .b-example-vr { flex-shrink: 0; width: 1.5rem; height: 100vh; } ``` -------------------------------- ### Executing Command in Compose Service Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/compose/README.md Executes a command (e.g., `echo test`) within a running container of a specific service (e.g., 'hub') managed by the `compose` instance. This is useful for debugging, setup, or running one-off tasks inside the service container. ```ruby compose.exec(service_name: "hub", command: "echo test") ``` -------------------------------- ### Initializing Redpanda Container with Default Image (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/redpanda/README.md This snippet demonstrates how to create a new instance of `Testcontainers::RedpandaContainer`. This initializes a Redpanda container using its default image, preparing it for use in tests. No specific parameters are required for this default instantiation. ```Ruby container = Testcontainers::RedpandaContainer.new ``` -------------------------------- ### Creating a default MongoDB container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mongo/README.md This snippet initializes a new `Testcontainers::MongoContainer` instance using default settings for the MongoDB image, user, password, and database. ```ruby container = Testcontainers::MongoContainer.new ``` -------------------------------- ### Initializing Testcontainers Compose Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/compose/README.md Creates a new instance of `Testcontainers::ComposeContainer`, specifying the current directory (`Dir.getwd`) as the location for the Docker Compose file (e.g., `docker-compose.yml`). This prepares the container for managing services defined in the compose file. ```ruby compose = Testcontainers::ComposeContainer.new(filepath: Dir.getwd) ``` -------------------------------- ### Initializing Customized MariaDB Container Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mariadb/README.md This snippet initializes a `Testcontainers::MariadbContainer` with specific parameters, allowing customization of the MariaDB image version, username, password, and database name. This provides flexibility for different testing scenarios. ```ruby container = Testcontainers::MariadbContainer.new("mariadb:10.10", username: "custom_user", password: "custom_pass", database: "custom_db") ``` -------------------------------- ### Initializing Compose with Multiple Files (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/compose/README.md Configures `Testcontainers::ComposeContainer` to use multiple Docker Compose files. This allows for modular service definitions, combining services from `docker-compose.dbs.yml` and `docker-compose.web.yml` into a single managed environment. ```ruby compose_filenames = ["docker-compose.dbs.yml", "docker-compose.web.yml"] compose = Testcontainers::ComposeContainer.new(filepath: Dir.getwd, compose_filenames: compose_filenames) compose.start ``` -------------------------------- ### Initializing Customized MySQL Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mysql/README.md This snippet demonstrates how to create a `Testcontainers::MysqlContainer` instance with custom parameters, allowing specification of the MySQL image version, username, password, and database name during initialization. ```ruby container = Testcontainers::MysqlContainer.new("mysql:5.7", username: "custom_user", password: "custom_pass", database: "custom_db") ``` -------------------------------- ### Retrieving MySQL Container Host and Port (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mysql/README.md These lines retrieve the dynamically assigned host and the first mapped port of the running MySQL container, which are essential for establishing a direct connection to the database. ```ruby host = container.host port = container.first_mapped_port ``` -------------------------------- ### Retrieving Postgres Container Host and Port (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/postgres/README.md These methods retrieve the dynamically assigned host and the first mapped port of the running Postgres container. These details are essential for establishing a direct connection to the database. ```Ruby host = container.host port = container.first_mapped_port ``` -------------------------------- ### Configuring Nginx Container Filesystem Binds (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/nginx/README.md Sets up read-only filesystem binds for the Nginx container, allowing custom configuration files (`/etc/nginx/conf.d`) or web content (`/usr/share/nginx/html`) to be mounted from local paths. ```Ruby container.with_filesystem_binds(["/local/path/custom/conf:/etc/nginx/conf.d:ro"]) container.with_filesystem_binds(["/local/path/custom/content:/usr/share/nginx/html:ro"]) ``` -------------------------------- ### Initializing Redpanda Container with Custom Image (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/redpanda/README.md This snippet shows how to create a `Testcontainers::MariadbContainer` instance while specifying a custom Docker image. The `"vectorized/redpanda"` string is passed as an argument to the constructor, allowing the use of a specific Redpanda image instead of the default. Note: The original text has `MariadbContainer` but the context is Redpanda, so the description reflects the intent of customizing the image. ```Ruby container = Testcontainers::MariadbContainer.new("vectorized/redpanda") ``` -------------------------------- ### Creating a Customized RabbitMQ Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/rabbitmq/README.md This snippet demonstrates how to create a `Testcontainers::RabbitmqContainer` with custom configurations. You can specify the Docker image, username, password, and virtual host during initialization to match specific testing requirements. ```ruby container = Testcontainers::RabbitmqContainer.new("rabbitmq:latest", username: "custom_user", password: "custom_pass", vhost: "custom_vhost") ``` -------------------------------- ### Initializing Compose with Environment File (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/compose/README.md Initializes `Testcontainers::ComposeContainer` to load environment variables from a specified `.env` file (e.g., `.env.test`). This allows for externalizing configuration parameters for Docker Compose services, enhancing flexibility and security. ```ruby compose_filename = ["docker-compose.test.yml"] compose = Testcontainers::ComposeContainer.new(filepath: Dir.getwd, env_file: ".env.test") ``` -------------------------------- ### Creating Default Selenium Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/selenium/README.md This snippet shows how to instantiate a new `Testcontainers::SeleniumContainer` with default settings, which typically configures a Firefox browser with a VNC password of 'secret'. ```ruby container = Testcontainers::SeleniumContainer.new ``` -------------------------------- ### Creating a customized MongoDB container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mongo/README.md This snippet demonstrates how to create a `Testcontainers::MongoContainer` with custom parameters, specifying the MongoDB image version, username, password, and database name during initialization. ```ruby container = Testcontainers::MongoContainer.new("mongo:5.7", username: "custom_user", password: "custom_pass", database: "custom_db") ``` -------------------------------- ### Retrieving MariaDB Container Host and Port Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mariadb/README.md These lines retrieve the dynamically assigned host and the first mapped port of the running MariaDB container. These details are essential for establishing a connection from your application. ```ruby host = container.host port = container.first_mapped_port ``` -------------------------------- ### Creating Default Nginx Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/nginx/README.md Instantiates a new `Testcontainers::NginxContainer` object, which creates a Docker container using the default Nginx image and port configuration. ```Ruby container = Testcontainers::NginxContainer.new ``` -------------------------------- ### Waiting for Docker Compose Service Readiness (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/compose/README.md Provides methods to wait for services to reach a ready state. `wait_for_logs` waits for a specific log pattern, `wait_for_http` waits for an HTTP endpoint to be accessible, and `wait_for_tcp_port` waits for a TCP port to open, ensuring services are fully operational before tests proceed. ```ruby compose.wait_for_logs(/Service started/) compose.wait_for_http(url: "http://localhost:4444/hub") compose.wait_for_tcp_port(host: "localhost", port: 3306) ``` -------------------------------- ### Creating Customized Selenium Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/selenium/README.md Customize the Selenium container by passing arguments to the constructor, such as specifying `chrome` capabilities or disabling the VNC password for easier access. ```ruby container = Testcontainers::SeleniumContainer.new(capabilities: :chrome, vnc_no_password: true) ``` -------------------------------- ### Mounting Custom Redis Config and Command in Ruby Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/redis/README.md This snippet demonstrates advanced container customization by binding a local directory containing a `redis.conf` file to a path inside the container and then setting the container's command to use this custom configuration file. This allows for fine-grained control over the Redis server's behavior. ```Ruby container.with_filesystem_binds(["#{Dir.pwd}/custom/conf:/usr/local/etc/redis:rw"]) container.with_cmd("redis-server /usr/local/etc/redis/redis.conf") ``` -------------------------------- ### Adding testcontainers-mysql to Gemfile (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mysql/README.md This snippet shows how to add the `testcontainers-mysql` gem to your application's Gemfile, specifically within the `:test` group, to include it as a testing dependency. ```ruby group :test do gem 'testcontainers-mysql' end ``` -------------------------------- ### Creating Custom Postgres Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/postgres/README.md This snippet shows how to create a `Testcontainers::PostgresContainer` with custom parameters. You can specify the Docker image, username, password, and database name, allowing for tailored container configurations. ```Ruby container = Testcontainers::PostgresContainer.new("postgres:11", username: "custom_user", password: "custom_pass", database: "custom_db") ``` -------------------------------- ### Adding Testcontainers-Compose to Gemfile (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/compose/README.md Adds the `testcontainers-compose` gem to the `:test` group in the application's Gemfile, ensuring it's only loaded during testing. This is the first step to integrate the library into a Ruby project. ```ruby group :test do gem 'testcontainers-compose' end ``` -------------------------------- ### Obtaining MongoDB container host and port (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mongo/README.md These methods retrieve the dynamically assigned host and the first mapped port of the running MongoDB container, essential for establishing a connection. ```ruby host = container.host port = container.first_mapped_port ``` -------------------------------- ### Creating Custom Nginx Container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/nginx/README.md Instantiates a `Testcontainers::NginxContainer` with a specified Nginx image (e.g., "nginx:alpine") and a custom host port (e.g., 8080) for mapping. ```Ruby container = Testcontainers::NginxContainer.new("nginx:alpine", port: 8080) ``` -------------------------------- ### Adding Testcontainers Redpanda Gem to Gemfile (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/redpanda/README.md This snippet shows how to add the `testcontainers-redpanda` gem to your application's `Gemfile`. It specifies the gem within a `:test` group, ensuring it's only loaded during testing environments. This is a prerequisite for using the library. ```Ruby group :test do gem 'testcontainers-redpanda' end ``` -------------------------------- ### Retrieving RabbitMQ Container Host and Port (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/rabbitmq/README.md This snippet shows how to obtain the dynamically assigned host and port of the running RabbitMQ container. These details are crucial for establishing a connection from your application to the containerized RabbitMQ instance. ```ruby host = container.host port = container.first_mapped_port ``` -------------------------------- ### Generating MariaDB Database URL Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mariadb/README.md This method generates a complete database URL for the running MariaDB container, simplifying connection string creation for various database client libraries. The URL includes host, port, user, password, and database. ```ruby database_url = container.database_url ``` -------------------------------- ### Requiring Testcontainers MariaDB Library Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mariadb/README.md This line requires the `testcontainers/mariadb` library, making its classes and methods available for use in your Ruby application. It's the first step before interacting with MariaDB containers. ```ruby require "testcontainers/mariadb" ``` -------------------------------- ### Creating a default Elasticsearch container (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/elasticsearch/README.md This snippet instantiates a new `Testcontainers::ElasticsearchContainer` object, which creates a container using the default Elasticsearch Docker image. ```Ruby container = Testcontainers::ElasticsearchContainer.new ``` -------------------------------- ### Retrieving Selenium Container Host and Port (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/selenium/README.md These methods allow you to obtain the dynamically assigned host and the first mapped port of the running Selenium container, useful for direct connections or debugging. ```ruby host = container.host post = container.first_mapped_port ``` -------------------------------- ### Requiring Testcontainers MySQL Library (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/mysql/README.md This line imports the `testcontainers/mysql` library into a Ruby script, making its classes and methods available for use, which is a prerequisite for interacting with MySQL containers. ```ruby require 'testcontainers/mysql' ``` -------------------------------- ### Retrieving Redpanda Container Host and Mapped Port (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/redpanda/README.md This snippet demonstrates how to obtain the connection details for the running Redpanda container. `container.host` returns the accessible hostname, and `container.mapped_port(9092)` returns the dynamically mapped host port for the internal Redpanda port 9092. These details are essential for client applications to connect. ```Ruby host = container.host port = container.mapped_port(9092) ``` -------------------------------- ### Retrieving Nginx Container Host and Port (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/nginx/README.md Obtains the dynamically assigned host and the first mapped port of the running Nginx container, essential for connecting to it from the test environment. ```Ruby host = container.host port = container.first_mapped_port ``` -------------------------------- ### Obtaining Elasticsearch container host and port (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/elasticsearch/README.md These methods retrieve the dynamically assigned host and the first mapped port of the running Elasticsearch container, essential for establishing a connection. ```Ruby host = container.host port = container.first_mapped_port ``` -------------------------------- ### Running Ruby Gem Tests with Rake Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/core/README.md This command executes the test suite for the Ruby gem using Rake. It's essential for verifying that changes haven't introduced regressions and that all functionalities work as expected. ```Shell rake test ``` -------------------------------- ### Retrieving Docker Compose Service Logs (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/compose/README.md Outputs the aggregated logs from all services managed by the `compose` instance to the console. This is useful for debugging and monitoring the startup and runtime behavior of the services. ```ruby puts compose.logs ``` -------------------------------- ### Generating Postgres Database URL (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/postgres/README.md This method generates a complete database connection URL for the Postgres container, typically in a format like `postgres://user:password@host:port/database`. This URL simplifies connecting with various database client libraries. ```Ruby database_url = container.database_url ``` -------------------------------- ### Creating Elasticsearch container with custom image (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/elasticsearch/README.md This snippet demonstrates how to create an Elasticsearch container instance using a specific Docker image and tag, allowing for version control or custom image usage. ```Ruby container = Testcontainers::ElasticsearchContainer.new("elasticsearch:7.17.10") ``` -------------------------------- ### Requiring Testcontainers-Compose Library (Ruby) Source: https://github.com/testcontainers/testcontainers-ruby/blob/main/compose/README.md Imports the `testcontainers/compose` library into the Ruby application, making its classes and methods available for use. This is a prerequisite for interacting with Docker Compose containers. ```ruby require "testcontainers/compose" ```