0

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.

p.s.

I'm using Ubuntu 16.04

2
  • 1
    Assuming you're on Ubuntu. I would do sudo netstat -alnp | grep LISTEN to check and see if the port I want to use is taken or not. If it is taken, can I terminate the program to free it up? if no then I typically move on to another port.
    – Samuel Toh
    Commented Jul 6, 2017 at 5:19
  • Yeah, Ubuntu it is. Sound a right solution though the printed output is noisy though - great if you would make it just the list of used port and post as the accepted answer. Thank you!
    – Nam G VU
    Commented Jul 6, 2017 at 5:51

2 Answers 2

2

Do the following command

sudo netstat -tlnp | grep ":RUN_PORT"

Replace RUN_PORT with the actual port to see which application is blocking it.

3
  • What a clever way <3
    – Nam G VU
    Commented Jul 6, 2017 at 11:19
  • Though I got lots of ports occupied on my server which run Docker; would love to be able to get the clean list of occupied ports.
    – Nam G VU
    Commented Jul 6, 2017 at 11:21
  • Then use the command stated in the comments above. Otherwise you have to look for an application that displays occupied ports in a proper GUI.
    – jz22
    Commented Jul 6, 2017 at 11:27
1

Listing all tcp ports being used (for listening) can be seen with:

netstat -lnt

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
1
  • Thanks for the random port idea. Though we need to set specific port by our own.
    – Nam G VU
    Commented Jul 7, 2017 at 1:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.