Written by

Senior Startups and Community Programs Manager at InterSystems Corporation
Question Evgeny Shvarov · Feb 3, 2024

How to check whether docker ports are busy before building the image?

Hi folks!

Sometimes building an images via docker-compose.yml can take significant time.

And often after a minute or two of waiting it turns out that ports listed in docker-compose are busy and you need to start building process again with different ports or to turn down processes that took it.

Is there a way to check ports listed in docker-compose BEFORE starting the image baking process?

How do you deal with this problem?

Comments

Patrick Jamieson · Feb 3, 2024

If you know the port and you want to check it status, you can issue the terminal command lsof -i:<<portno>>

For example I have a docker image running on port 80, as you can see there are processes attached to this port making it unavailable 

(base) USMBP16pjamieso:FHIRZPM pjamieso$ lsof -i :80
COMMAND    PID     USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
trustd    1208 pjamieso   23u  IPv4 0x5eb92c9816be8ddf      0t0  TCP usmbp16pjamieso.fios-router.home:53920->192.229.211.108:http (ESTABLISHED)
trustd    1208 pjamieso   27u  IPv4 0x5eb92c98164d7a1f      0t0  TCP usmbp16pjamieso.fios-router.home:53921->192.229.211.108:http (ESTABLISHED)
com.docke 7158 pjamieso  755u  IPv6 0x5eb92c9337bb54a7      0t0  TCP *:http (LISTEN)

0
Robert Cemper · Feb 3, 2024

In Windows it's netstat running from CMD as Admin
My preferred option:  netstat -anop TCP  (shortened)

C:\WINDOWS\system32>netstat -anop TCP 
  Aktive Connection
  Proto  Lokal Address          Remote Address         Status         PID
  TCP    0.0.0.0:21             0.0.0.0:0              LISTEN         4924
  TCP    0.0.0.0:80             0.0.0.0:0              LISTEN         4
  TCP    0.0.0.0:135            0.0.0.0:0              LISTEN         1384
  TCP    0.0.0.0:445            0.0.0.0:0              LISTEN         4
  TCP    0.0.0.0:623            0.0.0.0:0              LISTEN         10684
  TCP    0.0.0.0:1972           0.0.0.0:0              LISTEN         8004
  TCP    0.0.0.0:2179           0.0.0.0:0              LISTEN         2348
  TCP    0.0.0.0:16992          0.0.0.0:0              LISTEN         10684
  TCP    0.0.0.0:41773          0.0.0.0:0              LISTEN         11408
  TCP    0.0.0.0:42773          0.0.0.0:0              LISTEN         11408
  TCP    0.0.0.0:49664          0.0.0.0:0              LISTEN         848
  TCP    0.0.0.0:49665          0.0.0.0:0              LISTEN         652
  TCP    0.0.0.0:52493          0.0.0.0:0              LISTEN         11408
  TCP    0.0.0.0:52773          0.0.0.0:0              LISTEN         9476
  TCP    0.0.0.0:58091          0.0.0.0:0              LISTEN         5156
  TCP    0.0.0.0:58816          0.0.0.0:0              LISTEN         964
  TCP    127.0.0.1:2375         0.0.0.0:0              LISTEN         11408
  TCP    127.0.0.1:5354         0.0.0.0:0              LISTEN         4852
  TCP    127.0.0.1:5905         127.0.0.1:58786        ESTABLISHED    5156
  TCP    127.0.0.1:5905         127.0.0.1:58787        ESTABLISHED    5156
  TCP    127.0.0.1:5905         127.0.0.1:58788        ESTABLISHED    5156

In Ubuntu (container) I tried  netstat -at4p for a similar result while connected to SMP 

:~/dev$ netstat -at4p
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.11:37033        0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:52773           0.0.0.0:*               LISTEN      476/httpd
tcp        0      0 0.0.0.0:1972            0.0.0.0:*               LISTEN      453/irisdb
tcp        0      0 localhost:1972          localhost:44998         ESTABLISHED 1745/irisdb
tcp        0      0 localhost:44998         localhost:1972          ESTABLISHED 479/httpd
tcp        0      0 localhost:1972          localhost:45014         ESTABLISHED 1746/irisdb
tcp        0      0 localhost:1972          localhost:44992         ESTABLISHED 1744/irisdb
tcp        0      0 localhost:44984         localhost:1972          ESTABLISHED 479/httpd
tcp        0      0 localhost:1972          localhost:44954         ESTABLISHED 1741/irisdb
tcp        0      0 localhost:44992         localhost:1972          ESTABLISHED 479/httpd
tcp        0      0 localhost:44968         localhost:1972          ESTABLISHED 479/httpd
tcp        0      0 localhost:45014         localhost:1972          ESTABLISHED 479/httpd
tcp        0      0 localhost:44954         localhost:1972          ESTABLISHED 479/httpd
tcp        0      0 localhost:1972          localhost:44984         ESTABLISHED 1743/irisdb
tcp        0      0 localhost:1972          localhost:44968         ESTABLISHED 1742/irisdb
:~/dev$

might look similar in other *UX

0