簡體   English   中英

Docker 容器與 nodejs app(NestJS) 不能從其他容器或主機訪問

[英]Docker container with nodejs app(NestJS) is not accessible from both other containers or host

我有一個應用程序,它由幾個 docker 容器組成:nginx、客戶端、管理員、后端和 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;
        }
}

這是回購的鏈接: GitHub

使用 Fastify,對我有用的是:

app.listen(3000, "0.0.0.0")

為避免答案在評論中丟失:使用 Docker 網絡和鏈接的設置看起來是正確的,所以這似乎是過程本身的問題。

在nest.js 進程的啟動中,綁定到與localhost127.0.0.1不同的接口很重要,因為Docker 創建虛擬網絡接口並使用這些接口與進程通信。 因此,即使localhost直接在主機上運行時工作,這也不適用於 Docker 網絡。 該端口只能從容器內部訪問。

所以,而不是

app.listen("localhost:3000");

像這樣綁定到所有接口(當然使用相應的端口號):

app.listen("0.0.0.0:3000");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM