[英]Understanding docker compose port wiring for django, react app and haproxy
I came across a docker-compose.yml which has following port configuration:我遇到了一个 docker-compose.yml ,它具有以下端口配置:
wsgi:
ports:
- 9090 // ?? Is it by default mapped to host port 80 ??
nodejs
image: nodejs:myapp
ports:
- 9999:9999
environment:
BACKEND_API_URL: http://aa.bb.cc.dd:9854/api/
haproxy
ports:
- 9854:80
I am trying to understand how the port wiring is happening here.我试图了解端口接线是如何在这里发生的。
nodejs
UI app settings needs to specify backend port which is 9854
here. nodejs
UI 应用设置需要在这里指定后端端口,即9854
。 This port is exposed by haproxy
setting and is mapped to port 80
.此端口由
haproxy
设置公开并映射到端口80
。 I know that wsgi
is a django backend app.我知道
wsgi
是一个 django 后端应用程序。 From its entrypoint.sh
(in PS below) and port specification in above docker-compose.yml
, I get that django listens to port 9090
.从它的
entrypoint.sh
(在下面的 PS 中)和上面docker-compose.yml
中的端口规范,我知道 django 监听端口9090
。 But I am unable to get how this port 9090
maps to port 80
(which is then exposed by haproxy
at 9854
, which in turn is specified in BACKEND_API_URL
by nodejs
settings).但是我无法了解此端口
9090
如何映射到端口80
(然后由haproxy
在9854
公开,而这又由nodejs
设置在BACKEND_API_URL
中指定)。
PS: PS:
Django wsgi app has following in \wsgi\entrypoint.sh
: Django wsgi 应用程序在
\wsgi\entrypoint.sh
中有以下内容:
nohup gunicorn myapp.wsgi --bind "0.0.0.0:9090"
And nodejs react app has following in its server.js
file: nodejs react app 在其
server.js
文件中有以下内容:
const port = process.env.PORT || 9999;
On your host, there are three ports visible:在您的主机上,可以看到三个端口:
http://aa.bb.cc.dd:9854
forwards to port 80 on the haproxy
container. http://aa.bb.cc.dd:9854
转发到haproxy
容器上的 80 端口。http://aa.bb.cc.dd:9999
forwards to port 9999 on the nodejs
container. http://aa.bb.cc.dd:9999
转发到nodejs
容器上的 9999 端口。docker-compose port wsgi 9090
forwards to port 9090 on the wsgi
container. docker-compose port wsgi 9090
显示的端口转发到wsgi
容器上的端口 9090。 You don't discuss the HAProxy configuration at all, but it is presumably configured to listen on port 80, and that may be the missing bit of configuration you're looking for.您根本没有讨论 HAProxy 配置,但它可能配置为侦听端口 80,这可能是您正在寻找的配置中缺少的部分。
Between these three containers (so not visible to your front-end application), assuming you don't have any networks:
blocks in the Compose file, there are three obvious URLs: http://haproxy:80
(or just http://haproxy
), http://nodejs:9999
, and http://wsgi:9090
connect to their respective containers.在这三个容器之间(因此对您的前端应用程序不可见),假设您没有任何
networks:
在 Compose 文件中的块,有三个明显的 URL: http://haproxy:80
(或只是http://haproxy
)、 http://nodejs:9999
和http://wsgi:9090
连接到各自的容器。 Note that these use the "normal" ports for their service, and not the remapped port for haproxy
or the randomly-chosen port for wsgi
.请注意,它们使用“普通”端口进行服务,而不是
haproxy
的重新映射端口或wsgi
的随机选择端口。
I'm guessing the HAProxy container is configured to do some sort of path-based routing to one or the other of the other containers.我猜 HAProxy 容器被配置为对其他容器中的一个或另一个进行某种基于路径的路由。 If you have this setup, you might be able to configure your React application to not include a host name in the URL at all (
BACKEND_API_URL: /api/
), which will make it easier to deploy.如果你有这个设置,你也许可以将你的 React 应用程序配置为在 URL 中完全不包含主机名(
BACKEND_API_URL: /api/
),这将更容易部署。 You do not need the ports:
for connections between containers, and if you don't want a caller to be able to reach the back-end services without going via the proxy, you can delete their ports:
blocks.您不需要
ports:
用于容器之间的连接,并且如果您不希望调用者能够在不通过代理的情况下访问后端服务,您可以删除它们的ports:
块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.