· Feb 3

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?

Discussion (3)1
Log in or sign up to continue

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
trustd    1208 pjamieso   23u  IPv4 0x5eb92c9816be8ddf      0t0  TCP usmbp16pjamieso.fios-router.home:53920-> (ESTABLISHED)
trustd    1208 pjamieso   27u  IPv4 0x5eb92c98164d7a1f      0t0  TCP usmbp16pjamieso.fios-router.home:53921-> (ESTABLISHED)
com.docke 7158 pjamieso  755u  IPv6 0x5eb92c9337bb54a7      0t0  TCP *:http (LISTEN)

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                 LISTEN         4924
  TCP                 LISTEN         4
  TCP                LISTEN         1384
  TCP                LISTEN         4
  TCP                LISTEN         10684
  TCP               LISTEN         8004
  TCP               LISTEN         2348
  TCP              LISTEN         10684
  TCP              LISTEN         11408
  TCP              LISTEN         11408
  TCP              LISTEN         848
  TCP              LISTEN         652
  TCP              LISTEN         11408
  TCP              LISTEN         9476
  TCP              LISTEN         5156
  TCP              LISTEN         964
  TCP              LISTEN         11408
  TCP              LISTEN         4852
  TCP        ESTABLISHED    5156
  TCP        ESTABLISHED    5156
  TCP        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*               LISTEN      -
tcp        0      0 *               LISTEN      476/httpd
tcp        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

might look similar in other *UX