Skip to content

fix: handle port mappings with explicit IP bindings#60

Merged
Mcrich23 merged 3 commits intoMcrich23:mainfrom
ravshansbox:fix/port-binding-format
Mar 8, 2026
Merged

fix: handle port mappings with explicit IP bindings#60
Mcrich23 merged 3 commits intoMcrich23:mainfrom
ravshansbox:fix/port-binding-format

Conversation

@ravshansbox
Copy link
Contributor

Summary

Fixes the "invalid publish IPv4 address" error when docker-compose.yml port mappings include explicit IP bindings (e.g., 127.0.0.1:5432:5432).

Changes

  • Added composePortToRunArg() helper function in Helper Functions.swift that correctly parses Docker Compose port specifications into container run -p format
  • Updated configService() in ComposeUp.swift to use the new helper instead of blindly prepending 0.0.0.0:
  • Added 8 unit tests in HelperFunctionsTests.swift covering all port format scenarios

What was wrong

Previous code did:

runCommandArgs.append("0.0.0.0:\(resolvedPort)")

This produced invalid address 0.0.0.0:127.0.0.1:5432:5432 when the port spec already included an IP.

How it works now

The new parser intelligently handles all formats:

  • 30000.0.0.0:3000:3000 (auto-completes)
  • 8080:30000.0.0.0:8080:3000 (adds IP binding)
  • 127.0.0.1:5432:5432127.0.0.1:5432:5432 (preserves explicit IP)
  • [::1]:3000:3000[::1]:3000:3000 (preserves IPv6)
  • All with optional /tcp or /udp suffixes preserved

Testing

✅ All 70+ existing tests pass
✅ 8 new port conversion tests pass
✅ Build succeeds with no warnings

Closes #59

The previous implementation blindly prepended '0.0.0.0:' to every port specification, causing invalid formats like '0.0.0.0:127.0.0.1:5432:5432' when the compose file already included an IP binding.

Introduced composePortToRunArg helper function that:
- Handles simple port (e.g., '3000') → '0.0.0.0:3000:3000'
- Handles host:container pairs (e.g., '8080:3000') → '0.0.0.0:8080:3000'
- Preserves explicit IP bindings (e.g., '127.0.0.1:5432:5432') as-is
- Supports IPv6 bracket notation (e.g., '[::1]:3000:3000')
- Preserves protocol suffixes (/tcp, /udp)

Added comprehensive unit tests covering all port format variations.

Fixes the 'invalid publish IPv4 address' error when using explicit IP bindings in docker-compose.yml.
@Mcrich23
Copy link
Owner

Mcrich23 commented Mar 6, 2026

I love this so much. Let me verify on my end and then we can get this merged!

Reapply the explicit IP port binding changes without indentation-only churn so the PR stays focused on behavior and easier to review.
Copy link
Owner

@Mcrich23 Mcrich23 left a comment

Choose a reason for hiding this comment

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

Thank you so much for fixing that. One more thing: can you please add a dynamic test for the full check bringing up the container?

Thank you!

Verify compose up/down behavior with an explicit host IP port binding so the runtime path is covered end to end.
@ravshansbox
Copy link
Contributor Author

ravshansbox commented Mar 6, 2026

Implemented — I added a dynamic end-to-end test that brings the container up with an explicit IP port mapping and validates the full up/down lifecycle.

Details:

  • Added testComposeUpWithExplicitIPPortMapping in Tests/Container-Compose-DynamicTests/ComposeUpTests.swift
  • Verifies published ports are exactly 127.0.0.1:18081:80
  • Runs ComposeUp (detached), checks container is running, then runs ComposeDown and checks container is stopped
  • Included in commit 626f977

Thanks for the review.

@Mcrich23 Mcrich23 merged commit 5e6bd56 into Mcrich23:main Mar 8, 2026
1 check passed
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.

Port mapping fails when compose file specifies explicit IP binding

2 participants