Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Communicate two containers in docker with netcat

I want to communicate two containers in docker, I'm using netcat for the test. First I have created the Dockefile:

FROM ubuntu
WORKDIR /root
RUN apt-get update && apt-get install netcat iputils-ping -y

And added the image with:

docker build . -t ubuntu_netcat

Also I have created a new network:

docker network create --driver bridge nettest

Then I run two containers:

docker run --net=nettest --expose=8080 -it --name pc1 ubuntu_netcat
docker run --net=nettest --link=pc1 -it --name pc2 ubuntu_netcat

At first container (pc1) I listen on port 8080 with netcat command:

nc -vlk 8080

And I expect to communicate with it from the second container (pc2) executing:

nc -v pc1 8080

But I just got a connection refused:

root@c592b2015439:~# nc -v pc1 8080
pc1.nettest [172.18.0.2] 8080 (?) : Connection refused

I have been looking at the docker docs but all seems to be correct. In fact I can perform a ping between containers sucessfully, so they can reach one other, but I have something wrong with ports. ¿What am I doing wrong?

Thanks

like image 686
Eidansoft Avatar asked Jun 22 '17 14:06

Eidansoft


1 Answers

It looks like this version of netcat on Ubuntu doesn't listen like it normally does. You have to specify -p for the port (even though the options would appear to have port as a positional option).

Your netcat listener command should be:

nc -vlkp 8080
like image 72
Andy Shinn Avatar answered Sep 30 '22 07:09

Andy Shinn