I am unable to execute netcat command within docker bash terminal?

Mr.Robot picture Mr.Robot · Sep 29, 2018 · Viewed 12.3k times · Source

I am a beginner in docker .
I have installed docker-ce in my ubuntu 18.04 machine using command
sudo apt install docker-ce
As part of a tutorial , I am trying to establish connection between containers by executing series of below commands.

Below command will turn on ports 1234/4321 to listen to traffic inside/outside of containers i'm going to use.

root@ghost-SVE9999CNS:/home/ghost# docker run --rm -ti -p 1234:1234 -p 4321:4321 --name echo-server ubuntu:18.04 bash

Now, I wanted to run netcat commands within docker bash terminal.

root@xxxyyyyzzzz12:/# nc -lp 1234 | nc -lp 4321

Once i inovke above command from my terminal.. Its giving errors "nc: command not found"

bash: nc: command not found
bash: nc: command not found

Later, I have done enough research and i never found any official docker solution for this problem.

Please could anyone help me out installing netcat within docker-ce.
I've tried commands like below.

apt-get install netstat
apt-get install nc

But, no luck.

Answer

norbjd picture norbjd · Sep 29, 2018

nc is not installed by default on ubuntu:18.04 image, so you have to install it :

apt-get update && apt-get install -y netcat

apt-get update is necessary to first update list of packages (when the container is started, this list is empty). Once done, you can run nc -lp 1234 from the container.

To test all works as you expected, you can then :

  • run from a shell (on your host) something like telnet container_ip 1234 or telnet localhost 1234 (since ports have been forwarded)
  • type something
  • look at the container output to see what you typed in your host shell