4
votes

I have a .net core 2.1 api behind an nginx reverse proxy that I set up using docker compose in visual studio. When running, the api is reachable (I have a health check controller I can call to verify) but I can't debug. It looks as if my solution is not running after building. But my containers are up and reachable. I am using visual studio 2019.

This is my folder structure:

  • ...
  • RestApi (project folder folder)
  • Dockerfile
  • docker-compose.yml
  • ReverseProxy (folder)
    • Dockerfile
    • nginx.conf
  • ...

these are the files (from top to bottom in folder structure):

Dockerfile (for the api):

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/aspnet:2.1-stretch-slim AS base
WORKDIR /app
#EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:2.1-stretch AS build
WORKDIR /src
COPY ["RestApi/RestApi.csproj", "RestApi/"]
COPY ["Services/Services.csproj", "Services/"]
COPY ["DataServices/DataServices.csproj", "DataServices/"]
COPY ["Entities/Entities.csproj", "Entities/"]
RUN dotnet restore "RestApi/RestApi.csproj"
COPY . .
WORKDIR "/src/RestApi"
RUN dotnet build "RestApi.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "RestApi.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .

ENV ASPNETCORE_URLS http://+:5000
EXPOSE 5000

ENTRYPOINT ["dotnet", "RestApi.dll"]

docker-compose.yml:

version: '2.1'
services:
  restapi:
    build:
      context: ./
      dockerfile: Dockerfile
    expose:
      - "5000"
    #restart: always
  reverseproxy:
    build:
      context: ./ReverseProxy
      dockerfile: Dockerfile
    ports:
      - "80:80"
    #restart: always
    links :
      - restapi

Dockerfile (reverse proxy):

FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf

nginx.conf:

worker_processes 4;

events { worker_connections 1024; }

http {
    sendfile on;

    upstream app_servers {
        server RestApi:5000;
        #server 172.17.0.1:5000;
    }

    server {
        listen 80;

        location / {
            proxy_pass         http://app_servers;
            proxy_redirect     off;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }
}

When running docker-compose through visual studio the docker containers are created correctly but I can't debug and no browser screen is launched. I get no errors while building or running. If you need extra information please ask.

1

1 Answers

3
votes

I found out what was going wrong. The answer is the solution to the question posted here. https://developercommunity.visualstudio.com/content/problem/552563/debugger-silently-fails-to-attach-to-docker-compos.html

Basically when a dockerfile is NOT next to (adjacent to) the corresponding csproj file (project file), visual studio will not attach the debugger. This is by design because there might be containers that you want to start but not want to debug (reverse proxy, mysql database, etc..). So When I moved my Dockerfile for the api inside the restapi folder (the same folder as the csproj file) and adjusted my docker-compose.yml to look for a dockerfile inside that folder debugging worked in visual studio.