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

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

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