gpt4 book ai didi

node.js - 无法从其他容器或主机访问带有 nodejs 应用程序(NestJS)的 Docker 容器

转载 作者:行者123 更新时间:2023-12-05 00:54:31 28 4
gpt4 key购买 nike

我有一个应用程序,它由几个 docker 容器组成:nginx、client、admin、backend 和 mongo。

在容器中,“后端”在端口 5000 上运行 NestJS 应用程序。容器暴露了端口 5000。但是容器没有响应任何请求,并且容器内的应用程序没有收到它们。我什至尝试将端口 5000 暴露给我的本地机器,这样我就可以在 docker-host 之外发出请求,但是这样容器也不会响应。当我在我的机器上本地运行这个 NestJS 应用程序时,一切正常。我有 nginx.conf 来配置行为 nginx 容器。它应该使用代理将某些请求重定向到特定容器。这种方法适用于客户端和管理容器。托管 NextJS 应用程序和监听特定端口。我对“后端”容器使用了相同的方法,但即使 nginx 似乎发出了正确的请求,它也没有收到响应,或者由于某种原因它向 docker-host 内部的错误地址发出请求

我的自定义图像的 Dockerfile:

FROM node:14.15.4 as client
WORKDIR /usr/src/app
COPY /src/client/package*.json ./
RUN npm install
COPY /src/client .
EXPOSE 3000
CMD ["npm", "run", "dev"]

FROM node:14.15.4 as admin
WORKDIR /usr/src/app
COPY /src/admin/package*.json ./
RUN npm install
COPY /src/admin .
EXPOSE 3001
CMD ["npm", "run", "dev"]

FROM node:14.15.4 as backend
WORKDIR /usr/src/app
COPY /src/app/package*.json ./
RUN npm install
COPY /src/app .
EXPOSE 5000
CMD ["npm", "run", "start:dev"]

docker-compose.yml:

version: '3'
services:
nginx:
image: nginx:${NGNIX_VERSION}
depends_on:
- client
- admin
links:
- client:client
- admin:admin
- backend:backend
restart: on-failure:30
volumes:
- ./deploy/shared/config/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
env_file:
- .env
networks:
- default
expose:
- 80
ports:
- ${NGINX_BIND_PORT}:80
mongo:
image: mongo:${MONGO_VERSION}
env_file:
- .env
networks:
- default
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD}
client:
build:
context: .
target: client
networks:
- default
volumes:
- ./src/client:/usr/src/app
admin:
build:
context: .
target: admin
networks:
- default
volumes:
- ./src/admin:/usr/src/app
backend:
build:
context: .
target: backend
networks:
- default
volumes:
- ./src/app:/usr/src/app
ports:
- 5000:5000

networks:
default:
driver: bridge

nginx.conf:

upstream docker-client {
server client:3000;
}

upstream docker-admin {
server admin:3001;
}

upstream docker-backend {
server backend:5000;
}

server {
listen 80;
server_name mr0bread.local;

location / {
proxy_pass http://docker-client;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_read_timeout 600s;
}

location /admin {
proxy_pass http://docker-admin;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_read_timeout 600s;
}

location /backend {
proxy_pass http://docker-backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_read_timeout 600s;
}
}

这里是 repo 的链接:GitHub

最佳答案

使用 Fastify,对我有用的是:

app.listen(3000, "0.0.0.0")

关于node.js - 无法从其他容器或主机访问带有 nodejs 应用程序(NestJS)的 Docker 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66086427/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com