Install nginx on an existing asp.net core docker container

纵然是瞬间 提交于 2019-12-06 11:18:03

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.

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

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