Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
运行两个Docker容器(1个反向代理nginx,另一个是dotnetcore应用程序)_Docker_Nginx_.net Core_Docker Compose_Nginx Reverse Proxy - Fatal编程技术网

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

运行两个Docker容器(1个反向代理nginx,另一个是dotnetcore应用程序),docker,nginx,.net-core,docker-compose,nginx-reverse-proxy,Docker,Nginx,.net Core,Docker Compose,Nginx Reverse Proxy,第一次使用docker compose。正在尝试将Nginx容器设置为Web服务器和容纳我的dotnetcore应用程序的容器。目的是让nginx将呼叫传递给Kestrel。生成和运行两个映像,但访问时出错: 项目结构如下: Dockersinger项目 Dockersingleproject/dotnetcore应用程序 *应用程序文件' DockerFile Nginx/ nginx.conf DockerFile docker-compose.yml 我的印象是,问题在于web服务器容器和

第一次使用docker compose。正在尝试将Nginx容器设置为Web服务器和容纳我的dotnetcore应用程序的容器。目的是让nginx将呼叫传递给Kestrel。生成和运行两个映像,但访问时出错:

项目结构如下:

Dockersinger项目 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
        }
    } 
}
events {}
http {
  server {
    listen 80;
    location / {
        proxy_pass http://app:80;
    }
  }
}
服务器正在侦听端口8080,并将请求代理到服务器应用程序上的端口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网关损坏

这使用了Microsoft提供的ASP.NET核心示例:

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核心示例在:80上运行,已公开 Nginx容器也在:80上运行,并在:8080上的主机上公开 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
        }
    } 
}
events {}
http {
  server {
    listen 80;
    location / {
        proxy_pass http://app:80;
    }
  }
}

Nginx侦听:80,因为它的容器需要它 代理配置引用的服务名称应用位于:80 以及:

卷曲\ -沉默的\ -写出%{http_code}\ -输出/dev/null\ http://localhost:8080 200
嘿,我正试图重现你的问题,但在尝试提取dotnet图像时出现错误503。我遗漏了什么吗?@efratevitan您好,dotnet base image语句是由Visual Studio在添加Docker支持时生成的。不确定您为什么会收到503抱歉。您正在主机:4000上公开应用程序,您可能不应该这样做,但因为您是,您可以通过卷曲端点curl-request GET来确认.NET容器是否正常工作http://localhost:4000 从主人那里。如果这样做有效,那么问题就出在您的Nginx配置中。因为您希望Nginx代理您的.NET容器,所以您不需要向主机公开.NET。避免这种情况的一种方法是只使用端口:-:4000,但最好使用expose,因为它不公开任何主机点。在其他反馈中,docker compose现在的版本是:3,使用3,您不需要链接:-app,因为这是服务名称隐含的。您的docker文件容器冗余命令:from。。。不使用as底座。从。。。as build不使用生成引用,因此可以删除as build。看看你在哪里用这个。