【问题标题】:Running two Docker containers (1 reverse proxy nginx, the other a dotnetcore app)运行两个 Docker 容器(一个反向代理 nginx,另一个是 dotnetcore 应用程序)
【发布时间】:2019-05-12 17:15:39
【问题描述】:

第一次使用 docker-compose。尝试将 Nginx 容器设置为 Web 服务器和包含我的 dotnetcore 应用程序的容器。目的是让 nginx 将调用传递给 Kestrel。两个镜像都构建并运行,但在访问“http://localhost:8080”时出错:

proxy_1  | 2019/05/12 16:39:45 [error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://172.19.0.2:4000/", host: "localhost:8080"

项目结构如下:

  • Dockersingleproject
    • Dockersingleproject/(dotnetcore 应用程序)
      • *应用文件'
      • DockerFile
    • Nginx/
      • nginx.conf
      • DockerFile
    • docker-compose.yml

我的印象是问题在于 Web 服务器容器和应用程序容器之间的连接拒绝,但我不知道为什么。下面是应用的 Dockerfile:

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build

WORKDIR /Dockersingleproject
COPY bin/Debug/netcoreapp2.2/publish .

ENV ASPNETCORE_URLS http://+:4000
EXPOSE 4000

ENTRYPOINT ["dotnet", "Dockersingleproject.dll"]

应用程序 docker 文件暴露了 4000 端口。nginx.conf:

worker_processes 1;

events { worker_connections 1024; }

http {

    sendfile on;

    upstream docker-nginx {
        server app:4000;
    } 

    server {
        listen 8080;

        location / {
            proxy_pass         http://docker-nginx;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_cache_bypass $http_upgrade;
            proxy_buffers         8 16k;  # Buffer pool = 8 buffers of 16k
            proxy_buffer_size     16k;    # 16k of buffers from pool used for headers
        }
    } 
}

服务器正在侦听端口 8080,并将请求代理到服务器“app”上的端口 4000。在 docker-compose 文件中定义:

version: '2'

services:
  app:
    build:
      context:  ./Dockersingleproject
      dockerfile: Dockerfile
    ports:
      - "4000:4000"

  proxy:
    build:
      context:  ./nginx
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    links:
      - app

应用服务将端口 4000 请求映射到 4000,在我看来这应该可以工作。

nginx容器的IP为:172.19.0.3 应用容器的IP为:172.19.0.2

请让我知道我的困惑所在。我正要指责我的电脑是问题所在。任何信息表示赞赏。

访问站点时拒绝连接导致 nginx 502 网关错误

【问题讨论】:

  • 嘿,我试图重现您的问题,但在尝试提取 dotnet 图像时出现错误 503。我错过了什么吗?
  • @EfratLevitan 您好,dotnet 基础映像语句是由 Visual Studio 在添加 Docker 支持时生成的。不知道为什么会收到 503 抱歉。
  • 您在主机的:4000 上暴露了app(您可能不应该这样做),但是因为您是这样,所以您可以通过 curl 确认 .NET 容器正常工作从主机获取端点 (curl --request GET http://localhost:4000)。如果可行,那么问题出在您的 Nginx 配置中。
  • 因为您打算让 Nginx 代理您的 .NET 容器,所以您不需要将 .NET 暴露给主机。避免这种情况的一种方法是只使用ports: - ":4000" but it's better to use expose',因为这不会暴露任何主机点。在其他反馈中,docker-compose 现在位于 version: 3 并且使用 3 您不需要 links: - app,因为这是隐含在服务名称中的。
  • 您的 Dockerfile 容器冗余命令:FROM ... as base 未使用。并且FROM ... as build 不使用build 引用,因此可以删除as build。请参阅 multi-stage builds 了解您将在哪里使用它。

标签: docker nginx .net-core docker-compose nginx-reverse-proxy


【解决方案1】:

这使用 Microsoft 提供的 ASP.NET Core 示例:

docker-compose.yaml:

version: "3"

services:

  app:
    image: mcr.microsoft.com/dotnet/core/samples:aspnetapp
    expose:
    - "80"

  proxy:
    image: nginx
    volumes:
    - ./nginx.conf:/etc/nginx/nginx.conf
    ports:
    - "8080:80"

注意

  • ASP.NET Core 示例在 :80 上运行并且是 expose'd
  • Nginx 容器也在:80 上运行,并暴露在:8080 上的主机上

nginx.conf:

events {}
http {
  server {
    listen 80;
    location / {
        proxy_pass http://app:80;
    }
  }
}

注意

  • Nginx 监听 :80 因为它的容器需要它
  • 代理配置引用:80上的服务名称(app

还有:

curl \
--silent \
--write-out "%{http_code}" \
--output /dev/null \
http://localhost:8080

200

【讨论】:

    猜你喜欢
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 2018-07-28
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    相关资源
    最近更新 更多