Skip to content

feat: expose current container to templates#758

Open
JamBalaya56562 wants to merge 2 commits into
nginx-proxy:mainfrom
JamBalaya56562:feat/458-current-container
Open

feat: expose current container to templates#758
JamBalaya56562 wants to merge 2 commits into
nginx-proxy:mainfrom
JamBalaya56562:feat/458-current-container

Conversation

@JamBalaya56562

@JamBalaya56562 JamBalaya56562 commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Problem

When docker-gen runs standalone (not bundled with nginx-proxy) with -only-exposed / -only-published, the docker-gen container itself exposes no ports and is dropped by the server-side container filter. Templates that resolve the current container via where $ "ID" .Docker.CurrentContainerID | first then get nil, even though .Docker.CurrentContainerID is detected correctly.

Change

  • Adds a CurrentContainer() method on the root Context, returning the *RuntimeContainer whose ID matches .Docker.CurrentContainerID (or nil), so templates can write {{ with .CurrentContainer }}…{{ end }}.
  • In getContainers, after the filtered list is built, the current container is inspected and appended when its ID is non-empty and not already present — so it is available even when -only-exposed/-only-published would otherwise filter it out.

Implementation notes

  • The per-container inspect→RuntimeContainer conversion is extracted into an inspectContainer helper so the same logic serves both the list loop and the always-include path, without duplication.
  • The current-container lookup goes through a small package-level indirection (getCurrentContainerID) so the append path can be unit-tested without a live /proc.

Tests

  • TestCurrentContainer covers the match / no-match / empty-ID cases of the accessor.
  • TestGetContainersIncludesCurrentContainer verifies the container is appended when it is absent from the filtered list.
  • README documents .CurrentContainer.

Fixes #458

🤖 Generated with Claude Code

@JamBalaya56562 JamBalaya56562 force-pushed the feat/458-current-container branch from 58953bd to 16eb7dd Compare July 11, 2026 20:02
@buchdag

buchdag commented Jul 12, 2026

Copy link
Copy Markdown
Member

@JamBalaya56562 looks like this PR is proposing two solutions to the same problem.

If we're getting a CurrentContainer() method on the root Context, what's the point of forcing the inclusion of the current container in the filtered list returned by getContainers ?

Also please flag AI-generated PR explicitly in the PR description (ie "🤖 Generated with Claude Code" like in this PR) and add the AI agent as co-author of the commits.

@JamBalaya56562 JamBalaya56562 force-pushed the feat/458-current-container branch from 16eb7dd to e10d3ac Compare July 12, 2026 22:40
@JamBalaya56562

Copy link
Copy Markdown
Contributor Author

Thanks for the review!

On the attribution: done — I've added the Co-Authored-By trailer to the commit and the 🤖 Generated with [Claude Code](https://claude.com/claude-code) note to the description, and rebased onto the latest main.

On the design: they aren't really two solutions to the same problem — the inclusion in getContainers is the actual fix, and CurrentContainer() is only a convenience accessor layered on top of it.

CurrentContainer() looks the container up in the slice it is given (the same filtered list), so on its own it does not solve the -only-exposed / -only-published case: under those flags the docker-gen container is dropped by the server-side filter, so the method would just walk the filtered list, fail to find the ID, and return nil. Context is a plain []*RuntimeContainer with no docker client, so it cannot inspect the daemon itself to recover the missing container.

Forcing the inclusion in getContainers is what puts the current container back into the list — that is the part that actually fixes the issue. Once it is there, both the existing documented idiom where $ "ID" .Docker.CurrentContainerID | first and CurrentContainer() resolve it; the method is just sugar for that lookup, which the issue asked to surface as a top-level value.

So the two viable shapes are "inclusion only" or "inclusion + accessor" — dropping the inclusion is not an option, since the method alone can't recover a filtered-out container. If you'd prefer to keep the API surface minimal, I'm happy to drop CurrentContainer() and keep only the inclusion (the where … | first idiom then covers it). Or I can keep both if the accessor is welcome. Which would you prefer?

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 makes the docker-gen container itself accessible to templates even when container filtering (-only-exposed / -only-published) would normally exclude it, by exposing a .CurrentContainer accessor and ensuring the current container can be present in the root container list.

Changes:

  • Add Context.CurrentContainer() to return the container matching .Docker.CurrentContainerID.
  • Refactor container inspection into inspectContainer() and append the current container to the filtered container list when missing.
  • Add unit tests for the new accessor and the “append current container” behavior; document .CurrentContainer in the README.

Reviewed changes

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

Show a summary per file
File Description
README.md Documents the new .CurrentContainer template accessor and its behavior under filtering.
internal/generator/generator.go Refactors container inspection and appends the current container when absent from filtered results.
internal/generator/generator_test.go Adds coverage ensuring the current container is appended when not returned by the filtered list.
internal/context/context.go Adds CurrentContainer() accessor on the root Context.
internal/context/context_test.go Adds tests for CurrentContainer() behavior.

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

Comment thread internal/generator/generator.go Outdated
Comment thread internal/context/context_test.go
@buchdag

buchdag commented Jul 13, 2026

Copy link
Copy Markdown
Member

I'm sorry but I don't think I'm on board with this implementation.

An hypothetical Context.CurrentContainer should not rely on getContainers() bypassing filters or -only-exposed / -only-published with a clunky logic.

I think this should rather work like Context.Docker, with a separate set method called by getContainers() (see SetServerInfo()), and should not impact the slice of RuntimeContainer returned by getContainers().

@JamBalaya56562 JamBalaya56562 force-pushed the feat/458-current-container branch from bca7b64 to 4b09143 Compare July 13, 2026 10:30
@JamBalaya56562

Copy link
Copy Markdown
Contributor Author

Thanks for the clear direction — I've reworked it exactly along those lines (rebased on the latest main, in a follow-up commit).

CurrentContainer now works like Docker: getContainers() resolves the docker-gen container and stores it through a new context.SetCurrentContainer() setter (mirroring SetServerInfo()), and Context.CurrentContainer() simply reads that package-level value. The returned []*RuntimeContainer slice is no longer touched at all — no force-include and no filter bypass, so -only-exposed/-only-published and every other container filter are fully respected. The clunky gating logic is gone.

@buchdag buchdag force-pushed the feat/458-current-container branch from 4b09143 to 5782b10 Compare July 14, 2026 08:54
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@buchdag buchdag force-pushed the feat/458-current-container branch from 5782b10 to 0b3adfb Compare July 14, 2026 08:55
Comment thread README.md Outdated
Co-authored-by: Nicolas Duchon <nicolas.duchon@gmail.com>
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.

$CurrentContainer cannot be initialized when -only-exposed is used

3 participants