Docker app server 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

查看:25
本文介绍了Docker app server ip 地址 127.0.0.1 与 0.0.0.0 ip 之差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他每个人.我正在使用 docker 并尝试 dockerize 一个简单的 django 应用程序,该应用程序将外部 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"

这是我在 127.0.0.1 的情况下收到的 http 错误.也许会有用.

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 app server ip 地址 127.0.0.1 与 0.0.0.0 ip 之差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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