Add support for .with_network and .with_network_aliases#84
Add support for .with_network and .with_network_aliases#84dieter-medium wants to merge 2 commits intotestcontainers:mainfrom
Conversation
afac587 to
31bbef6
Compare
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.
There was a problem hiding this comment.
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
Networkclass for creating and managing Docker networks - Adds
with_networkandwith_network_aliasesmethods to theDockerContainerclass - 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 |
There was a problem hiding this comment.
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.
| self.aliases += aliases&.flatten | |
| self.aliases += aliases.flatten |
| end | ||
|
|
||
| def _networking_config | ||
| return nil unless network_name && !aliases&.empty? |
There was a problem hiding this comment.
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.
| return nil unless network_name && !aliases&.empty? | |
| return nil unless network_name && aliases&.any? |
| "Binds" => @filesystem_binds | ||
| }.compact | ||
| }.compact, | ||
| "NetworkingConfig": _networking_config |
There was a problem hiding this comment.
Using both string keys and symbol keys in the same hash is inconsistent. Use either 'NetworkingConfig' or convert other keys to symbols for consistency.
| "NetworkingConfig": _networking_config | |
| "NetworkingConfig" => _networking_config |
| script = <<~RUBY | ||
| require "testcontainers" | ||
| Testcontainers::Network::SHARED.create | ||
| # no explicit close – rely on at_exit |
There was a problem hiding this comment.
Missing space after the comment marker. Should be # no explicit close – rely on at_exit.
|
I've extended this with #93, and you'll still be credited as co-author. |
Like discussed in #80 this is my Proposal in order to add
with_networkandwith_network_aliases