0

I have a docker container A. There are two applications ( B&C) running in the container A in Ports 8080 and 8081( TCP ports). I would like to write a python/nodejs or bash script to check if ports are open by respective applications?

Usecase:

  1. Get Hostname of the docker container
  2. Validate if both applications A and B opens the port to Ports 8080 and 8081.
  3. Once validated, i need to print that test has passed and print exit status.
Coolbreeze
  • 906
  • 2
  • 15
  • 34

1 Answers1

0

You can use netstat to display open ports. Together with grep you can filter the output like this:

netstat | grep 8080

netstat's output will be directed to grep, which only displays lines containing "8080" You can then use test to check that both netstat commands output something, for example by using -n (meaning a string is nonzero):

#!/bin/bash
servicea=$(netstat | grep 8080)
serviceb=$(netstat | grep 8081)

if [ -n "$servicea" ] && [ -n "$serviceb" ]
then
echo "up and running"
fi

Note: This works from inside the container. To access ports from outside the container you need to map them.

jamcenner
  • 1
  • 1