sleemer/docker.dotnet.debug

Consider using volumes

eiximenis opened this issue · 2 comments

Hi,
An option for allowing debugging without needing to recreate the container everytime is to use a volume to share vsdbg and the source code with the container.

Given a multistage Dockerfile, that builds your netcore image and a valid compose file that allows you to start it, you can write another compose file to debug your container:

version: '3.4'

services:
  api:
    image: myapi:dev
    build:
      target: base
    labels:
      - "com.microsoft.visualstudio.targetoperatingsystem=linux"
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - DOTNET_USE_POLLING_FILE_WATCHER=1
    volumes:
      - .:/app
      - ~/.nuget/packages:/root/.nuget/packages:ro
      - ~/vsdbg-core:/vsdbg:ro
    entrypoint: tail -f /dev/null

Key points:

  • Using a bind mount to share the source code with container
  • Using a bind mount to share nuget cache
  • Using a bind mount to share vsdbg with the container (for this to work you need to download vsdbg on your host of course)
  • Stay on "base" stage
  • Redefining entrypoint to "do nothing"

Basically this compose makes my container to be "like the basic image" (base stage), and do nothing. Source code and vsdbg is provided through bind mounts. You can start the container once and "forget about it". Then just need a "docker exec" to run vsdbg on it.

Base stage is the typical initial stage in the Dockerfile:

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
EXPOSE 80
# end of this stage

Hi @eiximenis, thanks for sharing your solution. It's looks great, but the only problem that I can see is that you force everyone having nuget packages and vsdbg at the same path as you have. But that's not always possible. My original approach doesn't make any assumptions about your fs structure.

Hi! ;-)

About nuget packages, what is shared with container is the "nuget package cache folder" which is located in ~/.nuget (%USERPROFILE%\.nuget in win) in almost all nuget installations.

About vsdbg, you're true: need to download and store somewhere in your disk. Location is up to you, just update compose file to reflect your location. I. e. if you are in Windows and have VS installed with Docker Tools you already should have vsdbg in ~/vsdbg/vs2017u5 folder

Thanks!!