Skip to content

feat(container): add container name resolution for Docker/Podman#46

Merged
jpr5 merged 11 commits intojpr5:masterfrom
Horkyze:container-name-resolution
Feb 7, 2026
Merged

feat(container): add container name resolution for Docker/Podman#46
jpr5 merged 11 commits intojpr5:masterfrom
Horkyze:container-name-resolution

Conversation

@Horkyze
Copy link
Contributor

@Horkyze Horkyze commented Jan 11, 2026

Add container name resolution to display container names alongside IP addresses when capturing traffic from Docker or Podman containers.

Features:

  • Real-time container tracking via Docker socket events API
  • Fallback to CLI polling with 30-second TTL when socket unavailable
  • Support for both Docker and Podman runtimes
  • IP address formatted as "container_name(ip_address)"
  • Command injection prevention via container ID validation

Build system:

  • New configure flag: --enable-container-resolution (default off)
  • Adds container-resolution.o to EXTRA_OBJS when enabled

Must be enabled at compile-time with --enable-container-resolution.

Add container name resolution to display container names alongside IP
addresses when capturing traffic from Docker or Podman containers.

Features:
- Real-time container tracking via Docker socket events API
- Fallback to CLI polling with 30-second TTL when socket unavailable
- Support for both Docker and Podman runtimes
- IP address formatted as "container_name(ip_address)"
- Command injection prevention via container ID validation

Build system:
- New configure flag: --enable-container-resolution (default off)
- Adds container-resolution.o to EXTRA_OBJS when enabled

Must be enabled at compile-time with --enable-container-resolution.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@jpr5
Copy link
Owner

jpr5 commented Jan 12, 2026

Heyyyyy, you finally did it! Or, your AI did, rather. 😉 Thanks for this! One request: can you resolve any conflicts for me, to merge cleanly so I can test it? Thank you! 🙏 @Horkyze

@jpr5
Copy link
Owner

jpr5 commented Jan 12, 2026

I'm also wondering, could you write up an additional scenario in EXAMPLES.md that shows how it works? I think it's necessary for people to conceptualize how to use it -- it's not a common case at the outset (though it could eventually be), and since it's currently compile-time (vs. runtime, still thinking about pros/cons of putting it behind a cmdline flag), it's something whose value must be conceived of/understood before compiling. @Horkyze

jpr5 added 9 commits February 7, 2026 13:28
Covers microservice debugging, compose stack HTTP monitoring,
per-container BPF filtering, and inter-container DNS resolution.
add_container_by_id() (the socket event path) validates container IDs
via is_valid_container_id() before passing them to popen(), but
discover_containers_for_runtime() (the CLI polling path) does not.

Add the same validation check to the CLI path to prevent potential
command injection via crafted docker ps output.
Container names from Docker/Podman are printed directly to the
terminal without sanitization. While Docker restricts names to safe
characters, Podman may differ and crafted CLI output could contain
ANSI escape sequences that affect terminal behavior.

Add sanitize_container_name() that replaces characters outside
[a-zA-Z0-9_.-] with underscores. Applied in both the CLI discovery
path and the socket event path on cache insertion.
After ngrep drops privileges to an unprivileged user, CLI-based
container polling (docker ps / podman ps) silently fails because
the user lacks access. The cache goes stale with no indication.

Track whether init succeeded initially, and if a subsequent refresh
finds zero containers, disable further polling attempts and warn the
user. The pre-drop cache continues serving lookups until entries
expire naturally.
Flip the --enable-container-resolution default from off to on. Users
can still opt out with --disable-container-resolution at build time.
The feature has no external library dependencies and compiles cleanly
on all platforms.
Replace the compile-time-only activation model with a runtime flag.
Container resolution is now compiled in by default but only activates
when -r is passed on the command line. Zero overhead when not used.

The compile-time guard (USE_CONTAINER_RESOLUTION) remains so users
can still --disable-container-resolution at build time.

Also adds container-resolution.c to the Windows CMake build and
documents -r in the manpage.
The original code only tried two hardcoded socket paths
(/var/run/docker.sock and /run/podman/podman.sock), missing common
configurations on macOS and rootless Podman setups.

New discovery order:
1. $DOCKER_HOST (unix:// prefix; returns early for tcp://)
2. /var/run/docker.sock (Linux/BSD standard)
3. $HOME/.docker/run/docker.sock (macOS Docker Desktop)
4. /run/podman/podman.sock (Linux rootful Podman)
5. $XDG_RUNTIME_DIR/podman/podman.sock (rootless Podman)

$DOCKER_HOST is only used for socket connect(), never interpolated
into popen() commands (the docker CLI reads it from the environment).
Container resolution is now activated with -r at runtime instead of
requiring a special configure flag at build time. Update all examples
and intro text to reflect this.
MSVC uses _popen/_pclose instead of the POSIX popen/pclose. Without
these defines, the Windows build fails with unresolved external symbol
errors for both functions.
@jpr5 jpr5 merged commit dcf11e1 into jpr5:master Feb 7, 2026
14 checks passed
@jpr5
Copy link
Owner

jpr5 commented Feb 8, 2026

@Horkyze Well buddy, it's in. Thanks again.

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.

2 participants