Achilleus
Achilleus

Reputation: 1944

Docker network in MAC

I have 2 docker containers in the same network. I have created a network by :

docker network create my_network 

I am running a Landoop container in this network using:

docker run --rm -it -p 2181:2181 -p 3030:3030 -p 8081:8081 -p 8082:8082 
 -p 9092:9092 --net=my_network --name localkafka landoop/fast-data-dev

And I am running one more container using :

docker run -it --rm --net=my_network --name containerB 
containerName.

When i login inside containerB and try to ping localkafka using :

container ping -c 5 localkafka

It succeeds. But when I do

ping -c 5 localkafka:8081
ping: unknown host

What am I missing here? Any help is appreciated.

PS: I am using a MAC and I have to use the ports exposed by 1 container in another container B .

Upvotes: 0

Views: 249

Answers (2)

Cole Tierney
Cole Tierney

Reputation: 10314

If you're using a mac, you probably have netcat (nc) installed. Netcat can be used to test if a port is open or not:

nc -zw2 localkafka 8081 &>/dev/null && echo open || echo closed

The -z option makes netcat connect without transferring data. The -w option is tells netcat to timeout after 2 seconds if no connection can be established.

If netcat isn't available, /dev/tcp can be used directly to test if a port is open:

{ echo > /dev/tcp/localkafka/8081; } > /dev/null 2>&1 && echo open || echo closed

Upvotes: 0

iheanyi
iheanyi

Reputation: 3113

Your issue is that you're misusing ping.

Read this: https://technet.microsoft.com/en-us/library/cc732509(v=ws.10).aspx

Short answer - ping works via ICMP echo requests. You cannot ping a port because ports are a concept in transport layer protocols like TCP. So, localkafka:8080 is treated as a host with that name, not a host:port pair.

You can use nmap instead or look for a third party application that behaves like ping but over a transport layer protocol.

Upvotes: 2

Related Questions