运行两个Docker容器(一个反向代理Nginx,另一个是dotnetcore应用)

时间:2019-05-12 17:15:40

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

首次使用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应用)
      • * app文件”
      • 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

请让我知道我的困惑所在。我指责我的PC成为问题。任何信息表示赞赏。

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

1 个答案:

答案 0 :(得分:0)

这使用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"

NB

  • ASP.NET Core示例在:80上运行,并已expose
  • Nginx容器也运行在:80上,并在:8080的主机上公开

nginx.conf:

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

NB

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

并且:

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

200