Docker to run new container – list of all mapped ports?
When running a new container, we specify a port
RUN_PORT:EXPOSED_PORT to map with the host machine. This will fail if
RUN_PORT is already used.
So my question is how to list all the mapped port – so that we can pick up the port number out of the list.
I’m using Ubuntu 16.04
2 Solutions collect form web for “Docker to run new container – list of all mapped ports?”
Do the following command
sudo netstat -tlnp | grep ":RUN_PORT"
Replace RUN_PORT with the actual port to see which application is blocking it.
Listing all tcp ports being used (for listening) can be seen with:
Looking up what is using a single port can be done with a netstat and grep, or if you have lsof installed:
sudo lsof -i :80 # shows the process using port 80
Starting a docker container on a random available port mapped to port 80 inside the container:
docker run -p 80 -n container_name your_container
Looking up what random port docker used in the above command on the host (this includes what IP interfaces it’s attached to, or 0.0.0.0 for all interfaces, which is the default):
docker port container_name 80