Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 667
Mr.Robot Avatar asked Sep 29 '18 15:09

Mr.Robot


2 Answers

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
like image 106
norbjd Avatar answered Oct 22 '22 05:10

norbjd


It is not necessary to use ubuntu:18.04 to follow the tutorial, you can use ubuntu:14.04 for example, in which nc installed by default.

docker run --rm -ti -p 1234:1234 -p 4321:4321 --name echo-server ubuntu:14.04 bash

like image 29
mrDzurb Avatar answered Oct 22 '22 03:10

mrDzurb