Skip to content

fix(dockerutil): fall back to NetworkSettings.Ports in GetBoundHostPorts (#8657) [skip ci] - #8657

Merged
rfay merged 1 commit into
ddev:mainfrom
rfay:20260802_rfay_bound_host_ports_fallback
Aug 12, 2026
Merged

rfay merged 1 commit into
ddev:mainfrom
rfay:20260802_rfay_bound_host_ports_fallback

Conversation

@rfay

@rfay rfay commented Aug 3, 2026

Copy link
Copy Markdown
Member

The Issue

GetBoundHostPorts() only reads HostConfig.PortBindings (the requested
port bindings) to figure out which host ports a container has bound. On
socktainer (the Docker-compatible API in front of Apple Container), that
field comes back empty even for containers with real, working port
bindings — only NetworkSettings.Ports (the actual bindings) is
populated. The practical effect: DDEV thinks the port is free and tries to
reallocate it, when it's actually already in use by the running container.

This is a real (if narrow) DDEV bug independent of Apple Container: the
Docker API documents HostConfig.PortBindings and NetworkSettings.Ports
as separate, sometimes-divergent fields, and DDEV was only ever reading
one of them.

How This PR Solves The Issue

GetBoundHostPorts() now falls back to NetworkSettings.Ports when
HostConfig.PortBindings yields no bound ports, via a shared
addBoundHostPorts() helper used for both fields.

Manual Testing Instructions

  • go test -v ./pkg/dockerutil -run TestAddBoundHostPorts
  • On a normal Docker setup this is a no-op (regular dockerd populates
    both fields), so existing behavior is unchanged; verified live against
    socktainer where the fallback path is what actually fires.

Automated Testing Overview

Added TestAddBoundHostPorts in pkg/dockerutil/containers_internal_test.go
(whitebox, package dockerutil), covering both the PortBindings-populated
and fallback-to-NetworkSettings.Ports cases.

Release/Deployment Notes

None. Pure pkg/dockerutil change, no image dependency.

@github-actions github-actions Bot added the bugfix label Aug 3, 2026
@github-actions

github-actions Bot commented Aug 3, 2026 •

Copy link
Copy Markdown

GetBoundHostPorts() only read HostConfig.PortBindings, which is the
binding that was *requested* at container-create time. Some providers
(socktainer/Apple Container, observed while investigating ddev#7372) return an
empty HostConfig.PortBindings on inspect even though the container has
ports bound, while NetworkSettings.Ports - the binding actually in
effect - is populated correctly.

The empty result fed GetAvailableRouterPort(), which uses it to decide
whether ddev-router already owns a proposed port before probing host
availability. With no bound ports visible, every router port looked
unclaimed, the host-availability probe found the router's own listener and
called it busy, and DDEV allocated a fresh ephemeral port on every command
that touches the router (e.g. `ddev ssh`), silently rewriting the
project's URLs each time.

Extracted the port-collecting loop into addBoundHostPorts() so it can
build the result from either source, and fall back to
NetworkSettings.Ports only when HostConfig.PortBindings yields nothing -
real Docker keeps HostConfig.PortBindings populated, so behavior there is
unchanged. Verified live against a running ddev-router on socktainer: the
same inspect that reports HostConfig.PortBindings as null carries the
correct ports in NetworkSettings.Ports.

Added TestAddBoundHostPorts covering the merge/fallback/empty-HostPort
cases; existing TestGetBoundHostPorts continues to pass unchanged.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@rfay
rfay force-pushed the 20260802_rfay_bound_host_ports_fallback branch from b0f196d to 0321934 Compare August 11, 2026 19:45
@rfay
rfay marked this pull request as ready for review August 11, 2026 19:46
@rfay
rfay requested a review from a team as a code owner August 11, 2026 19:46
@rfay

rfay commented Aug 11, 2026

Copy link
Copy Markdown
Member Author

I guess this is safe but annoying. Probably reasonable.

@rfay
rfay requested a review from stasadev August 11, 2026 19:46

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

@rfay rfay changed the title fix(dockerutil): fall back to NetworkSettings.Ports in GetBoundHostPorts fix(dockerutil): fall back to NetworkSettings.Ports in GetBoundHostPorts (#8657) [skip ci] Aug 12, 2026
@rfay
rfay merged commit a58b59e into ddev:main Aug 12, 2026
52 of 54 checks passed
@rfay
rfay deleted the 20260802_rfay_bound_host_ports_fallback branch August 12, 2026 03:28
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.

2 participants