Skip to content

Avoid panic on nil container Config/NetworkSettings/HostConfig#748

Open
JamBalaya56562 wants to merge 1 commit into
nginx-proxy:mainfrom
JamBalaya56562:fix/227-nil-container-guards
Open

Avoid panic on nil container Config/NetworkSettings/HostConfig#748
JamBalaya56562 wants to merge 1 commit into
nginx-proxy:mainfrom
JamBalaya56562:fix/227-nil-container-guards

Conversation

@JamBalaya56562

Copy link
Copy Markdown
Contributor

getContainers dereferenced container.Config, container.NetworkSettings and container.HostConfig (and GetContainerAddresses/renderAddress dereferenced NetworkSettings/Config) without nil checks. Inspect can return these as nil for some containers/daemons, causing a panic that aborts generation for every container.

This copies each pointer into a zero value when nil, so the container is still emitted with its ID/Name and empty fields instead of crashing, and makes GetContainerAddresses nil-safe (it's exported and used elsewhere). State is a value (not a pointer) and ranging nil maps/slices is safe, so those are left unchanged.

Adds nil-struct tests for GetContainerAddresses and getContainers.

Fixes #227

🤖 Generated with Claude Code

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens container inspection and address rendering against nil docker.Container sub-struct pointers returned by some Docker daemons/remote APIs, preventing panics that would otherwise abort template generation for all containers.

Changes:

  • In getContainers, copy Config, NetworkSettings, and HostConfig into zero-value locals when their pointers are nil, and use the locals for field access.
  • Make context.GetContainerAddresses / renderAddress tolerant of nil NetworkSettings (and nil Config for the exposed-ports fallback).
  • Add tests covering nil struct pointers for both getContainers and GetContainerAddresses.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
internal/generator/generator.go Avoid nil-pointer dereferences during container inspect processing by using zero-value copies.
internal/generator/generator_test.go Add regression test ensuring nil Config/NetworkSettings/HostConfig doesn’t panic during getContainers.
internal/context/address.go Make address rendering and port enumeration nil-safe for missing NetworkSettings/Config.
internal/context/address_test.go Add tests ensuring GetContainerAddresses returns empty (and doesn’t panic) when struct pointers are nil.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/generator/generator.go
Comment thread internal/generator/generator.go Outdated
@JamBalaya56562 JamBalaya56562 force-pushed the fix/227-nil-container-guards branch from 955f216 to 6417e1b Compare June 28, 2026 12:03
@JamBalaya56562

Copy link
Copy Markdown
Contributor Author

Addressed Copilot's review feedback — renamed the shadowing config local to containerConfig and fixed the adressesaddresses typo.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@JamBalaya56562 JamBalaya56562 force-pushed the fix/227-nil-container-guards branch from 6417e1b to 5d61367 Compare June 28, 2026 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No container Image Name cause panic

2 participants