daemon: backfill empty PBs slices for backward compat

So far, on ContainerStart, the daemon was silently backfilling empty
PortBindings slices with a PortBinding with unspecified HostIP and
HostPort. This was done by github.com/docker/go-connections/nat.SortPortMap.

This backfilling doesn't make much sense, and we're trying to remove
that package. So, move the backfilling to the API server, keep it for
older API versions, deprecate it for API 1.52, and drop it for API 1.53
and above.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
This commit is contained in:
Albin Kerouanton
2025-09-02 13:29:56 +02:00
parent 578ce11a56
commit 0ca7ac3258
5 changed files with 209 additions and 0 deletions

View File

@@ -260,6 +260,7 @@ func (container *Container) readHostConfig() error {
}
container.InitDNSHostConfig()
container.BackfillEmptyPBs()
return nil
}
@@ -625,6 +626,32 @@ func (container *Container) InitDNSHostConfig() {
}
}
// BackfillEmptyPBs backfills empty PortBindings slices with a port binding
// with an unspecified HostIP and HostPort. This is required to ensure backward
// compatibility for containers created with older Engine versions.
//
// Prior to v29.0, the daemon was doing this backfilling automatically through
// github.com/docker/go-connections/nat on ContainerStart. Starting with that
// version, it is done by the API for older API versions (i.e. < 1.53). Newer
// API versions are just dropping entries with empty PortBindings slices.
//
// See https://github.com/moby/moby/pull/50710#discussion_r2315840899 for more
// context.
func (container *Container) BackfillEmptyPBs() {
if container.HostConfig == nil {
return
}
for portProto, pb := range container.HostConfig.PortBindings {
if len(pb) > 0 || pb == nil {
continue
}
container.HostConfig.PortBindings[portProto] = []containertypes.PortBinding{
{}, // Backfill an empty PortBinding
}
}
}
// UpdateMonitor updates monitor configure for running container
func (container *Container) UpdateMonitor(restartPolicy containertypes.RestartPolicy) {
container.RestartManager().SetPolicy(restartPolicy)