Skip to content

perf(router): update network aliases in place instead of recreating on start, for #8132 (#8561) [skip ci] - #8561

Merged
rfay merged 4 commits into
ddev:mainfrom
stasadev:20260706_stasadev_dns
Jul 29, 2026
Merged

rfay merged 4 commits into
ddev:mainfrom
stasadev:20260706_stasadev_dns

Conversation

@stasadev

@stasadev stasadev commented Jul 6, 2026 •

Copy link
Copy Markdown
Member

The Issue

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

How This PR Solves The Issue

This PR preserves the correctness guarantee introduced in #8132 while limiting router recreation to the cases where it is actually required. Hostname-only changes are now handled without recreating the router container.

A new UpdateContainerNetworkAliases() function was added to pkg/dockerutil/containers.go. Since Docker only allows network aliases to be assigned when a container connects to a network, this function updates the aliases by disconnecting and reconnecting the router to the ddev_default network with the new alias set, avoiding container recreation.

The recreation logic in StartDdevRouter() (pkg/ddevapp/router.go) was also reworked. The router is now recreated only when it is not running, its state cannot be determined, or its published ports have changed, since published ports cannot be modified on a running container. Changes to hostnames alone no longer trigger recreation.

When the router is already running and only the hostname list has changed, its network aliases are updated in place using UpdateContainerNetworkAliases() before the Traefik configuration is pushed.

As a result, the correctness guarantee from #8132 is preserved because the aliases are always kept up to date, while the common case of starting an additional project no longer requires a full router recreation.

Manual Testing Instructions

# 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

New test TestRouterNotRebuiltOnHostnameChange.

Release/Deployment Notes

There are no user-facing configuration changes. Starting additional projects is now faster because the router is no longer recreated when only project hostnames change. The router is still recreated when its published ports change or when it is not running.

@stasadev
stasadev requested a review from a team as a code owner July 6, 2026 14:12
@github-actions github-actions Bot added the bugfix label Jul 6, 2026
@github-actions

github-actions Bot commented Jul 6, 2026 •

Copy link
Copy Markdown

@rfay

This comment was marked as off-topic.

@stasadev

This comment was marked as off-topic.

@stasadev

This comment was marked as off-topic.

@stasadev

This comment was marked as off-topic.

@stasadev
stasadev marked this pull request as draft July 6, 2026 18:42
@rfay

rfay commented Jul 6, 2026

Copy link
Copy Markdown
Member

I'm annoyed that the router recreation thing resurfaced :)

@stasadev

This comment was marked as off-topic.

@stasadev stasadev closed this Jul 14, 2026
@stasadev

Copy link
Copy Markdown
Member Author

Anyway, the repro shows another problem with recreating the router again and again:

Starting ddev-router, pushing config... 
[+] up 1/1
 ✔ Container ddev-router Recreated 

So I can work on it here.

I forgot about this, reopening.

@stasadev stasadev reopened this Jul 14, 2026
@stasadev
stasadev force-pushed the 20260706_stasadev_dns branch from a82c03c to 56451d1 Compare July 14, 2026 11:21
@stasadev stasadev changed the title fix(hostname): retry DNS lookup before adding /etc/hosts entry perf(router): update network aliases in place instead of recreating on start, for #8132 Jul 14, 2026
@stasadev
stasadev marked this pull request as ready for review July 14, 2026 11:29
@rfay

rfay commented Jul 17, 2026

Copy link
Copy Markdown
Member

This is a little confusing to sort through as the PR changed purpose, focus and code, would have been better as a new PR. But glad you sorted it out nicely. Trying to make sure I fully understand it.

@rfay
rfay force-pushed the 20260706_stasadev_dns branch from 56451d1 to 3f645ed Compare July 17, 2026 01:22
@stasadev
stasadev force-pushed the 20260706_stasadev_dns branch 2 times, most recently from 893f36c to 7993bd0 Compare July 17, 2026 19:05
@stasadev

Copy link
Copy Markdown
Member Author

I added a new test TestRouterNotRebuiltOnHostnameChange.

@stasadev
stasadev force-pushed the 20260706_stasadev_dns branch from 7993bd0 to 539ff3c Compare July 18, 2026 10:51

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

Thank you for this!

I looked through this carefully, thanks for the test. I did a bunch of manual testing and stepped through it. It seems perfect.

I pushed a trivial comment in networks_test.go:144 that said the router had to be restarted.

There is one possibililty claude caught that may be worth exploring if you think it's relevant:

Sticky failure after a failed reconnect (my main concern): if NetworkDisconnect succeeds but NetworkConnect fails, the router is left running but off the network, and every subsequent ddev start fails at the disconnect step ("not connected") because GetRouterNetworkAliases returns an empty list without error for a disconnected container. Tolerating "not connected" on disconnect, or falling back to full router recreation when the in-place update errors, would fix it.

@rfay

rfay commented Jul 25, 2026

Copy link
Copy Markdown
Member

I'm fine with this going in as-is, I don't think the risk Claude pointed out is significant, and there is adequate error-reporting. The path forward is what you see fit or have time for.

stasadev and others added 4 commits July 29, 2026 20:25
…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]>
If NetworkConnect fails or is interrupted after NetworkDisconnect
succeeds, the router keeps running but is left off ddev_default.
GetRouterNetworkAliases then returns an empty list without error, so
StartDdevRouter takes the in-place update path again and every later
ddev start fails at the disconnect step with "is not connected to
network ddev_default".

UpdateContainerNetworkAliases now inspects the container and skips the
disconnect when it isn't attached to the network, so the reconnect runs
and repairs the state instead of erroring permanently.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@stasadev
stasadev force-pushed the 20260706_stasadev_dns branch from 7fabd62 to dd50b11 Compare July 29, 2026 17:25
@stasadev

Copy link
Copy Markdown
Member Author

There is one possibililty claude caught that may be worth exploring if you think it's relevant:

Sticky failure after a failed reconnect (my main concern): if NetworkDisconnect succeeds but NetworkConnect fails, the router is left running but off the network, and every subsequent ddev start fails at the disconnect step ("not connected") because GetRouterNetworkAliases returns an empty list without error for a disconnected container. Tolerating "not connected" on disconnect, or falling back to full router recreation when the in-place update errors, would fix it.

Thank you, I pushed one more commit for this.

@rfay rfay changed the title perf(router): update network aliases in place instead of recreating on start, for #8132 perf(router): update network aliases in place instead of recreating on start, for #8132 (#8561) [skip ci] Jul 29, 2026
@rfay
rfay merged commit 6e7e416 into ddev:main Jul 29, 2026
43 of 46 checks passed
@rfay
rfay deleted the 20260706_stasadev_dns branch July 29, 2026 21:54
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.

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

2 participants