从Docker容器内部向localhost发出请求
我在本地主机的8080端口上运行了一个应用程序。我有一些使用该服务的python代码。该代码在我的基本系统上运行良好,但是一旦将其放入docker容器中,我就会得到 urllib2.URLError:< urlopen错误[Errno 111]连接被拒绝>
。我还有另一个在端口6543公开api的应用程序。同样的问题。
I have an application runing on my localhost at port 8080. I have some python code that consumes that service. The code runs fine on my base system but as soon as I put it inside a docker container I get urllib2.URLError: <urlopen error [Errno 111] Connection refused>
. I have another application that exposes an api at port 6543. Same problem.
我假设我需要告诉docker它允许使用某些本地主机端口。我该怎么做?
I assume I need to tell docker that it's allowed to consume certain localhost ports. How do I do that?
这里有一些更具体的细节:
Here are some more specific details:
我可以执行此行代码在我的基本系统上就很好了:
I can execute this line of code just fine on my base system:
urllib2.urlopen(req, json.dumps(dData))
但是当我尝试从Docker容器内部进行操作时,我得到了:
but when I try to do it from inside a docker container then I get:
File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 431, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 449, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1227, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/lib/python2.7/urllib2.py", line 1197, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 111] Connection refused>
我尝试为docker.sock添加权限
I've tried adding permissions to docker.sock
ls -l /var/run/docker.sock
=> srw-rw-rwx 1 root docker 0 Feb 17 11:09 /var/run/docker.sock
使用参数 -net host
创建python容器,它将与主机共享地址和端口,因此可以访问进度
Create your python container with argument -net host
,it will share address and port with host,so it can access progress which is running on host.
请参阅我的另一个答案: https:// stackoverflow .com / a / 48069763/5465023
Refer to my another answer: https://stackoverflow.com/a/48069763/5465023