简体   繁体   English

docker 同一网络上的容器(来自撰写)看不到对方

[英]docker containers on the same net (from compose) don't see each other

I have two docker containers: nginx_server and django_server (with UWSGI ) and a posgres with postgresql .我有两个 docker 容器: nginx_serverdjango_server (带有UWSGI )和一个带有posgrespostgresql Now the thing is, My nginx setup seems to be a bit off, because if gives me a 502 Bad Gateway any time I curl 0.0.0.0:8000 (both on host and inside container).现在的问题是,我的nginx设置似乎有点不对劲,因为如果任何时候我curl 0.0.0.0:8000 (在主机和容器内)都给我一个502 Bad Gateway

django_server is runs on UWSGI and is configured such as: django_server在 UWSGI 上运行,配置如下:

uwsgi --http-socket 0.0.0.0:80 \
        --master \
        --module label_it.wsgi \
        --static-map /static=/app/label_it/front/static \

I'am able to connect to it both outside and inside container and get a proper result.我能够在容器内外连接到它并获得正确的结果。

Inside django_server container I can ping and curl nginx_server with positive result and 502 Bad Gateway .django_server容器中,我可以 ping 和 curl nginx_server ,结果是肯定的和502 Bad Gateway

curl django_server in nginx_server container outputs: nginx_server容器中的curl django_server输出:

<html lang="en">
<head>
  <title>Bad Request (400)</title>
</head>
<body>
  <h1>Bad Request (400)</h1><p></p>
</body>
</html>

(I receive above output in django_server container when curl localhost:80 rather than curl 0.0.0.0:80 / curl 127.0.0.1:80 ) (I receive above output in django_server container when curl localhost:80 rather than curl 0.0.0.0:80 / curl 127.0.0.1:80 )

It seems to me, like nginx_server is not properly requesting data from uwsgi .在我看来,就像nginx_server没有正确地从uwsgi请求数据。

nginx_server configuration: nginx_server配置:

upstream django {
    server django_server:8000;
}

# configuration of the server
server {
    # the port your site will be served on
    listen      0.0.0.0:8000;

    # the domain name it will serve for
    server_name label_it.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    location /static {
        alias /vol/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }
}

I've researched that connection must be set to 0.0.0.0 for container to be accessible from outside, and it is done.我研究过连接必须设置为0.0.0.0才能从外部访问容器,并且完成了。

Another thing, I call django_server in nginx_server by it's container name, which should allow mi for connection on the docker network since I docker-compose up .另一件事,我通过容器名称在nginx_server中调用django_server ,这应该允许 mi 连接到docker network ,因为我docker-compose up

Main problem is: django_server content (requesting for uwsgi ) is not accessible from other containers, but is accessible from a host.主要问题是: django_server内容(请求uwsgi )无法从其他容器访问,但可以从主机访问。

Additionally (with netstat -tlnp those two are availible):此外(使用netstat -tlnp这两个可用):

tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:8001            0.0.0.0:*               LISTEN      -                   

docker-compose.prod.yaml : docker-compose.prod.yaml

version: "3.8"

services:
  db:
    image: postgres
    container_name: postgres
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes:
      - ./private/posgtesql/data/data:/var/lib/postgresql/data
  app:
    build: .
    container_name: django_server
    environment:
      - DJANGO_SECRET_KEY=***
      - DJANGO_DEBUG=False
      - PATH=/app/scripts:${PATH}
      - DJANGO_SETTINGS_MODULE=label_it.settings
    volumes:
      - .:/app
      - static_data:/vol/static
    ports:
      - "8001:80"
    depends_on:
      - db
  nginx:
    container_name: nginx_server
    build:
      context: ./nginx
    volumes:
      - static_data:/vol/static
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./nginx/uwsgi_params:/etc/nginx/uwsgi_params
    ports:
      - "8000:80"
    depends_on:
      - app

volumes:
  static_data:

EDIT:编辑:

According to David Maze, I've changed port mapping to be simplier and easier to track.根据 David Maze 的说法,我已将端口映射更改为更简单且更易于跟踪。

django_server` is now `port 8001:8001
nginx_server` is now `port 8000:8000

Respectively:分别:

django_server uwsgi: --http-socket 0.0.0.0:8001
nginx_server: listen 0.0.0.0:8000 and server django_server:8001

Even tho above changes are applied, issue persist.即使应用了上述更改,问题仍然存在。

You need to make sure all of the port numbers match up.您需要确保所有端口号都匹配。

If you start the back-end service with a command如果使用命令启动后端服务

uwsgi --http-socket 0.0.0.0:80 ...

then cross-container calls need to connect to port 80;那么跨容器调用需要连接到80端口; ports: are not considered here at all (and aren't necessary if you don't want to connect to the container from outside Docker). ports:这里根本不考虑(如果您不想从 Docker 外部连接到容器,则没有必要)。 Your Nginx configuration needs to say你的 Nginx 配置需要说

upstream django {
    server django_server:80; # same port as process in container
}

and in the Compose ports: setting, the second port number needs to match this port并且在 Compose ports:设置中,第二个端口号需要匹配这个端口

services:
  app:
    ports:
      - "8001:80" # second port must match process in container

Similarly, since you've configure the Nginx container to同样,由于您已将 Nginx 容器配置为

listen      0.0.0.0:8000;

then you need to use that port 8000 for inter-container communication and as the second ports: number那么您需要使用该端口 8000 进行容器间通信并作为第二个ports:编号

services:
  nginx:
    ports:
      - "8000:8000" # second port must match "listen" statement

(If you use the Docker Hub nginx image it by default listens on the standard HTTP port 80, and so you'd need to publish ports: ["8000:80"] to match that port number.) (如果您使用Docker 集线器nginx映像,则默认情况下它会在标准 HTTP 端口 80 上侦听,因此您需要发布ports: ["8000:80"] 0)

Solution: Official UWSGI site https://uwsgi-docs.readthedocs.io/en/latest/Options.html described a uwsgi-socket connection.解决方案:UWSGI 官方网站https://uwsgi-docs.readthedocs.io/en/latest/Options.html描述了一个 uwsgi uwsgi-socket连接。

Nginx is capable of connecting to uwsgi based on uwsgi_params , but then socket in uwsgi configuration must match the type of request. Nginx 能够基于uwsgi_params连接到 uwsgi,但是 uwsgi 配置中的socket必须匹配请求的类型。 My explanation is a bit messy, but it did work.我的解释有点混乱,但确实有效。

My uwsgi.ini file for uwsgi configuration:我用于uwsgi配置的uwsgi.ini文件:

[uwsgi]
uwsgi-socket = 0.0.0.0:8001 <- this line solved the issue
master = true
module = label_it.wsgi:application
chdir = /app/label_it <- important
static-map = /static=/app/label_it/front/static <- might not be neccessery
chmod-socket = 666 <- might not be neccessery

ngxing default.conf file: ngxing default.conf文件:

upstream django {
    server django_server:8001;
}

# configuration of the server
server {
    # the port your site will be served on
    listen      0.0.0.0:8000;

    server_name label_it.com;
    charset     utf-8;


    client_max_body_size 75M;

    location /static {
        alias /vol/static; # your Django project's static files - amend as required
    }

    
    location / {
        uwsgi_pass  django;
        include     /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
    }
}

My uwsgi version is: uwsgi --version: 2.0.19.1我的uwsgi版本是: uwsgi --version: 2.0.19.1

Nginx installed with official docker hub image: nginx version: nginx/1.19.7 Nginx 安装官方docker hub镜像: nginx version: nginx/1.19.7

Solution: add uwsgi-socket = 0.0.0.0:some_port to uwsgi.ini解决方法:在uwsgi.ini中添加uwsgi.ini uwsgi-socket = 0.0.0.0:some_port

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM