【问题标题】:Connect to a host socket from a docker container从 docker 容器连接到主机套接字
【发布时间】:2019-06-19 21:28:29
【问题描述】:

我有以下 docker 容器

FROM ubuntu:16.04
RUN apt-get update && apt-get install -y curl net-tools netcat nmap
CMD /bin/bash

我使用卷 /tmp mapped 运行它

从主机,我在/tmp 内创建了一个套接字。我用ncat 进行了测试,连接从主机工作。但是,它不适用于 docker 容器。

ncat -U /tmp/uwsgi.sock
Ncat: Connection refused.

我怎样才能做到这一点?

权限:

$ ls -al /tmp/uwsgi.sock
srwxrwxrwx  1 user  wheel  0 Jun 19 23:53 /tmp/uwsgi.sock

【问题讨论】:

  • 您能否提供有关如何创建套接字以及从主机和容器内部运行的所有命令的详细信息。

标签: docker networking wsgi


【解决方案1】:

Ncat: Connection refused. 表示对端已关闭连接,因此您在容器中无法链接到它。

给你一个例子:

docker 主机部分:

shubuntu1@shubuntu1:~$ ncat -lU /tmp/test.sock
hello
world

使用另一个 ssh 会话午餐容器的命令:

shubuntu1@shubuntu1:~$ docker run -it -v /tmp:/tmp ubuntu /bin/bash

容器部分:

root@01f724409cec:/# apt-get update && apt-get install -y curl net-tools netcat nmap
root@01f724409cec:/# ncat -U /tmp/test.sock
hello
world

输入hello,world,任何一边都可以看到,另一边也可以看到。

但如果关闭 docker 主机端的 unix 套接字:

shubuntu1@shubuntu1:~$ ncat -lU /tmp/test.sock
hello
world
^C
shubuntu1@shubuntu1:~$

然后在容器端再次连接:

root@01f724409cec:/# ncat -U /tmp/test.sock
hello
world
^C
root@01f724409cec:/# ncat -U /tmp/test.sock
Ncat: Connection refused.

它将显示Connection refuesd

【讨论】:

  • 它是如何回答这个问题的?显然监听器正在运行,它没有关闭连接。 OP 面临的问题是我们(我也是)无论如何都被拒绝连接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-25
  • 2018-01-09
  • 2017-04-08
  • 1970-01-01
  • 2015-11-28
  • 2016-01-05
相关资源
最近更新 更多