Docker应用服务器的IP地址127.0.0.1的差异为0.0.0.0 ip [英] Docker app server ip address 127.0.0.1 difference of 0.0.0.0 ip

查看:885
本文介绍了Docker应用服务器的IP地址127.0.0.1的差异为0.0.0.0 ip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他大家。我正在使用docker并尝试将一个简单的django应用程序进行dockerize,该应用程序执行一个外部http连接到网页(真实网站)
,因此当我在Docker文件中设置应在其中工作的django服务器的地址时容器- 127.0.0.1:8000 。由于无法建立与网站的外部连接,我的应用无法正常工作。

He everyone. I'm working with docker and trying to dockerize a simple django application that does an external http connect to a web page (real website) so when I set in the Docker file the address of my django server that should work in the container - 127.0.0.1:8000. my app wasn't working because of the impossibility to do an external connection to the website.

但是当我为服务器设置端口时: 0.0.0.0 :8000 开始工作。

but when I set the port for my server: 0.0.0.0:8000 it started to work.

所以我的问题是:为什么会这样呢?在这种情况下有什么区别?我只是想了解它。

So my question is: Why it behaves like that? What is the difference in this particular case? I just want to understand it.

我读了一些有关0.0.0.0的文章,就像一个通用或占位符端口,允许使用OC默认端口

I read some articles about 0.0.0.0 and it's like a 'generic' or 'placeholder' port that allows to use the OC default port.

127.0.0.1就像是将请求重定向到当前计算机的主机。我就知道。
但是,当我在本地计算机(主机:127.0.0.0:8000)上运行该应用程序时,一切正常,该应用程序可以连接到真实网站,但是在docker的情况下,它停止工作。

127.0.0.1 is like a host that redirects the request to the current machine. I knew it. But when I run the app at my localmachine (host: 127.0.0.0:8000) everything was working and the app could do the connection to the real website but in case of docker it stopped to work.

谢谢您的帮助!

以下是我的资料来源:
Docker文件

Here are my sources: Docker file

FROM python:3.6

RUN mkdir /code
WORKDIR /code

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . ./

EXPOSE 8000

ENTRYPOINT [ "python", "manage.py" ]
CMD [ "runserver", "127.0.0.1:8000" ] # doesn't work
# CMD [ "runserver", "0.0.0.0:8000" ] - works

docker-compose.yml

docker-compose.yml

version: "3"
services:
  url_rest:
    container_name: url_keys_rest
    build:
      context: .
      dockerfile: Dockerfile
    image: url_keys_rest_image
    stdin_open: true
    tty: true
    volumes:
      - .:/var/www/url_keys
    ports:
      - "8000:8000"

这是我收到的http错误如果是127.0.0.1。

here is the http error that I received in case of 127.0.0.1. Maybe it will be useful.

http: error: ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=8000): Max retries exceeded with url: /api/urls (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x10cd51e80>: Failed to establish a new connection: [Errno 61] Connection refused')) while doing GET request to URL: http://127.0.0.1:8000/api/urls


推荐答案

您必须将容器的主进程设置为绑定到特殊的0.0.0.0所有接口地址,否则它将无法从容器外部访问。

You must set a container’s main process to bind to the special 0.0.0.0 "all interfaces" address, or it will be unreachable from outside the container.

在Docker 127.0.0.1中,几乎总是表示此容器,而不是此计算机。如果您从容器建立到127.0.0.1的出站连接,它将返回到同一容器;如果将服务器绑定到127.0.0.1,它将不接受来自外部的连接。

In Docker 127.0.0.1 almost always means "this container", not "this machine". If you make an outbound connection to 127.0.0.1 from a container it will return to the same container; if you bind a server to 127.0.0.1 it will not accept connections from outside.

Docker的核心功能之一是给每个容器自己的单独的网络空间。特别是,每个容器都有自己的 lo 接口和自己的 localhost 的概念。

One of the core things Docker does is to give each container its own separate network space. In particular, each container has its own lo interface and its own notion of localhost.

在非常低的级别,网络服务调用 bind (2)系统调用以开始接受连接。这需要一个地址参数。它可以是两件事之一:可以是某些系统接口的IP地址,也可以是特殊的0.0.0.0所有接口地址。如果选择一个接口,它将仅接受来自该接口的连接;例如,如果您在一个物理系统上有两个网卡,则可以使用它仅接受来自一个网络的连接,而不能接受来自另一个网络的连接。

At a very low level, network services call the bind(2) system call to start accepting connections. That takes an address parameter. It can be one of two things: either it can be the IP address of some system interface, or it can be the special 0.0.0.0 "all interfaces" address. If you pick an interface, it will only accept connections from that interface; if you have two network cards on a physical system, for example, you can use this to only accept connections from one network but not the other.

因此,如果设置了服务绑定到127.0.0.1,这是 lo 接口的地址,并且该服务将仅接受来自该接口的连接。但是每个容器都有自己的 lo 接口和自己的 localhost ,因此此设置将导致服务拒绝连接,除非它们从容器本身内部启动。如果将其设置为绑定到0.0.0.0,它还将接受来自每个容器 eth0 接口的连接,容器外部的所有连接都将到达。

So, if you set a service to bind to 127.0.0.1, that’s the address of the lo interface, and the service will only accept connections from that interface. But each container has its own lo interface and its own localhost, so this setting causes the service to refuse connections unless they’re initiated from within the container itself. It you set it to bind to 0.0.0.0, it will also accept connections from the per-container eth0 interface, where all connections from outside the container arrive.

这篇关于Docker应用服务器的IP地址127.0.0.1的差异为0.0.0.0 ip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆