Skip to content

fix(router): recreate router when network aliases change, fixes #8110 - #8132

Merged
rfay merged 4 commits into
ddev:mainfrom
rfay:20260211_rfay_network_aliases
Feb 19, 2026
Merged

rfay merged 4 commits into
ddev:mainfrom
rfay:20260211_rfay_network_aliases

Conversation

@rfay

@rfay rfay commented Feb 11, 2026 •

Copy link
Copy Markdown
Member

The Issue

When starting a second DDEV project with identical port configuration (even just generic projects show it), the router optimization from PR #7992 prevented recreation because ports matched. However, network aliases in .router-compose-full.yaml (required by PR #7642 for inter-project 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 #7992 (router isn't recreated unnecessarily) while ensuring network aliases are always updated when new projects start.

Manual Testing Instructions

Test a variant of the excellent reproduction scenario from issue #8110:

# Clean slate
ddev poweroff
mkdir -p ~/tmp/t1 ~/tmp/t2

# First project starts
cd ~/tmp/t1 && ddev config --auto && ddev start
printf '<?php\nprint "this is t1\n'; >index.php

# Second project starts
cd ~/tmp/t2 && ddev config --auto && ddev start
printf '<?php\nprint "BUT this is t2\n'; >index.php
# This should result in router recreation because the new network alias has to be added

# t1 should be able to access t2 by URL
cd ~/tmp/t1
ddev exec curl -I https://t2.ddev.site


docker inspect ddev-router --format '{{json .NetworkSettings.Networks.ddev_default.Aliases}}' | jq
# Should show both t1.ddev.site and t2.ddev.site

ddev restart t1 t2
# No router recreation should happen

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.

@github-actions

github-actions Bot commented Feb 11, 2026 •

Copy link
Copy Markdown

rfay added a commit to rfay/ddev that referenced this pull request Feb 12, 2026
Enhance TestNetworkAliases to properly test issue ddev#8110 by configuring both
test projects with identical port configurations (80/443). Previously, app1
used custom ports (8080/8443) while app2 used default ports (80/443), which
meant the router would be recreated due to port differences alone.

Changes:
- Both projects now use the same ports (80/443) to test the scenario where
  ports match but hostnames differ
- Updated test cases to use GetHTTPURL()/GetHTTPSURL() methods for project
  name tests instead of manually constructing URLs
- Added new subtest "router_recreated_for_new_hostname" that specifically
  validates the issue ddev#8110 fix by verifying both projects can communicate
  via HTTPS through the router and both hostnames exist as network aliases

This ensures the test validates that the router is recreated when hostnames
change even if ports remain the same, which is the core fix from PR ddev#8132.

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
rfay added a commit to rfay/ddev that referenced this pull request Feb 12, 2026
Enhance TestNetworkAliases to properly test issue ddev#8110 by configuring both
test projects with identical port configurations (80/443). Previously, app1
used custom ports (8080/8443) while app2 used default ports (80/443), which
meant the router would be recreated due to port differences alone.

Changes:
- Both projects now use the same ports (80/443) to test the scenario where
  ports match but hostnames differ
- Updated test cases to use GetHTTPURL()/GetHTTPSURL() methods for project
  name tests instead of manually constructing URLs
- Added new subtest "router_recreated_for_new_hostname" that specifically
  validates the issue ddev#8110 fix by verifying both projects can communicate
  via HTTPS through the router and both hostnames exist as network aliases

This ensures the test validates that the router is recreated when hostnames
change even if ports remain the same, which is the core fix from PR ddev#8132.

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
@rfay
rfay force-pushed the 20260211_rfay_network_aliases branch from d713407 to ca0ae4f Compare February 12, 2026 19:17
@rfay
rfay marked this pull request as ready for review February 13, 2026 18:05
@rfay
rfay requested a review from a team as a code owner February 13, 2026 18:05
@rfay
rfay requested a review from stasadev February 13, 2026 18:05
rfay added a commit to rfay/ddev that referenced this pull request Feb 17, 2026
Enhance TestNetworkAliases to properly test issue ddev#8110 by configuring both
test projects with identical port configurations (80/443). Previously, app1
used custom ports (8080/8443) while app2 used default ports (80/443), which
meant the router would be recreated due to port differences alone.

Changes:
- Both projects now use the same ports (80/443) to test the scenario where
  ports match but hostnames differ
- Updated test cases to use GetHTTPURL()/GetHTTPSURL() methods for project
  name tests instead of manually constructing URLs
- Added new subtest "router_recreated_for_new_hostname" that specifically
  validates the issue ddev#8110 fix by verifying both projects can communicate
  via HTTPS through the router and both hostnames exist as network aliases

This ensures the test validates that the router is recreated when hostnames
change even if ports remain the same, which is the core fix from PR ddev#8132.

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
@rfay
rfay force-pushed the 20260211_rfay_network_aliases branch from ca0ae4f to c84106f Compare February 17, 2026 03:17

@stasadev stasadev 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 good to me and works fine.

We still need to test TestNetworkAliases with different ports, as we did before. I remember this test failed for me when I added aliases at the project level instead of in ddev-router. It is still important to make sure communication on different ports works as expected.

Maybe we should just duplicate the test as TestNetworkAliasesDifferentPorts?

rfay and others added 4 commits February 18, 2026 11:43
…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]>
Enhance TestNetworkAliases to properly test issue ddev#8110 by configuring both
test projects with identical port configurations (80/443). Previously, app1
used custom ports (8080/8443) while app2 used default ports (80/443), which
meant the router would be recreated due to port differences alone.

Changes:
- Both projects now use the same ports (80/443) to test the scenario where
  ports match but hostnames differ
- Updated test cases to use GetHTTPURL()/GetHTTPSURL() methods for project
  name tests instead of manually constructing URLs
- Added new subtest "router_recreated_for_new_hostname" that specifically
  validates the issue ddev#8110 fix by verifying both projects can communicate
  via HTTPS through the router and both hostnames exist as network aliases

This ensures the test validates that the router is recreated when hostnames
change even if ports remain the same, which is the core fix from PR ddev#8132.

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
…elper

- Extract runNetworkAliasesTest helper shared by both test functions
- TestNetworkAliases keeps same-port scenario (80/443) for ddev#8110 fix coverage
- Add TestNetworkAliasesDifferentPorts with app1 on 8080/8443, app2 on 80/443
- Skip entire test early via app.CanUseHTTPOnly() instead of conditionally
  excluding HTTPS URLs; always test both HTTP and HTTPS
- Use strings.Replace(app.GetHTTPURL(), ...) for additional hostname/FQDN
  URLs so port handling comes from the existing app methods

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@rfay
rfay force-pushed the 20260211_rfay_network_aliases branch from c84106f to 66faa14 Compare February 18, 2026 18:44
@rfay

rfay commented Feb 18, 2026

Copy link
Copy Markdown
Member Author

Rebased and added TestNetworkAliasesDifferentPorts

@stasadev stasadev 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.

Test refactoring looks great, thank you!

@rfay
rfay merged commit c4a9c92 into ddev:main Feb 19, 2026
29 checks passed
@rfay
rfay deleted the 20260211_rfay_network_aliases branch February 19, 2026 00:32
stasadev added a commit to stasadev/ddev that referenced this pull request Jul 14, 2026
…n start

## The Issue

- Follow-up to ddev#8132, which fixed ddev#8110

PR ddev#8132 made the router recreate itself whenever project hostnames
(network aliases) changed, so inter-project HTTP/S communication kept
working. That fixed correctness, but it also meant that starting an
additional project (`ddev start`) recreated the whole router even though
its published ports were unchanged. Router recreation is disruptive and
slower than necessary for the common "start another project" case.

## How This PR Solves The Issue

Recreation is now reserved for the cases that actually require it, and
hostname-only changes are handled without recreating the container:

1. **Added `UpdateContainerNetworkAliases()`** in
   `pkg/dockerutil/containers.go` - Docker only lets network aliases be
   set at connect time, so this disconnects and reconnects the router on
   the `ddev_default` network with the new alias set. No container
   recreation.

2. **Reworked the recreation decision** in `StartDdevRouter()`
   (`pkg/ddevapp/router.go`) - The router is recreated only when it is
   not running, its state can't be read, or its published ports changed
   (published ports can't be changed on a live container). A
   hostname-only change no longer triggers recreation.

3. **Update aliases in place when already running** - When the router is
   already running and only hostnames changed, the network aliases are
   refreshed in place via `UpdateContainerNetworkAliases()` before the
   Traefik config is pushed.

The correctness guarantee from ddev#8132 is preserved (aliases are always
current), while the common case of starting an extra project no longer
pays for a full router recreation.

## Manual Testing Instructions

```bash
# Clean slate
ddev poweroff
mkdir -p ~/tmp/t1 ~/tmp/t2

# First project starts (router is created)
cd ~/tmp/t1 && ddev config --auto && ddev start
printf '<?php\nprint "this is t1\n";' >index.php

# Second project starts: router is NOT recreated, aliases updated in place
cd ~/tmp/t2 && ddev config --auto && ddev start
printf '<?php\nprint "BUT this is t2\n";' >index.php

# Aliases include both projects
docker inspect ddev-router --format '{{json .NetworkSettings.Networks.ddev_default.Aliases}}' | jq
# Should show both t1.ddev.site and t2.ddev.site

# Inter-project communication still works
cd ~/tmp/t1
ddev exec curl -I https://t2.ddev.site

# Router container id stays the same across the second start
# (compare `docker inspect ddev-router --format '{{.Id}}'` before/after)
```

## Automated Testing Overview

Existing `TestGetRouterNetworkAliases()` (`pkg/dockerutil`) and
`TestHostnamesMatch()` (`pkg/ddevapp`) continue to cover alias extraction
and comparison. `TestNetworkAliases()` in
`pkg/dockerutil/networks_test.go` exercises the disconnect/reconnect alias
update path end-to-end.

## Release/Deployment Notes

No user-facing configuration changes. Starting additional projects is
faster because the router is no longer recreated for hostname-only
changes; recreation still happens when published ports change or the
router is not running.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
rfay pushed a commit to stasadev/ddev that referenced this pull request Jul 17, 2026
…n start

## The Issue

- Follow-up to ddev#8132, which fixed ddev#8110

PR ddev#8132 made the router recreate itself whenever project hostnames
(network aliases) changed, so inter-project HTTP/S communication kept
working. That fixed correctness, but it also meant that starting an
additional project (`ddev start`) recreated the whole router even though
its published ports were unchanged. Router recreation is disruptive and
slower than necessary for the common "start another project" case.

## How This PR Solves The Issue

Recreation is now reserved for the cases that actually require it, and
hostname-only changes are handled without recreating the container:

1. **Added `UpdateContainerNetworkAliases()`** in
   `pkg/dockerutil/containers.go` - Docker only lets network aliases be
   set at connect time, so this disconnects and reconnects the router on
   the `ddev_default` network with the new alias set. No container
   recreation.

2. **Reworked the recreation decision** in `StartDdevRouter()`
   (`pkg/ddevapp/router.go`) - The router is recreated only when it is
   not running, its state can't be read, or its published ports changed
   (published ports can't be changed on a live container). A
   hostname-only change no longer triggers recreation.

3. **Update aliases in place when already running** - When the router is
   already running and only hostnames changed, the network aliases are
   refreshed in place via `UpdateContainerNetworkAliases()` before the
   Traefik config is pushed.

The correctness guarantee from ddev#8132 is preserved (aliases are always
current), while the common case of starting an extra project no longer
pays for a full router recreation.

## Manual Testing Instructions

```bash
# Clean slate
ddev poweroff
mkdir -p ~/tmp/t1 ~/tmp/t2

# First project starts (router is created)
cd ~/tmp/t1 && ddev config --auto && ddev start
printf '<?php\nprint "this is t1\n";' >index.php

# Second project starts: router is NOT recreated, aliases updated in place
cd ~/tmp/t2 && ddev config --auto && ddev start
printf '<?php\nprint "BUT this is t2\n";' >index.php

# Aliases include both projects
docker inspect ddev-router --format '{{json .NetworkSettings.Networks.ddev_default.Aliases}}' | jq
# Should show both t1.ddev.site and t2.ddev.site

# Inter-project communication still works
cd ~/tmp/t1
ddev exec curl -I https://t2.ddev.site

# Router container id stays the same across the second start
# (compare `docker inspect ddev-router --format '{{.Id}}'` before/after)
```

## Automated Testing Overview

Existing `TestGetRouterNetworkAliases()` (`pkg/dockerutil`) and
`TestHostnamesMatch()` (`pkg/ddevapp`) continue to cover alias extraction
and comparison. `TestNetworkAliases()` in
`pkg/dockerutil/networks_test.go` exercises the disconnect/reconnect alias
update path end-to-end.

## Release/Deployment Notes

No user-facing configuration changes. Starting additional projects is
faster because the router is no longer recreated for hostname-only
changes; recreation still happens when published ports change or the
router is not running.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
stasadev added a commit to stasadev/ddev that referenced this pull request Jul 17, 2026
…n start

## The Issue

- Follow-up to ddev#8132, which fixed ddev#8110

PR ddev#8132 made the router recreate itself whenever project hostnames
(network aliases) changed, so inter-project HTTP/S communication kept
working. That fixed correctness, but it also meant that starting an
additional project (`ddev start`) recreated the whole router even though
its published ports were unchanged. Router recreation is disruptive and
slower than necessary for the common "start another project" case.

## How This PR Solves The Issue

Recreation is now reserved for the cases that actually require it, and
hostname-only changes are handled without recreating the container:

1. **Added `UpdateContainerNetworkAliases()`** in
   `pkg/dockerutil/containers.go` - Docker only lets network aliases be
   set at connect time, so this disconnects and reconnects the router on
   the `ddev_default` network with the new alias set. No container
   recreation.

2. **Reworked the recreation decision** in `StartDdevRouter()`
   (`pkg/ddevapp/router.go`) - The router is recreated only when it is
   not running, its state can't be read, or its published ports changed
   (published ports can't be changed on a live container). A
   hostname-only change no longer triggers recreation.

3. **Update aliases in place when already running** - When the router is
   already running and only hostnames changed, the network aliases are
   refreshed in place via `UpdateContainerNetworkAliases()` before the
   Traefik config is pushed.

The correctness guarantee from ddev#8132 is preserved (aliases are always
current), while the common case of starting an extra project no longer
pays for a full router recreation.

## Manual Testing Instructions

```bash
# Clean slate
ddev poweroff
mkdir -p ~/tmp/t1 ~/tmp/t2

# First project starts (router is created)
cd ~/tmp/t1 && ddev config --auto && ddev start
printf '<?php\nprint "this is t1\n";' >index.php

# Second project starts: router is NOT recreated, aliases updated in place
cd ~/tmp/t2 && ddev config --auto && ddev start
printf '<?php\nprint "BUT this is t2\n";' >index.php

# Aliases include both projects
docker inspect ddev-router --format '{{json .NetworkSettings.Networks.ddev_default.Aliases}}' | jq
# Should show both t1.ddev.site and t2.ddev.site

# Inter-project communication still works
cd ~/tmp/t1
ddev exec curl -I https://t2.ddev.site

# Router container id stays the same across the second start
# (compare `docker inspect ddev-router --format '{{.Id}}'` before/after)
```

## Automated Testing Overview

Existing `TestGetRouterNetworkAliases()` (`pkg/dockerutil`) and
`TestHostnamesMatch()` (`pkg/ddevapp`) continue to cover alias extraction
and comparison. `TestNetworkAliases()` in
`pkg/dockerutil/networks_test.go` exercises the disconnect/reconnect alias
update path end-to-end.

## Release/Deployment Notes

No user-facing configuration changes. Starting additional projects is
faster because the router is no longer recreated for hostname-only
changes; recreation still happens when published ports change or the
router is not running.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
stasadev added a commit to stasadev/ddev that referenced this pull request Jul 17, 2026
…n start

## The Issue

- Follow-up to ddev#8132, which fixed ddev#8110

PR ddev#8132 made the router recreate itself whenever project hostnames
(network aliases) changed, so inter-project HTTP/S communication kept
working. That fixed correctness, but it also meant that starting an
additional project (`ddev start`) recreated the whole router even though
its published ports were unchanged. Router recreation is disruptive and
slower than necessary for the common "start another project" case.

## How This PR Solves The Issue

Recreation is now reserved for the cases that actually require it, and
hostname-only changes are handled without recreating the container:

1. **Added `UpdateContainerNetworkAliases()`** in
   `pkg/dockerutil/containers.go` - Docker only lets network aliases be
   set at connect time, so this disconnects and reconnects the router on
   the `ddev_default` network with the new alias set. No container
   recreation.

2. **Reworked the recreation decision** in `StartDdevRouter()`
   (`pkg/ddevapp/router.go`) - The router is recreated only when it is
   not running, its state can't be read, or its published ports changed
   (published ports can't be changed on a live container). A
   hostname-only change no longer triggers recreation.

3. **Update aliases in place when already running** - When the router is
   already running and only hostnames changed, the network aliases are
   refreshed in place via `UpdateContainerNetworkAliases()` before the
   Traefik config is pushed.

The correctness guarantee from ddev#8132 is preserved (aliases are always
current), while the common case of starting an extra project no longer
pays for a full router recreation.

## Manual Testing Instructions

```bash
# Clean slate
ddev poweroff
mkdir -p ~/tmp/t1 ~/tmp/t2

# First project starts (router is created)
cd ~/tmp/t1 && ddev config --auto && ddev start
printf '<?php\nprint "this is t1\n";' >index.php

# Second project starts: router is NOT recreated, aliases updated in place
cd ~/tmp/t2 && ddev config --auto && ddev start
printf '<?php\nprint "BUT this is t2\n";' >index.php

# Aliases include both projects
docker inspect ddev-router --format '{{json .NetworkSettings.Networks.ddev_default.Aliases}}' | jq
# Should show both t1.ddev.site and t2.ddev.site

# Inter-project communication still works
cd ~/tmp/t1
ddev exec curl -I https://t2.ddev.site

# Router container id stays the same across the second start
# (compare `docker inspect ddev-router --format '{{.Id}}'` before/after)
```

## Automated Testing Overview

Existing `TestGetRouterNetworkAliases()` (`pkg/dockerutil`) and
`TestHostnamesMatch()` (`pkg/ddevapp`) continue to cover alias extraction
and comparison. `TestNetworkAliases()` in
`pkg/dockerutil/networks_test.go` exercises the disconnect/reconnect alias
update path end-to-end.

## Release/Deployment Notes

No user-facing configuration changes. Starting additional projects is
faster because the router is no longer recreated for hostname-only
changes; recreation still happens when published ports change or the
router is not running.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
stasadev added a commit to stasadev/ddev that referenced this pull request Jul 18, 2026
…n start

## The Issue

- Follow-up to ddev#8132, which fixed ddev#8110

PR ddev#8132 made the router recreate itself whenever project hostnames
(network aliases) changed, so inter-project HTTP/S communication kept
working. That fixed correctness, but it also meant that starting an
additional project (`ddev start`) recreated the whole router even though
its published ports were unchanged. Router recreation is disruptive and
slower than necessary for the common "start another project" case.

## How This PR Solves The Issue

Recreation is now reserved for the cases that actually require it, and
hostname-only changes are handled without recreating the container:

1. **Added `UpdateContainerNetworkAliases()`** in
   `pkg/dockerutil/containers.go` - Docker only lets network aliases be
   set at connect time, so this disconnects and reconnects the router on
   the `ddev_default` network with the new alias set. No container
   recreation.

2. **Reworked the recreation decision** in `StartDdevRouter()`
   (`pkg/ddevapp/router.go`) - The router is recreated only when it is
   not running, its state can't be read, or its published ports changed
   (published ports can't be changed on a live container). A
   hostname-only change no longer triggers recreation.

3. **Update aliases in place when already running** - When the router is
   already running and only hostnames changed, the network aliases are
   refreshed in place via `UpdateContainerNetworkAliases()` before the
   Traefik config is pushed.

The correctness guarantee from ddev#8132 is preserved (aliases are always
current), while the common case of starting an extra project no longer
pays for a full router recreation.

## Manual Testing Instructions

```bash
# Clean slate
ddev poweroff
mkdir -p ~/tmp/t1 ~/tmp/t2

# First project starts (router is created)
cd ~/tmp/t1 && ddev config --auto && ddev start
printf '<?php\nprint "this is t1\n";' >index.php

# Second project starts: router is NOT recreated, aliases updated in place
cd ~/tmp/t2 && ddev config --auto && ddev start
printf '<?php\nprint "BUT this is t2\n";' >index.php

# Aliases include both projects
docker inspect ddev-router --format '{{json .NetworkSettings.Networks.ddev_default.Aliases}}' | jq
# Should show both t1.ddev.site and t2.ddev.site

# Inter-project communication still works
cd ~/tmp/t1
ddev exec curl -I https://t2.ddev.site

# Router container id stays the same across the second start
# (compare `docker inspect ddev-router --format '{{.Id}}'` before/after)
```

## Automated Testing Overview

Existing `TestGetRouterNetworkAliases()` (`pkg/dockerutil`) and
`TestHostnamesMatch()` (`pkg/ddevapp`) continue to cover alias extraction
and comparison. `TestNetworkAliases()` in
`pkg/dockerutil/networks_test.go` exercises the disconnect/reconnect alias
update path end-to-end.

## Release/Deployment Notes

No user-facing configuration changes. Starting additional projects is
faster because the router is no longer recreated for hostname-only
changes; recreation still happens when published ports change or the
router is not running.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
stasadev added a commit to stasadev/ddev that referenced this pull request Jul 29, 2026
…n start

## The Issue

- Follow-up to ddev#8132, which fixed ddev#8110

PR ddev#8132 made the router recreate itself whenever project hostnames
(network aliases) changed, so inter-project HTTP/S communication kept
working. That fixed correctness, but it also meant that starting an
additional project (`ddev start`) recreated the whole router even though
its published ports were unchanged. Router recreation is disruptive and
slower than necessary for the common "start another project" case.

## How This PR Solves The Issue

Recreation is now reserved for the cases that actually require it, and
hostname-only changes are handled without recreating the container:

1. **Added `UpdateContainerNetworkAliases()`** in
   `pkg/dockerutil/containers.go` - Docker only lets network aliases be
   set at connect time, so this disconnects and reconnects the router on
   the `ddev_default` network with the new alias set. No container
   recreation.

2. **Reworked the recreation decision** in `StartDdevRouter()`
   (`pkg/ddevapp/router.go`) - The router is recreated only when it is
   not running, its state can't be read, or its published ports changed
   (published ports can't be changed on a live container). A
   hostname-only change no longer triggers recreation.

3. **Update aliases in place when already running** - When the router is
   already running and only hostnames changed, the network aliases are
   refreshed in place via `UpdateContainerNetworkAliases()` before the
   Traefik config is pushed.

The correctness guarantee from ddev#8132 is preserved (aliases are always
current), while the common case of starting an extra project no longer
pays for a full router recreation.

## Manual Testing Instructions

```bash
# Clean slate
ddev poweroff
mkdir -p ~/tmp/t1 ~/tmp/t2

# First project starts (router is created)
cd ~/tmp/t1 && ddev config --auto && ddev start
printf '<?php\nprint "this is t1\n";' >index.php

# Second project starts: router is NOT recreated, aliases updated in place
cd ~/tmp/t2 && ddev config --auto && ddev start
printf '<?php\nprint "BUT this is t2\n";' >index.php

# Aliases include both projects
docker inspect ddev-router --format '{{json .NetworkSettings.Networks.ddev_default.Aliases}}' | jq
# Should show both t1.ddev.site and t2.ddev.site

# Inter-project communication still works
cd ~/tmp/t1
ddev exec curl -I https://t2.ddev.site

# Router container id stays the same across the second start
# (compare `docker inspect ddev-router --format '{{.Id}}'` before/after)
```

## Automated Testing Overview

Existing `TestGetRouterNetworkAliases()` (`pkg/dockerutil`) and
`TestHostnamesMatch()` (`pkg/ddevapp`) continue to cover alias extraction
and comparison. `TestNetworkAliases()` in
`pkg/dockerutil/networks_test.go` exercises the disconnect/reconnect alias
update path end-to-end.

## Release/Deployment Notes

No user-facing configuration changes. Starting additional projects is
faster because the router is no longer recreated for hostname-only
changes; recreation still happens when published ports change or the
router is not running.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
rfay added a commit that referenced this pull request Jul 29, 2026
…n start, for #8132 (#8561) [skip ci]

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: Randy Fay <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v1.25.0: communication between containers by HTTP(S) doesn't work in some cases, ddev-router doesn't update network aliases

2 participants