Install nginx on an existing asp.net core docker container

喜夏-厌秋 提交于 2019-12-22 12:18:06

问题


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

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"]

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

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.

Thanks!


回答1:


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.

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

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.

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

EDIT: 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"]

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.




回答2:


It would be better to set at the application gateway level (like Kong, Tyk.io, Aws, Azure) rather than on individual services.

For example Kong uses nginx, you can configure the nginx to do the response compression. Set the following environment variables

  - KONG_NGINX_HTTP_GZIP="on"
  - KONG_NGINX_HTTP_GZIP_TYPES="*" # for all requests, you can add specific mime-type as you want.
  - KONG_NGINX_HTTP_GZIP_PROXIED="any"
  - KONG_NGINX_HTTP_GZIP_MIN_LENGTH="1000"

You can find more details on kong/nginx configuration at https://docs.konghq.com/1.0.x/configuration/#injecting-nginx-directives http://nginx.org/en/docs/http/ngx_http_gzip_module.html



来源:https://stackoverflow.com/questions/51264577/install-nginx-on-an-existing-asp-net-core-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!