Skip to content

feat(docker): auto HTTP/S communication via network aliases instead of external_links - #7642

Merged
stasadev merged 11 commits into
mainfrom
20250919_stasadev_network_aliases
Sep 24, 2025
Merged

stasadev merged 11 commits into
mainfrom
20250919_stasadev_network_aliases

Conversation

@stasadev

@stasadev stasadev commented Sep 19, 2025 •

Copy link
Copy Markdown
Member

The Issue

Communication between two ddev projects - this is one of the most popular questions about DDEV.

How This PR Solves The Issue

Forget about manual configuration with external_links and .ddev/docker-compose.communicate.yaml, DDEV will do everything automatically.

This PR:

  • Removes the external_links from the project's .ddev/.ddev-docker-compose-base.yaml file
  • Adds Docker network aliases for ddev_default network to ~/.ddev/.router-compose.yaml
  • Adds TestNetworkAliases
  • Moves TestNetworkAmbiguity to dockerutils, because it's a proper place to test Docker networks

Manual Testing Instructions

Review updated https://ddev--7642.org.readthedocs.build/en/7642/users/usage/faq/#can-different-projects-communicate-with-each-other

mkdir foo && mkdir bar
echo '<?php echo "foo" . PHP_EOL;' > foo/index.php
echo '<?php echo "bar" . PHP_EOL;' > bar/index.php

cd foo && ddev config --additional-hostnames=one,two --additional-fqdns=foo.example.com && cd ..
cd bar && ddev config --additional-hostnames=three --additional-fqdns=bar.example.ddev.site && cd ..
ddev start foo bar

cd foo
ddev exec curl https://bar.ddev.site
ddev exec curl https://three.ddev.site
ddev exec curl https://bar.example.ddev.site
ddev exec curl https://foo.ddev.site
ddev exec curl https://one.ddev.site
ddev exec curl https://two.ddev.site
ddev exec curl https://foo.example.com

cd ../bar
ddev exec curl https://bar.ddev.site
ddev exec curl https://three.ddev.site
ddev exec curl https://bar.example.ddev.site
ddev exec curl https://foo.ddev.site
ddev exec curl https://one.ddev.site
ddev exec curl https://two.ddev.site
ddev exec curl https://foo.example.com

# check config for ddev-router, which has all these aliases:
cat ~/.ddev/.router-compose-full.yaml

Repeat the same using different router ports:

# For example, change ports in foo:
ddev config --router-http-port 81 --router-https-port 444
# And change global ports:
ddev config global --router-http-port 8080 --router-https-port 8443

Automated Testing Overview

See the new test: TestNetworkAliases

Release/Deployment Notes

StackOverflow answer needs an update.

Bonus: Podman is not compatible with external_links, removing them will fix ddev start on Podman.

@stasadev stasadev changed the title feat: use network aliases instead of external_links feat(docker): use network aliases instead of external_links Sep 19, 2025
@github-actions

github-actions Bot commented Sep 19, 2025 •

Copy link
Copy Markdown

@rfay

rfay commented Sep 19, 2025

Copy link
Copy Markdown
Member

There are two purposes for the external_links:

  • Communication between projects
  • Usages of *.ddev.site - usually for self - inside the web container.

@stasadev

Copy link
Copy Markdown
Member Author

There are two purposes for the external_links:

  • Communication between projects
  • Usages of *.ddev.site - usually for self - inside the web container.

And both of them work with network aliases.

@asacolips

Copy link
Copy Markdown

Confirming that this works for me on MacOS (Arm64) using Podman. All testing instructions completed successfully, and I was also able to run a test Drupal 11 project in Podman as well.

Output from the testing instructions:

❯ cd ../bar
ddev exec curl https://bar.ddev.site
ddev exec curl https://three.ddev.site
ddev exec curl https://bar.example.ddev.site
ddev exec curl https://foo.ddev.site
ddev exec curl https://one.ddev.site
ddev exec curl https://two.ddev.site
ddev exec curl https://foo.example.com
bar
bar
bar
foo
foo
foo
foo

Some notes on my setup:

  • MacBook Pro, using an Apple M1 Pro chip
  • Currently on MacOS Sonoma
  • Podman version 5.6.0
  • Podman settings:
    • Settings -> Preferences -> Docker Compatibility: Enabled
    • Settings -> Docker Compatibility -> Third-Party Docker Tool Compatibility: Enabled
    • Settings -> Docker Compatibility -> Docker Cli: Context: default (unix:///var/run/docker.sock)
  • docker cli installed via homebrew with brew install docker, and docker context set to Podman in the above config ^

The only significant warning I have when running DDEV in Podman now is the following:

Problem with your Docker provider: installed Docker version 5.6.0 is not supported, please update to version 25.0 or newer.

That seems expected since Podman has an entirely different versioning scheme from Docker, and is coming from root.go during the docker version check.

@stasadev

stasadev commented Sep 19, 2025 •

Copy link
Copy Markdown
Member Author

Problem with your Docker provider: installed Docker version 5.6.0 is not supported, please update to version 25.0 or newer.

This warning definitely needs to be fixed, but in a different PR.

@stasadev stasadev changed the title feat(docker): use network aliases instead of external_links feat(docker): auto HTTP/S communication via network aliases, remove external_links Sep 22, 2025
@stasadev
stasadev marked this pull request as ready for review September 22, 2025 17:20
@stasadev
stasadev requested review from a team as code owners September 22, 2025 17:20
@rfay rfay changed the title feat(docker): auto HTTP/S communication via network aliases, remove external_links feat(docker): auto HTTP/S communication via network aliases, remove external_links, Podman? Sep 22, 2025
@rfay rfay mentioned this pull request Sep 22, 2025
12 tasks done
@rfay rfay changed the title feat(docker): auto HTTP/S communication via network aliases, remove external_links, Podman? feat(docker): auto HTTP/S communication via network aliases, remove external_links, Podman Sep 22, 2025
@rfay

rfay commented Sep 22, 2025

Copy link
Copy Markdown
Member

Congrats!

Do you think this change has any impact on:

@rfay rfay changed the title feat(docker): auto HTTP/S communication via network aliases, remove external_links, Podman feat(docker): auto HTTP/S communication via network aliases, remove external_links Sep 22, 2025
@rfay

rfay commented Sep 22, 2025 •

Copy link
Copy Markdown
Member

Just almost looked like it was going to work with podman on macOS.

Container ddev-d11-web Error response from daemon: bad parameter: link is not supported Error response from daemon: bad parameter: link is not supported

brew install podman
brew install podman-desktop
brew install brew slp/krunkit/krunkit

Start podman-desktop (disable k8s)
Set up podman
Allow macOS helper (takes over default docker socket)

Edit: Today I can get it working using Podman Desktop. I couldn't use ports 80/443, as it doesn't (on first blush) know how to use privileged ports.

@rfay
rfay force-pushed the 20250919_stasadev_network_aliases branch from 0510b12 to bb7aae4 Compare September 22, 2025 22:47
@rfay

rfay commented Sep 22, 2025

Copy link
Copy Markdown
Member

Rebased, since we had such big vendor changes.

@rfay rfay left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks pretty great and pretty surgical to me!

Comment thread docs/content/users/usage/faq.md Outdated
Let’s say we have two projects, for example: project A, and project B.

To enable server-side HTTP/S communication (i.e. server-side API calls) between projects you can:
DDEV v1.24.9+ has built-in support for inter-project HTTP/S communication:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No need to make a change now, but this may be getting long-ish for the FAQ, and we may want to find a new home for it and link from FAQ.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good idea, I'll do it here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've updated the docs and included more use cases.

Comment thread docs/content/users/usage/faq.md Outdated

See [Third Party Services May Need To Trust `ddev-webserver`](../extend/custom-compose-files.md#third-party-services-may-need-to-trust-ddev-webserver).

For DDEV v1.24.8 and earlier, to enable server-side HTTP/S communication (i.e. server-side API calls) between projects you can:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IMO we don't have to include this any more. The earlier section says clearly v1.24.9+

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Okay.

Comment thread pkg/ddevapp/router.go Outdated
var routerHostnames []string

for _, app := range GetActiveProjects() {
err := app.ReadDockerComposeYAML()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't see a problem with this, since it only happens when we're reconfiguring the router. But it does seem like a lot of reading on some people's systems.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I thought about this too, but didn't pay much attention.

GetActiveProjects() is called several times in the router, I'll review the logic here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I refactored it to use only one call to GetActiveProjects() in StartDdevRouter().

It looks like we read the config of all projects at least 4 times before.

@rfay

rfay commented Sep 23, 2025

Copy link
Copy Markdown
Member

TestNetworkAliases did fail on WSL2 DD, https://buildkite.com/ddev/wsl2-docker-desktop/builds/6318#0199739c-c38e-48b5-8d00-5aa627ee6965/1011-1195

Most likely incidental problem but I'm sure you'll look closely. I didn't restart.

@stasadev

Copy link
Copy Markdown
Member Author

Do you think this change has any impact on:

I don't think so, this PR is about hostnames, including everything added to the VIRTUAL_HOST.

TestNetworkAliases did fail on WSL2 DD, buildkite.com/ddev/wsl2-docker-desktop/builds/6318#0199739c-c38e-48b5-8d00-5aa627ee6965/1011-1195

Most likely incidental problem but I'm sure you'll look closely. I didn't restart.

I looks like Traefik failed, but I can't say how. I added docker logs ddev-router to the test.

@stasadev stasadev changed the title feat(docker): auto HTTP/S communication via network aliases, remove external_links feat(docker): auto HTTP/S communication via network aliases instead of external_links Sep 23, 2025
@stasadev

Copy link
Copy Markdown
Member Author

I looks like Traefik failed, but I can't say how. I added docker logs ddev-router to the test.

And it didn't fail on the second attempt. I guess we'll know more after this PR lands into main.

@rfay

rfay commented Sep 23, 2025

Copy link
Copy Markdown
Member

Looks like you can pull when you're happy with tests today!

@stasadev
stasadev merged commit 4ccba81 into main Sep 24, 2025
@stasadev
stasadev deleted the 20250919_stasadev_network_aliases branch September 24, 2025 13:03
rfay added a commit to rfay/ddev that referenced this pull request Feb 11, 2026
…8110

## The Issue

- Fixes ddev#8110

When starting a second DDEV project with identical port configuration (e.g., both using ddev-adminer on port 9101), the router optimization from PR ddev#7992 prevented recreation because ports matched. However, network aliases in `.router-compose-full.yaml` (required by PR ddev#7642 for inter-container HTTP/S communication) were not updated. This caused the second project's containers to be unable to access their own services via HTTPS, breaking inter-container communication.

## How This PR Solves The Issue

This PR extends the router recreation logic to check both ports AND hostnames (network aliases), not just ports:

1. **Added `GetRouterNetworkAliases()`** in `pkg/dockerutil/containers.go` - Extracts network aliases from the running router container's `ddev_default` network configuration, mirroring the pattern of `GetBoundHostPorts()`

2. **Added `HostnamesMatch()`** in `pkg/ddevapp/router.go` - Compares existing vs needed hostnames using subset matching (allows extra hostnames from stopped projects), exactly mirroring `PortsMatch()` logic

3. **Modified `StartDdevRouter()`** in `pkg/ddevapp/router.go` - Now checks both ports and hostnames before deciding to skip recreation. The router is recreated when EITHER ports OR hostnames have changed, while preserving the optimization when neither has changed

The fix maintains the performance optimization from PR ddev#7992 (router isn't recreated unnecessarily) while ensuring network aliases are always updated when new projects start.

## Manual Testing Instructions

Test the exact reproduction scenario from issue ddev#8110:

```bash
# Clean slate
ddev poweroff
mkdir -p /tmp/pruebas && cd /tmp/pruebas

# Start first project with adminer
mkdir prueba1 && cd prueba1
ddev config --auto
ddev add-on get ddev/ddev-adminer
DDEV_DEBUG=true ddev start
# Verify works from container
ddev exec curl -I https://prueba1.ddev.site:9101
# Should succeed

# Start second project with same configuration
cd /tmp/pruebas
mkdir prueba2 && cd prueba2
ddev config --auto
ddev add-on get ddev/ddev-adminer
DDEV_DEBUG=true ddev start
# Should see: "Router hostnames have changed, will recreate router"
ddev exec curl -I https://prueba2.ddev.site:9101
# Should succeed (this would fail without the fix)

# Verify both aliases exist in router
docker inspect ddev-router --format '{{json .NetworkSettings.Networks.ddev_default.Aliases}}' | jq
# Should show both prueba1.ddev.site and prueba2.ddev.site

# Verify optimization still works
ddev restart
# Should see: "Router ports and hostnames have not changed, skipping recreation"
```

## Automated Testing Overview

**New Unit Tests:**
- `TestHostnamesMatch()` in `pkg/ddevapp/router_test.go` - 6 test cases verifying hostname comparison logic mirrors `TestPortsMatch()` behavior (empty slices, identical, different order, missing hostname, extra hostnames)
- `TestGetRouterNetworkAliases()` in `pkg/dockerutil/containers_test.go` - Verifies alias extraction from containers, error handling

**Integration Tests:**
- Existing `TestNetworkAliases()` in `pkg/dockerutil/networks_test.go` will verify the fix works end-to-end (can be enhanced with same-port scenario)

All tests pass. golangci-lint passes with 0 issues.

## Release/Deployment Notes

- **Backward compatible:** No breaking changes, existing behavior preserved
- **Performance:** Minimal impact - one additional Docker API call (`ContainerInspect` for network aliases) when router already exists, same O(n+m) complexity as port comparison
- **User impact:** Fixes broken inter-container communication for users with multiple projects using identical port configurations (common with add-ons)
- **No config changes required:** Fix works automatically on upgrade

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
rfay added a commit to rfay/ddev that referenced this pull request Feb 12, 2026
…8110

## The Issue

- Fixes ddev#8110

When starting a second DDEV project with identical port configuration (e.g., both using ddev-adminer on port 9101), the router optimization from PR ddev#7992 prevented recreation because ports matched. However, network aliases in `.router-compose-full.yaml` (required by PR ddev#7642 for inter-container HTTP/S communication) were not updated. This caused the second project's containers to be unable to access their own services via HTTPS, breaking inter-container communication.

## How This PR Solves The Issue

This PR extends the router recreation logic to check both ports AND hostnames (network aliases), not just ports:

1. **Added `GetRouterNetworkAliases()`** in `pkg/dockerutil/containers.go` - Extracts network aliases from the running router container's `ddev_default` network configuration, mirroring the pattern of `GetBoundHostPorts()`

2. **Added `HostnamesMatch()`** in `pkg/ddevapp/router.go` - Compares existing vs needed hostnames using subset matching (allows extra hostnames from stopped projects), exactly mirroring `PortsMatch()` logic

3. **Modified `StartDdevRouter()`** in `pkg/ddevapp/router.go` - Now checks both ports and hostnames before deciding to skip recreation. The router is recreated when EITHER ports OR hostnames have changed, while preserving the optimization when neither has changed

The fix maintains the performance optimization from PR ddev#7992 (router isn't recreated unnecessarily) while ensuring network aliases are always updated when new projects start.

## Manual Testing Instructions

Test the exact reproduction scenario from issue ddev#8110:

```bash
# Clean slate
ddev poweroff
mkdir -p /tmp/pruebas && cd /tmp/pruebas

# Start first project with adminer
mkdir prueba1 && cd prueba1
ddev config --auto
ddev add-on get ddev/ddev-adminer
DDEV_DEBUG=true ddev start
# Verify works from container
ddev exec curl -I https://prueba1.ddev.site:9101
# Should succeed

# Start second project with same configuration
cd /tmp/pruebas
mkdir prueba2 && cd prueba2
ddev config --auto
ddev add-on get ddev/ddev-adminer
DDEV_DEBUG=true ddev start
# Should see: "Router hostnames have changed, will recreate router"
ddev exec curl -I https://prueba2.ddev.site:9101
# Should succeed (this would fail without the fix)

# Verify both aliases exist in router
docker inspect ddev-router --format '{{json .NetworkSettings.Networks.ddev_default.Aliases}}' | jq
# Should show both prueba1.ddev.site and prueba2.ddev.site

# Verify optimization still works
ddev restart
# Should see: "Router ports and hostnames have not changed, skipping recreation"
```

## Automated Testing Overview

**New Unit Tests:**
- `TestHostnamesMatch()` in `pkg/ddevapp/router_test.go` - 6 test cases verifying hostname comparison logic mirrors `TestPortsMatch()` behavior (empty slices, identical, different order, missing hostname, extra hostnames)
- `TestGetRouterNetworkAliases()` in `pkg/dockerutil/containers_test.go` - Verifies alias extraction from containers, error handling

**Integration Tests:**
- Existing `TestNetworkAliases()` in `pkg/dockerutil/networks_test.go` will verify the fix works end-to-end (can be enhanced with same-port scenario)

All tests pass. golangci-lint passes with 0 issues.

## Release/Deployment Notes

- **Backward compatible:** No breaking changes, existing behavior preserved
- **Performance:** Minimal impact - one additional Docker API call (`ContainerInspect` for network aliases) when router already exists, same O(n+m) complexity as port comparison
- **User impact:** Fixes broken inter-container communication for users with multiple projects using identical port configurations (common with add-ons)
- **No config changes required:** Fix works automatically on upgrade

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
rfay added a commit to rfay/ddev that referenced this pull request Feb 17, 2026
…8110

## The Issue

- Fixes ddev#8110

When starting a second DDEV project with identical port configuration (e.g., both using ddev-adminer on port 9101), the router optimization from PR ddev#7992 prevented recreation because ports matched. However, network aliases in `.router-compose-full.yaml` (required by PR ddev#7642 for inter-container HTTP/S communication) were not updated. This caused the second project's containers to be unable to access their own services via HTTPS, breaking inter-container communication.

## How This PR Solves The Issue

This PR extends the router recreation logic to check both ports AND hostnames (network aliases), not just ports:

1. **Added `GetRouterNetworkAliases()`** in `pkg/dockerutil/containers.go` - Extracts network aliases from the running router container's `ddev_default` network configuration, mirroring the pattern of `GetBoundHostPorts()`

2. **Added `HostnamesMatch()`** in `pkg/ddevapp/router.go` - Compares existing vs needed hostnames using subset matching (allows extra hostnames from stopped projects), exactly mirroring `PortsMatch()` logic

3. **Modified `StartDdevRouter()`** in `pkg/ddevapp/router.go` - Now checks both ports and hostnames before deciding to skip recreation. The router is recreated when EITHER ports OR hostnames have changed, while preserving the optimization when neither has changed

The fix maintains the performance optimization from PR ddev#7992 (router isn't recreated unnecessarily) while ensuring network aliases are always updated when new projects start.

## Manual Testing Instructions

Test the exact reproduction scenario from issue ddev#8110:

```bash
# Clean slate
ddev poweroff
mkdir -p /tmp/pruebas && cd /tmp/pruebas

# Start first project with adminer
mkdir prueba1 && cd prueba1
ddev config --auto
ddev add-on get ddev/ddev-adminer
DDEV_DEBUG=true ddev start
# Verify works from container
ddev exec curl -I https://prueba1.ddev.site:9101
# Should succeed

# Start second project with same configuration
cd /tmp/pruebas
mkdir prueba2 && cd prueba2
ddev config --auto
ddev add-on get ddev/ddev-adminer
DDEV_DEBUG=true ddev start
# Should see: "Router hostnames have changed, will recreate router"
ddev exec curl -I https://prueba2.ddev.site:9101
# Should succeed (this would fail without the fix)

# Verify both aliases exist in router
docker inspect ddev-router --format '{{json .NetworkSettings.Networks.ddev_default.Aliases}}' | jq
# Should show both prueba1.ddev.site and prueba2.ddev.site

# Verify optimization still works
ddev restart
# Should see: "Router ports and hostnames have not changed, skipping recreation"
```

## Automated Testing Overview

**New Unit Tests:**
- `TestHostnamesMatch()` in `pkg/ddevapp/router_test.go` - 6 test cases verifying hostname comparison logic mirrors `TestPortsMatch()` behavior (empty slices, identical, different order, missing hostname, extra hostnames)
- `TestGetRouterNetworkAliases()` in `pkg/dockerutil/containers_test.go` - Verifies alias extraction from containers, error handling

**Integration Tests:**
- Existing `TestNetworkAliases()` in `pkg/dockerutil/networks_test.go` will verify the fix works end-to-end (can be enhanced with same-port scenario)

All tests pass. golangci-lint passes with 0 issues.

## Release/Deployment Notes

- **Backward compatible:** No breaking changes, existing behavior preserved
- **Performance:** Minimal impact - one additional Docker API call (`ContainerInspect` for network aliases) when router already exists, same O(n+m) complexity as port comparison
- **User impact:** Fixes broken inter-container communication for users with multiple projects using identical port configurations (common with add-ons)
- **No config changes required:** Fix works automatically on upgrade

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
rfay added a commit to rfay/ddev that referenced this pull request Feb 18, 2026
…8110

## The Issue

- Fixes ddev#8110

When starting a second DDEV project with identical port configuration (e.g., both using ddev-adminer on port 9101), the router optimization from PR ddev#7992 prevented recreation because ports matched. However, network aliases in `.router-compose-full.yaml` (required by PR ddev#7642 for inter-container HTTP/S communication) were not updated. This caused the second project's containers to be unable to access their own services via HTTPS, breaking inter-container communication.

## How This PR Solves The Issue

This PR extends the router recreation logic to check both ports AND hostnames (network aliases), not just ports:

1. **Added `GetRouterNetworkAliases()`** in `pkg/dockerutil/containers.go` - Extracts network aliases from the running router container's `ddev_default` network configuration, mirroring the pattern of `GetBoundHostPorts()`

2. **Added `HostnamesMatch()`** in `pkg/ddevapp/router.go` - Compares existing vs needed hostnames using subset matching (allows extra hostnames from stopped projects), exactly mirroring `PortsMatch()` logic

3. **Modified `StartDdevRouter()`** in `pkg/ddevapp/router.go` - Now checks both ports and hostnames before deciding to skip recreation. The router is recreated when EITHER ports OR hostnames have changed, while preserving the optimization when neither has changed

The fix maintains the performance optimization from PR ddev#7992 (router isn't recreated unnecessarily) while ensuring network aliases are always updated when new projects start.

## Manual Testing Instructions

Test the exact reproduction scenario from issue ddev#8110:

```bash
# Clean slate
ddev poweroff
mkdir -p /tmp/pruebas && cd /tmp/pruebas

# Start first project with adminer
mkdir prueba1 && cd prueba1
ddev config --auto
ddev add-on get ddev/ddev-adminer
DDEV_DEBUG=true ddev start
# Verify works from container
ddev exec curl -I https://prueba1.ddev.site:9101
# Should succeed

# Start second project with same configuration
cd /tmp/pruebas
mkdir prueba2 && cd prueba2
ddev config --auto
ddev add-on get ddev/ddev-adminer
DDEV_DEBUG=true ddev start
# Should see: "Router hostnames have changed, will recreate router"
ddev exec curl -I https://prueba2.ddev.site:9101
# Should succeed (this would fail without the fix)

# Verify both aliases exist in router
docker inspect ddev-router --format '{{json .NetworkSettings.Networks.ddev_default.Aliases}}' | jq
# Should show both prueba1.ddev.site and prueba2.ddev.site

# Verify optimization still works
ddev restart
# Should see: "Router ports and hostnames have not changed, skipping recreation"
```

## Automated Testing Overview

**New Unit Tests:**
- `TestHostnamesMatch()` in `pkg/ddevapp/router_test.go` - 6 test cases verifying hostname comparison logic mirrors `TestPortsMatch()` behavior (empty slices, identical, different order, missing hostname, extra hostnames)
- `TestGetRouterNetworkAliases()` in `pkg/dockerutil/containers_test.go` - Verifies alias extraction from containers, error handling

**Integration Tests:**
- Existing `TestNetworkAliases()` in `pkg/dockerutil/networks_test.go` will verify the fix works end-to-end (can be enhanced with same-port scenario)

All tests pass. golangci-lint passes with 0 issues.

## Release/Deployment Notes

- **Backward compatible:** No breaking changes, existing behavior preserved
- **Performance:** Minimal impact - one additional Docker API call (`ContainerInspect` for network aliases) when router already exists, same O(n+m) complexity as port comparison
- **User impact:** Fixes broken inter-container communication for users with multiple projects using identical port configurations (common with add-ons)
- **No config changes required:** Fix works automatically on upgrade

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants