Skip to content

Conversation

dieter-medium
Copy link

Like discussed in #80 this is my Proposal in order to add with_network and with_network_aliases

@dieter-medium dieter-medium force-pushed the add-network-capabilities branch from afac587 to 31bbef6 Compare May 7, 2025 15:04
Previously, the container_bridge_ip and container_gateway_ip helpers always assumed the default “bridge” network. This change updates those methods (and related logic in _networking_config) to reference @network.name when present, ensuring correct IP and gateway retrieval for user-specified networks in docker_container.rb.
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds network support to the testcontainers-core gem by implementing with_network and with_network_aliases methods for Docker containers. The changes enable containers to be connected to custom networks with specific aliases.

  • Introduces a new Network class for creating and managing Docker networks
  • Adds with_network and with_network_aliases methods to the DockerContainer class
  • Implements network configuration in container creation options

Reviewed Changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
core/testcontainers-core.gemspec Adds development dependencies for base64 and mutex_m
core/test/network_test.rb Comprehensive test suite for network creation, lifecycle, and cleanup
core/test/docker_container_test.rb Tests for container network connectivity and alias functionality
core/lib/testcontainers/network.rb New Network class implementation with shared network management
core/lib/testcontainers/docker_container.rb Network integration into DockerContainer with configuration methods
core/lib/testcontainers.rb Adds require statement for the new network module

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

#
# @param aliases [Array<String>] The aliases for the container in the network.
def with_network_aliases(*aliases)
self.aliases += aliases&.flatten
Copy link
Preview

Copilot AI Sep 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The aliases&.flatten operation will fail if aliases is nil because nil doesn't respond to flatten. Use aliases.flatten or handle the nil case explicitly.

Suggested change
self.aliases += aliases&.flatten
self.aliases += aliases.flatten

Copilot uses AI. Check for mistakes.

end

def _networking_config
return nil unless network_name && !aliases&.empty?
Copy link
Preview

Copilot AI Sep 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition !aliases&.empty? will return true when aliases is nil (since nil&.empty? returns nil, and !nil is true). This means networking config will be created even when there are no aliases. Use aliases&.any? instead.

Suggested change
return nil unless network_name && !aliases&.empty?
return nil unless network_name && aliases&.any?

Copilot uses AI. Check for mistakes.

"Binds" => @filesystem_binds
}.compact
}.compact,
"NetworkingConfig": _networking_config
Copy link
Preview

Copilot AI Sep 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using both string keys and symbol keys in the same hash is inconsistent. Use either 'NetworkingConfig' or convert other keys to symbols for consistency.

Suggested change
"NetworkingConfig": _networking_config
"NetworkingConfig" => _networking_config

Copilot uses AI. Check for mistakes.

script = <<~RUBY
require "testcontainers"
Testcontainers::Network::SHARED.create
# no explicit close – rely on at_exit
Copy link
Preview

Copilot AI Sep 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after the comment marker. Should be # no explicit close – rely on at_exit.

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants