Communicate two containers in docker with netcat
Asked Answered
S

1

6

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

Sada answered 22/6, 2017 at 14:13 Comment(2)
Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See What topics can I ask about here in the Help Center. Perhaps Super User or Unix & Linux Stack Exchange would be a better place to ask.Natterjack
This helped me with a docker network question to link two Docker containers. Did you follow some blog to come up with this example, or come up with your own here?Gelb
A
18

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
Annual answered 22/6, 2017 at 16:58 Comment(2)
You are right ! I was thinking it was docker configuration problem but the problem was at the netcat version. Thank you!Sada
Avoid this and other similar issues by using netcat-openbsdHybris

© 2022 - 2024 — McMap. All rights reserved.