我无法在 docker bash 终端中执行 netcat 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52570028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 17:01:43  来源:igfitidea点击:

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

bashdockerubuntunetcat

提问by Nandan S V

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.

我是 docker 的初学者。
我已经使用命令在我的 ubuntu 18.04 机器上安装了 docker-ce
sudo apt install docker-ce
作为教程的一部分,我试图通过执行以下一系列命令来建立容器之间的连接。

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

下面的命令将打开端口 1234/4321 以侦听我将要使用的容器内部/外部的流量。

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.

现在,我想在 docker bash 终端中运行 netcat 命令。

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

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

一旦我从我的终端输入上面的命令..它给出错误“nc:找不到命令”

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.

后来,我做了足够的研究,但我从来没有找到任何官方的 docker 解决方案来解决这个问题。

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

请谁能帮我在docker-ce中安装netcat。
我试过如下命令。

apt-get install netstat
apt-get install nc

But, no luck.

但是,没有运气。

回答by norbjd

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

nc默认情况下未在ubuntu:18.04图像上安装,因此您必须安装它:

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

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

apt-get update需要先更新包列表(当容器启动时,这个列表是空的)。完成后,您可以nc -lp 1234从容器运行。

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

要按预期测试所有工作,您可以:

  • run from a shell (on your host) something like telnet container_ip 1234or telnet localhost 1234(since ports have been forwarded)
  • type something
  • look at the container output to see what you typed in your host shell
  • 从shell(在您的主机上)运行类似telnet container_ip 1234telnet localhost 1234(因为端口已转发)
  • 输入一些东西
  • 查看容器输出以了解您在主机 shell 中键入的内容