debugging doesn't work for docker compose with visual studio

Viewed 8087

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.

3 Answers

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.

I am adding this not as a solution to this problem but it may help some other people.

My problem was, in short, a space in my solution name.

I have a large solution that was originally started on VS Windows. My only guess is that on windows you are allowed to have a space in the solution file name. So mine was like "My App.sln". I had no problems running everything on VS Mac until I added docker support for a project.

Debugging my docker-compose project would do everything as expected except start the app and attach the debugger. It would start the container fine and it would be running in docker desktop. If I ran the entrypoint manually in my /app/bin folder I could start the app and it worked. So, since there was no error info it was hard to diagnose.

Finally I got annoyed and just tried to file new project and added docker support. It worked and debugged fine. Then I tried with a space in the solution name and VS Mac said "no way dude" illegal character in the name. So, it pointed me at fixing my real project.

It would have been super duper nice if somewhere in the tooling it would at least throw a warning for a space (or any illegal character) that is in the path or filename that would prevent docker from working. I literally wasted over 15 hours on trying to figure this out.

Related