从Docker容器连接到主机套接字

时间:2019-06-19 21:28:29

标签: docker networking wsgi

我有以下docker容器

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

我在映射卷/tmp的情况下运行了

在主机上,我在/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

1 个答案:

答案 0 :(得分: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

输入helloworld时,您可以在任何一侧看到,另一侧也可以看到。

但是如果关闭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

相关问题