Python 同一网络上的docker容器(来自compose)don';我看不见对方
我有两个docker容器:Python 同一网络上的docker容器(来自compose)don';我看不见对方,python,django,docker,networking,docker-compose,Python,Django,Docker,Networking,Docker Compose,我有两个docker容器:nginx_服务器和django_服务器(带有UWSGI)和一个posgres带有postgresql。现在的问题是,我的nginx设置似乎有点不对劲,因为每当我curl0.0.0.0:8000(在主机和容器内部)时,if都会给我一个502坏网关 django_服务器在UWSGI上运行,配置如下: uwsgi --http-socket 0.0.0.0:80 \ --master \ --module label_it.wsgi \
nginx_服务器
和django_服务器
(带有UWSGI
)和一个posgres
带有postgresql
。现在的问题是,我的nginx
设置似乎有点不对劲,因为每当我curl0.0.0.0:8000
(在主机和容器内部)时,if都会给我一个502坏网关
django_服务器
在UWSGI上运行,配置如下:
uwsgi --http-socket 0.0.0.0:80 \
--master \
--module label_it.wsgi \
--static-map /static=/app/label_it/front/static \
我能够连接到它的外部和内部容器,并得到一个适当的结果
在django_服务器
容器内,我可以ping和curlnginx_服务器
,结果为正,502坏网关
nginx\u服务器中的curl django\u服务器
容器输出:
错误请求(400)
错误请求(400)
(当curl localhost:80
而不是curl 0.0.0:80
/curl 127.0.0.1:80时,我在django_服务器
容器中接收上述输出)
在我看来,就像nginx\u服务器
没有正确地从uwsgi
请求数据一样
nginx\u服务器
配置:
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
}
}
[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
我已经研究过,必须将连接设置为0.0.0
,才能从外部访问容器,并且已经完成了
另一件事,我在nginx_服务器
中调用django_服务器
,它的容器名应该允许mi连接到docker网络
,因为Idocker组合
主要问题是:
django_服务器
内容(请求uwsgi
)无法从其他容器访问,但可以从主机访问
另外(对于netstat-tlnp
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:
编辑:
根据David Maze的说法,我已将端口映射更改为更简单、更易于跟踪
django_server` is now `port 8001:8001
nginx_server` is now `port 8000:8000
分别为:
django_server uwsgi: --http-socket 0.0.0.0:8001
nginx_server: listen 0.0.0.0:8000 and server django_server:8001
即使应用了上述更改,问题仍然存在。您需要确保所有端口号都匹配
如果您使用命令启动后端服务
uwsgi--http套接字0.0.0.0:80。。。
然后跨容器调用需要连接到端口80<代码>端口:
在这里根本不被考虑(如果您不想从Docker外部连接到容器,则不需要这些端口)。您的Nginx配置需要说明
upstream django {
server django_server:80; # same port as process in container
}
在Composeports:
设置中,第二个端口号需要与此端口匹配
服务:
应用程序:
端口:
-“8001:80”#第二个端口必须与容器中的流程匹配
类似地,由于您已经将Nginx容器配置为
listen 0.0.0.0:8000;
然后,您需要将该端口8000用于集装箱间通信,并作为第二个端口:
编号
服务:
nginx:
端口:
-“8000:8000”#第二个端口必须与“侦听”语句匹配
(如果使用默认设置,it将侦听标准HTTP端口80,因此需要发布端口:[“8000:80”]
以匹配该端口号。)
解决方案:
UWSGI官方网站
描述了一个uwsgi插座
连接
Nginx能够基于uwsgi_参数
连接到uwsgi,但uwsgi配置中的socket
必须与请求类型匹配。我的解释有点混乱,但确实有效
我的uwsgi.ini
文件用于uwsgi
配置:
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
}
}
[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
我的uwsgi
版本是:uwsgi--version:2.0.19.1
Nginx安装了官方的docker hub
image:Nginx版本:Nginx/1.19.7
解决方案:将uwsgi socket=0.0.0.0:some_port
添加到uwsgi.ini
谢谢你简短而精彩的解释!我学到了一些新东西。根据您的答案应用更改。遗憾的是,问题并没有得到解决(请参见编辑)。此外,在dubugging时,我尝试完全删除django_服务器中的端口:-8001:8001
,因为我认为它可能会阻止uwsgi
套接字,但仍然没有任何更改。