【发布时间】:2018-11-17 00:05:17
【问题描述】:
不幸的是,没有太多关于如何将 Nginx 或 Apache 代理注入 ASP.NET Core Docker 容器的文档。 这是很好的手册,但它为 ASP.NET Core 应用程序和 Nginx 提供了单独的图像。
ASP.NET Core API behind the Nginx Reverse Proxy with Docker.
我想在 Azure 上托管我的 Docker 映像,所以我需要在我的 Docker 容器中安装 Nginx。
基于这篇文章Nginx Reverse Proxy to ASP.NET Core – Same Docker Container我已经创建了这个配置:
nginx.conf
worker_processes 4;
events { worker_connections 1024; }
http {
sendfile on;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
large_client_header_buffers 4 16k;
upstream app_servers {
server 127.0.0.1:5000;
}
server {
listen 80;
location / {
proxy_pass http://app_servers;
proxy_redirect off;
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-Host $server_name;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}
}
}
Dockerfile
FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80
FROM microsoft/aspnetcore-build:2.0 AS build
RUN apt-get update
RUN apt-get install -y apt-utils
RUN apt-get update
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y nginx
WORKDIR /src
COPY MyApp.csproj MyApp.csproj
RUN dotnet restore
COPY . .
WORKDIR /src
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
RUN rm -f /app/startup.sh
COPY startup.sh /app
RUN chmod 755 /app/startup.sh
RUN rm -f /etc/nginx/nginx.conf
COPY nginx.conf /etc/nginx
ENV ASPNETCORE_URLS http://+:5000
EXPOSE 5000 80
CMD ["sh", "/app/startup.sh"]
Startup.sh
#!/bin/bash
service nginx start
dotnet /app/MyApp.dll
仍然收到“服务不可用” 可能是因为我有 Azure AAD 身份验证。 有人可以推荐一些东西或提供另一种工作配置吗?
【问题讨论】:
-
这是一个很好的手册,但它有 ASP.NET Core 应用程序和 Nginx 的单独图像 这正是 IS THE ANSWER 和正确的方法去做吧。每个 docker 容器一个应用程序。您可以使用 docker compose 之类的东西或其他类似的 ochestration 工具来同时启动/部署多个相互依赖的容器。而且你还可以在 azure 上托管多个容器,不知道问题出在哪里。虽然 azure 托管的自然选择是为单个应用程序使用 azure 应用服务
-
其他一切都没有意义。 Docker 不是您在其中运行操作系统的虚拟机,它将首先使 docker 的所有优势过时。 docker 的一大优势是您可以独立部署应用程序(来自 asp.net 核心应用程序的 nginx)并更新一个不重新创建整个图像的应用程序。您想要的听起来更像是创建一个 VM 并在其中运行 多个 服务。为此,您不需要。不要使用 docker,因为它很酷,每个人都在使用它,或者你的简历中有另一个流行词。将 docker 用于它的用途
-
无法将其标记为答案,但似乎是。谢谢你,@Tseng。似乎解决方案是使用这篇文章(将来,当它不在预览中时)Docker Compose deployment support in Azure Service Fabric (Preview) 或 Swarm 或其他东西..
-
太棒了。对于每个容器 1app 上的所有纯粹主义者 - 是的,但有时需要这种方式。这个答案很棒,因为它帮助我实现了这种方法,我相信它可以帮助过去和未来的许多其他人。
-
亚历克斯。你得到它的工作。想一想,您是否尝试过以交互模式进入容器并运行 curl localhost:5000 以查看您的应用程序是否在容器内工作。我也尝试将代理相关的东西复制到 /etc/nginx/sites-available/default
标签: azure docker nginx asp.net-core