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:
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.
If you want to debug in Docker Compose, run the command Docker Compose Up using one of the two Docker Compose files as described in the previous section, and then attach using the appropriate Attach launch configuration. Launching directly using the normal launch configuration does not use Docker Compose.
In the Visual Studio toolbar, make sure the configuration is set to Debug. To start debugging, select the profile name in the toolbar, such as <project profile name>, IIS Express, or <IIS profile name> in the toolbar, select Start Debugging from the Debug menu, or press F5.
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.
Edit2:
Patch for Visual Studio (16.11.6+) was released.
You can docker-compose enable-v2
now.
This answer is now obsolete.
For random googlers, there is new issue (October/November 2021) in docker-compose
which broke Visual Studio 2019 (but not VS2022 preview) debugging with almost the same symptoms as in this question.
The thing is, docker-compose config
will return relative path to Build.Context
and Build.Dockerfile
(it should return absolute path in Build.Context
and relative in Build.Dockerfile
)
Solution right now is docker-compose disable-v2
and Clean & Rebuild your solution until new docker-compose or visual studio patch is released.
For details, see https://github.com/microsoft/DockerTools/issues/311 and https://github.com/docker/compose/issues/8760
Edit:
The next VS update is on Nov 9th [2021]. It will have the fix.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With