Jay Taylor's notes
back to listing indexnetstat - Docker: any way to list open sockets inside a running docker container? - Stack Overflow
[web search]-
Home
-
- Public
-
Questions -
Tags
-
Users
-
Companies
-
Collectives
-
Explore Collectives
-
TeamsStack Overflow for Teams – Start collaborating and sharing organizational knowledge.
Create a free Team Why Teams?
I would like to execute netstat inside a running docker container to see open TCP sockets and their statuses. But, on some of my docker containers, netstat is not available. Is there any way to get open sockets (and their statuses, and which IP addresses they are connected to if any) without using netstat, via some docker API? (BTW, my container uses docker-proxy - that is, not directly bridged)
I guess I could look at /proc file system directly, but at that point, I might as well docker cp netstat into the container and execute it. I was wondering if there was any facility that docker might provide for this.
7 Answers
You can use the nsenter
command to run a command on your host inside the network namespace of the Docker container. Just get the PID of your Docker container:
docker inspect -f '{{.State.Pid}}' container_name_or_id
For example, on my system:
$ docker inspect -f '{{.State.Pid}}' c70b53d98466
15652
And once you have the PID, use that as the argument to the target (-t
) option of nsenter
. For example, to run netstat
inside the container network namespace:
$ sudo nsenter -t 15652 -n netstat
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
Notice that this worked even though the container does not have netstat
installed:
$ docker exec -it c70b53d98466 netstat
rpc error: code = 13 desc = invalid header field value "oci runtime error: exec failed: container_linux.go:247: starting container process caused "exec: "netstat": executable file not found in $PATH"n"
(nsenter
is part of the util-linux
package)
-
Does this solution applicable for other platforms such as windows, mac etc.,?– RaoNov 1, 2016 at 6:42
-
@Rao, possibly:
nsenter
is a Linux command, so you would need to be able to log in to the Linux VM that is actually being used to host your Docker containers. And of course, that VM would need to have thensenter
command available.– larsksNov 1, 2016 at 11:06 -
you may use this snippet to get all netstat for all dockers stackoverflow.com/questions/37171909/… Mar 15, 2018 at 10:16
-
On an AWS EKS node, I'm root, but get:
sudo nsenter -t 14207 -n netstat
returnsnsenter: cannot open /proc/14207/ns/net: No such file or directory
. I can see the path that it says does not exist but cannot seem to do anything to interrogate it. Has anyone run into this? Apr 21, 2021 at 15:59 -
nsenter
=> Permission deniedsudo nsenter
=> bash: sudo: command not found– MarcSep 21, 2021 at 18:15
The two commands from @larsks answer merged into one-liner - no need to copy-paste the PID(s) (just replace container_name_or_id
):
sudo nsenter -t $(docker inspect -f '{{.State.Pid}}' container_name_or_id) -n netstat
-
Sidenote: one would need to add another
sudo
so that the command is... $(sudo docker inspect ...
, otherwise the command will fail if it isn't run in a root shell. Jun 11, 2021 at 19:53
If you have iproute2
package installed, you can use
sudo nsenter -t $(docker inspect -f '{{.State.Pid}}' container_name_or_id) -n ss
or
sudo nsenter -t $(docker inspect -f '{{.State.Pid}}' container_name_or_id) -n ss -ltu
It will show TCP
and UDP
If you want them all (all containers) try this.
$ for i in `docker ps -q` ; do sudo nsenter -t $(docker inspect -f '{{.State.Pid}}' $i) -n netstat ; done
I tried the other solutions and it didn't work for me by my colleague gave me this solution. Thought I would mention it here for others like me and for me to refer to later lol.
docker exec -it [container name] bash
grep -v “rem_address” /proc/net/tcp
docker inspect <container_id>
- Look for "ExposedPorts" in "Config"
server:docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80acfa804b59 admirito/gsad:10 "docker-entrypoint.s…" 18 minutes ago Up 10 minutes 80/tcp gvmcontainers_gsad_1
-
this is wrong. this will only five you the ports that either the Dockerimage declared and those that were explicitly exposed (in both cases it doesn't matter if the container process is actually listening...) Dec 21, 2019 at 13:43
Your Answer
Not the answer you're looking for? Browse other questions tagged docker netstat alpine or ask your own question.
- The Overflow Blog
-
-
- Featured on Meta
-
-
-
-
-
- Hot Meta Posts
-
16
Get the weekly newsletter! In it, you'll get:
- The week's top questions and answers
- Important community announcements
- Questions that need answers
see an example newsletter
Linked
Related
Hot Network Questions
-
How to (relatively safely) store a small amount (200 milliliters) of gasoline for up to a month?
-
Finite groups with integral character table
-
How can we get a line plot along x or y from a 2D plot?
-
Z80 is making weird relative jump errors. How can that be?
-
IBM PC Alt + numpad for entering character codes: firmware, BIOS or OS?
-
Why MySQL 8 doesn't return any rows when using a table with a descending PRIMARY KEY, 2 indexes, and valid join conditions with existing rows
-
How to place objects in a grid pattern with geometry nodes?
-
Accessing the content of your check-in baggage in baggage claim at an international airport
-
How is CAS ans GS affected by instant wind changes at different altitudes
-
Undesired spaces in mathematics two column paper
-
Difference in having even number and odd number of samples in DFT?
-
How are fighter jets able to be flown by one pilot even with maximum take off weights exceeding 12,500 lb?
-
Wordle Game clone
-
How to tell your advisor that you lost motivation for a PhD and need a break?
-
Preventing auxiliary condenser jug overflow in a portable air conditioner
-
Does a US President have to file any paperwork to declassify information?
-
Does Garbage Collection Scan The Entire Memory?
-
Rerolling or taking +2
-
EV-friendly accomodation
-
How to explain differences in mutual intelligibility?
-
Could vacuum armour protect vehicles and people from explosions?
-
What does "め" mean in this case?
-
What does the average American citizen think about the raid on Mar-a-Lago?
-
How many openings should I study simultaneously?