在现有的asp.net核心docker容器上安装nginx

编程入门 行业动态 更新时间:2024-10-24 06:26:01
本文介绍了在现有的asp核心docker容器上安装nginx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我当前正在为ASP.NET Core WebAPI项目运行一个docker容器. 该Web服务当前在端口80上公开.

I'm currently running a docker container for an ASP.NET Core WebAPI project. This web service is currently exposed on port 80.

我的dockerfile看起来像这样:

My dockerfile looks like this:

FROM microsoft/dotnet:2.1-aspnetcore-runtime ARG source WORKDIR /app EXPOSE 80 COPY ${source:-obj/Docker/publish} . ENTRYPOINT ["dotnet", "testapi.dll"]

我想在服务和公开的公共网络之间安装nginx作为层.

I'd like to install nginx as a layer between the service and the exposed public network.

最终结果应该是ASP.NET运行在仅内部端口90上,并且nginx Web服务器将本地90转发到公共80端口.

The final result should be ASP.NET running on port 90 which is internal only and an nginx webserver forwarding the local 90 to public 80 port.

这可能吗?如果是这样,我如何实现这种架构?顺便说一句,我完全知道由额外的层引起的开销.

Is this possible? if so, how can I achieve such architecture? btw, I'm completely aware of the overhead caused by an extra layer.

谢谢!

推荐答案

Docker做到这一点的方法是为两个服务提供两个容器. Docker-compose是可帮助管理多容器应用程序的工具.

The Docker way to do this would be to have two containers for the two services. Docker-compose is a tool that helps manage multi-container applications.

以下docker-compose.yml应该为您工作:

The following docker-compose.yml should work for you:

version: '3' services: app: build: context: ./dotnet dockerfile: Dockerfile expose: - "80" proxy: build: context: ./nginx dockerfile: Dockerfile ports: - "80:80" links: - app

基本上,我们为您的应用程序公开端口,以便Nginx容器可以访问它,然后将Nginx容器上的端口绑定到主机上的端口.

Basically we expose the port for your app so the Nginx container can access it, and then we bind the port on the Nginx container to the one on your host machine.

您将Dockerfile和应用程序放置在"dotnet"文件夹中,并将您的NginX逻辑放置在/nginx/Dockerfile文件中.

You put your Dockerfile and app in the "dotnet" folder, and your NginX logic in the /nginx/Dockerfile file.

如果您真的想将两个服务都放在一个容器中,则可以使用以下命令:

If you would really like to have both services in a single container you can use the following:

FROM microsoft/dotnet:2.1-aspnetcore-runtime ENV ASPNETCORE_URLS *:8080 RUN apt update && \ apt install nginx ADD nginx-website.conf /etc/nginx/sites-enabled RUN service nginx restart ARG source WORKDIR /app EXPOSE 80 COPY ${source:-obj/Docker/publish} . ENTRYPOINT ["dotnet", "testapi.dll"]

这时,您只需要创建Nginx配置文件"nginx-website.conf"并为端口80到8080设置反向代理.

At this point you just need to create the Nginx config file "nginx-website.conf" and set up the reverse proxy for port 80 to 8080.

更多推荐

在现有的asp.net核心docker容器上安装nginx

本文发布于:2023-11-17 04:38:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608852.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   核心   net   asp   docker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!