I solved this problem with @paj solution given in comment to this question.
if you are running elastic search in a seperate container then localhost is the wrong hostname, you need to use the hostname of the ES container.
if you are running elastic search in a seperate container then localhost is the wrong hostname, you need to use the hostname of the ES container.
So checked my Docker container.
As you can see my docker container name for elastic search is "es1", so i changed my Magento2 install command to
--elasticsearch-host='es1:9200'
and it worked for me.
it was --elasticsearch-host='localhost'
before and was not working.