Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install nginx on an existing asp.net core docker container

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!

like image 622
NOP-MOV Avatar asked Aug 31 '25 22:08

NOP-MOV


1 Answers

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.

like image 119
Neekoy Avatar answered Sep 03 '25 15:09

Neekoy