简体   繁体   English

从docker容器内部向localhost发出请求

[英]making requests to localhost from inside docker container

I have an application runing on my localhost at port 8080. I have some python code that consumes that service. 我有一个应用程序在我的localhost上运行端口8080.我有一些python代码消耗该服务。 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> . 代码在我的基本系统上运行正常,但是当我把它放在一个docker容器中时,我得到urllib2.URLError: <urlopen error [Errno 111] Connection refused> I have another application that exposes an api at port 6543. Same problem. 我有另一个在端口6543暴露api的应用程序。同样的问题。

I assume I need to tell docker that it's allowed to consume certain localhost ports. 我假设我需要告诉docker它允许使用某些localhost端口。 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))

but when I try to do it from inside a docker container then I get: 但是当我尝试从docker容器内部进行操作时,我得到:

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>

I've tried adding permissions to docker.sock 我已经尝试为docker.sock添加权限

ls -l /var/run/docker.sock      
=> srw-rw-rwx 1 root docker 0 Feb 17 11:09 /var/run/docker.sock

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. 使用参数-net host创建python容器,它将与主机共享地址和端口,因此它可以访问在主机上运行的进度。

Refer to my another answer: https://stackoverflow.com/a/48069763/5465023 请参阅我的另一个答案: https//stackoverflow.com/a/48069763/5465023

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以从正在运行的 Docker 容器内部发送 HTTP 请求 - Is it possible to send HTTP requests from inside a running Docker container 来自 Docker 容器本地主机的 Jupyter 笔记本不起作用 - Jupyter notebook from Docker container localhost not working 从本地主机访问AWS中的Docker容器 - Accessing docker container in AWS from localhost docker 从容器内部构建 - docker build from inside container Docker - 从内部容器运行容器 - Docker - Run Container from Inside Container Python从docker容器内部获取Docker容器名称 - Python getting Docker Container Name from the inside of a docker container 如何从Docker容器内部使用selenium和webdriver访问运行在http://127.0.0.1:8090/的localhost服务器? - From inside of a Docker container, how can I use selenium and webdriver to access the localhost server running at http://127.0.0.1:8090/? Ansible 可以从 localhost 连接到 windows 机器,但不能从 docker 容器连接 - Ansible can connect to windows machine from localhost but not from docker container 如何从Docker容器中的python脚本连接到localhost上的mysql数据库 - How to connect to a mysql database on localhost from a python script in a docker container 无法从Docker容器内部连接到Redis - Unnable to connect to Redis from inside Docker container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM