![](/img/trans.png)
[英]Trying to connect to a python socket inside a docker container from host
[英]Connect to a host socket from a docker container
我有以下 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
Ncat: Connection refused.
意味着對端已關閉連接,因此您在容器中無法鏈接到它。
給你一個例子:
碼頭主機部分:
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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.