在docker容器中运行netcat [英] running netcat inside docker container

查看:958
本文介绍了在docker容器中运行netcat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用以下Dockerfile创建了docker映像。

I have created docker images using the below Dockerfile.

 FROM ubuntu
 RUN apt-get update \
  && DEBIAN_FRONTEND=noninteractive apt-get install -y \
    net-tools \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*
RUN apt-get update \
  && DEBIAN_FRONTEND=noninteractive apt-get install -y \
    netcat \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*
EXPOSE 1234
ENTRYPOINT bin/bash
CMD ["nc", "-l", "1234"]

我从上面的docker文件创建了映像,并通过运行以下命令来使用该映像运行docker容器。

I created image from the above docker file and run the docker container using the image by running below command.

docker run -d  -i -p 1234:1234 --name daemon  nc-ubuntu nc -l 1234

在另一个终端中,运行以下命令。

In another terminal, I run the below command.

telnet localhost 1234

我得到以下输出。

$ telnet localhost 1234
Trying ::1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

我正在尝试此操作,因为这是第二章实践中来自本书docker的示例,配置了将docker作为守护进程运行。

I am trying this as this is sample from book docker in practice in chapter 2 by manning for running docker as daemon process.

根据作者,我应该得到以下结果。

As per author I should get below result.

$ telnet localhost 1234
Trying ::1...
Connected to localhost.
Escape character is '^]'.
hello daemon

任何我为什么没有得到预期输出的想法。

Any idea why I'm not getting expected output.

推荐答案

那是行不通的。 Dockerfile存在一些问题。

That's never going to work. There are several problems with your Dockerfile.

设置 ENTRYPOINT / bin / bash 意味着 docker run ... 只是要开始 bash 。阅读此问题有关 ENTRYPOINT CMD

Setting ENTRYPOINT to /bin/bash means that docker run ... is simply going to start bash. Read this question about ENTRYPOINT and CMD.

由于您处于非交互模式, bash 将立即退出。考虑:

Since you're in non-interactive mode, bash is going to exit immediately. Consider:

host$ docker run nc-ubuntu
host$

Vs:

host$ docker run -it nc-ubuntu
root@e3e1a1f4e453:/# 

后者是因为 -it (分配tty设备,bash在交互模式下需要该设备),得到 bash 提示。

The latter, because of the -it (which allocates a tty device, which bash requires in interactive mode), gets a bash prompt.

这两个调用都不会导致容器运行 netcat ...即使这样做,Dockerfile中的任何内容也不会生成您期望的 hello守护程序响应。

Neither invocation will cause the container to run netcat...and even if it did, nothing in your Dockerfile would generate the hello daemon response you're expecting.

nc 命令行不正确。语法为:

The nc command line is incorrect. The syntax is:

nc -l -p <port>

所以您需要:

CMD ["nc", "-l", "-p", "1234"]



5



如果您确实希望 nc 为您提供 hello daemon 响应,则需要在 nc -c 命令c>命令行,例如:

5

If you actually want nc to provide you with the hello daemon response, you would need to add an appropriate -c command to your nc command line, as in:

CMD ["nc", "-l", "-p", "1234", "-c", "echo hello daemon"]

这使最终的Dockerfile看起来像:

This makes the final Dockerfile look like:

FROM ubuntu
RUN apt-get update \
  && DEBIAN_FRONTEND=noninteractive apt-get install -y \
    net-tools \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*
RUN apt-get update \
  && DEBIAN_FRONTEND=noninteractive apt-get install -y \
    netcat \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*
EXPOSE 1234
CMD ["nc", "-l", "-p", "1234", "-c", "echo hello daemon"]

如果我构建了该文件:

docker build -t nc-ubuntu .

然后运行:

docker run -d  -i -p 1234:1234 --name daemon  nc-ubuntu

然后我可以远程登录到主机上的端口 1234 并查看预期的响应:

I can then telnet to port 1234 on my host and see the expected response:

host$ telnet localhost 1234
Trying ::1...
Connected to localhost.
Escape character is '^]'.
hello daemon
Connection closed by foreign host.

此时,由于 nc 在接受单个连接(不带其他参数)后退出,而Docker在前台进程退出时退出。

At this point, the container will have exited because nc exits after accepting a single connection (without additional parameters), and a Docker contain exits when the foreground process exits.

我无权访问这本书,所以我无法告诉您这是否是本书的问题,或者您在实现中犯了错误,但我建议您使用一些在线Docker教程至少可能同样出色。

I don't have access to the book so I can't tell if this is do to a problem with the book or if you have made a mistake in your implementation, but I would suggest that there are a number of online Docker tutorials that are probably at least as good.

这篇关于在docker容器中运行netcat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆