Often while working with a Docker container, we need to look at the network connections being used by the container for initial debugging or troubleshooting purposes. You may want to see which IP is listening on a port or how many connections are currently active in the container. Since a Docker is an isolated environment, running netstat on a server won’t give you network connections of the container. Instead, you have to either get inside a container to run the netstat or run it remotely. Let’s see both options…
# 1. Getting inside Docker container to run netstat
As a first step, find the Container ID of the container that you want to troubleshoot. Here the one I want to troubleshoot is the container with ID 0ce7cfb9be37. Now to get a shell (bash) session of this container, use: This should land you in a bash prompt inside the container. You can install the netstat package to look for established network connections. By default, these utilities may not be available inside the container. So to install it, use: Now, we can use the netstat command as usual. Output: As you can see from the above output, established connections with their source and destination addresses are listed. To see processes listening on ports, you can use: Output:
# 2. Run netstat without getting inside the container
First thing first, we need to get the container ID with the docker ps command. And, then run the docker command as below to find out all the established connections for the container. This would result something like below. So the idea is to run the netstat command along with the docker exec command.
Conclusion
Now you have the required connection details, you can proceed with your troubleshooting by looking further at docker and process logs.