feat: expose current container to templates#758
Conversation
58953bd to
16eb7dd
Compare
|
@JamBalaya56562 looks like this PR is proposing two solutions to the same problem. If we're getting a 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. |
16eb7dd to
e10d3ac
Compare
|
Thanks for the review! On the attribution: done — I've added the On the design: they aren't really two solutions to the same problem — the inclusion in
Forcing the inclusion in 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 |
There was a problem hiding this comment.
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
.CurrentContainerin 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.
|
I'm sorry but I don't think I'm on board with this implementation. An hypothetical I think this should rather work like |
bca7b64 to
4b09143
Compare
|
Thanks for the clear direction — I've reworked it exactly along those lines (rebased on the latest
|
4b09143 to
5782b10
Compare
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
5782b10 to
0b3adfb
Compare
Co-authored-by: Nicolas Duchon <nicolas.duchon@gmail.com>
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 viawhere $ "ID" .Docker.CurrentContainerID | firstthen get nil, even though.Docker.CurrentContainerIDis detected correctly.Change
CurrentContainer()method on the rootContext, returning the*RuntimeContainerwhose ID matches.Docker.CurrentContainerID(or nil), so templates can write{{ with .CurrentContainer }}…{{ end }}.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-publishedwould otherwise filter it out.Implementation notes
RuntimeContainerconversion is extracted into aninspectContainerhelper so the same logic serves both the list loop and the always-include path, without duplication.getCurrentContainerID) so the append path can be unit-tested without a live/proc.Tests
TestCurrentContainercovers the match / no-match / empty-ID cases of the accessor.TestGetContainersIncludesCurrentContainerverifies the container is appended when it is absent from the filtered list..CurrentContainer.Fixes #458
🤖 Generated with Claude Code