Skip to content

feat: filter containers seen by docker-gen #623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Conversation

buchdag
Copy link
Member

@buchdag buchdag commented May 17, 2024

The PR add the -container-filter option to filter the containers that docker-gen will see.

The option work pretty much like -notify-filter and can be used multiple times to combine filters with AND.

This:

docker-gen \
    -container-filter status=running \
    -container-filter label=com.github.nginx-proxy.nginx \
    ./some/template.tmpl ./result.ext

will result in docker-gen only seeing running container that possess the label com.github.nginx-proxy.nginx.

Closes #276

Unfortunately Docker filter options for containers does not provide negative filtering, so #117 and #252 won't be entirely fixed by this.

Thanks @tarasov65536 for the inspiration.

@buchdag buchdag self-assigned this May 17, 2024
@buchdag buchdag marked this pull request as draft May 17, 2024 16:55
@buchdag buchdag force-pushed the filter-containers branch 2 times, most recently from dee75d0 to b8b39af Compare May 25, 2025 14:16
@buchdag buchdag force-pushed the filter-containers branch from b8b39af to 938fec6 Compare May 25, 2025 20:51
@buchdag buchdag force-pushed the filter-containers branch from 938fec6 to d3962a7 Compare May 25, 2025 21:26
@buchdag buchdag requested a review from Copilot May 25, 2025 21:29
Copy link

@Copilot Copilot AI left a comment

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 adds a new -container-filter option to docker-gen, allowing users to filter containers in a more flexible and composable way. Key changes include the removal of legacy filtering logic from the template package, updates to generator and configuration modules to support the new filter option, and corresponding updates in command-line options and documentation.

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
internal/template/template.go Removed legacy filterRunning logic and updated logging to use unfiltered containers, assuming filtering now occurs upstream.
internal/generator/generator.go Updated getContainers signature and internal filtering logic to use config.ContainerFilter; removed the All flag.
internal/context/context.go & context_test.go Removed the PublishedAddresses method and its test, as published/exposed filtering is now handled via container filters.
internal/config/config.go Replaced OnlyExposed, OnlyPublished, and IncludeStopped flags with ContainerFilter.
cmd/docker-gen/main.go Added container-filter flag and updated default filter logic, along with documentation changes.
README.md Updated command option descriptions to reflect the new filtering option.
Comments suppressed due to low confidence (3)

internal/context/context.go:107

  • The PublishedAddresses method and its associated tests have been removed. Verify that no other part of the codebase relies on this functionality and that its removal is fully covered by the new container filtering approach.
func (r *RuntimeContainer) PublishedAddresses() []Address {

internal/template/template.go:153

  • The removal of the filterRunning function means that filtering is now assumed to happen upstream. Ensure that the logging statement (which now logs the count of 'containers') still accurately reflects the intended filtered list; otherwise, update the log to avoid potential confusion.
contents := executeTemplate(config.Template, containers)

internal/generator/generator.go:400

  • The removal of the All flag and the decision to always pass 'true' for All in the Docker client's ListContainersOptions should be carefully validated. Confirm that this new approach interacts correctly with the provided container filters under all expected scenarios.
All:     true,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Docker stack filter
1 participant