brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: nsenter runs host tools inside a Docker container's network

Docker
~/tips/tip-docker-nsenter-network-namespace

sudo nsenter -t "$pid" -n ss -tlnp

Use the host's ss, tcpdump or curl as if they ran inside a container.

Minimal images have no ss, ip or tcpdump. You do not have to install them in the container: on the host, nsenter can run your own copies inside the container’s network namespace.

pid=$(docker inspect --format '{{.State.Pid}}' myapp)
sudo nsenter -t "$pid" -n ss -tlnp
sudo nsenter -t "$pid" -n ip addr

-t picks the target process, the container’s main process here, and -n enters only its network namespace. The filesystem stays the host’s, so every tool you have on the host is available, while the network view is the container’s: its interfaces, its IP and the ports it really listens on.

I find this the fastest way to answer whether the app listens on 127.0.0.1 or on all interfaces inside the container, which explains many ports that are published but unreachable. tcpdump -i eth0 works the same way.

It needs root on the host and a Linux host. On Docker Desktop the containers run in a VM, so the PID means nothing to your Mac or Windows shell. There, a debug container with --network container:myapp is the way to go. The PID also changes every time the container restarts, so look it up again rather than reusing an old value.

Further reading: nsenter(1) manual page, docker container inspect

More Docker tips ONBUILD RUN /usr/local/bin/python-build --dir /app/src SHELL ["/bin/bash", "-c"] docker commit <container> myname/testimage:version3 docker import /path/to/exampleimage.tgz docker compose events All Docker tips (192) All topics

Link