在现有的asp.net核心docker容器上安装nginx [英] Install nginx on an existing asp.net core docker container

查看:68
本文介绍了在现有的asp.net核心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 https://*: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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆