microsoft / DockerTools

Tools For Docker, including Visual Studio Provisioning and Publishing
Other
175 stars 26 forks source link

Visual Studio Docker orchestrator: cannot debug when project dockerfile isn't in project directory #441

Closed StormeNet closed 2 months ago

StormeNet commented 2 months ago

Visual Studio does not show a debug option when using a Docker compose project (.dcproj) and the Dockerfile is not in the project folder but up the directory tree.

Here's my file structure:

root
|- docker-compose.yml
|- docker-compose.dcproj
|-- DockerFiles\
|   |- Dockerfile
|-- TestForDocker\
|   |- TestForDocker.sln
|   |-- TestForDocker\
|       |- TestForDocker.csproj

This causes that there's not an Start Debugging option in the "Manage Docker Compose Launch Settings" image

I assumed this could be fixed by adding the following to TestForDocker.csproj:

<DockerfileFile>..\..\DockerFiles\Dockerfile</DockerfileFile>

However this does not seem to work in combination with

<DockerComposeProjectPath>..\..\docker-compose.dcproj</DockerComposeProjectPath>

For reference, here's the docker-compose.yml:

services:
  testfordocker:
    image: ${DOCKER_REGISTRY-}testfordocker
    build:
      context: .
      dockerfile: DockerFiles/Dockerfile
# The StartDebugging becomes visible whith the line below uncommented instead of the one above.
#      dockerfile: TestForDocker/TestForDocker/Dockerfile

And the docker-compose.dcproj:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" Sdk="Microsoft.Docker.Sdk">
  <PropertyGroup Label="Globals">
    <ProjectVersion>2.1</ProjectVersion>
    <DockerTargetOS>Linux</DockerTargetOS>
    <DockerPublishLocally>False</DockerPublishLocally>
    <ProjectGuid>0fa65af5-e860-4946-a460-8973fcc73164</ProjectGuid>
    <DockerLaunchAction>LaunchBrowser</DockerLaunchAction>
    <DockerServiceUrl>{Scheme}://localhost:{ServicePort}/swagger</DockerServiceUrl>
    <DockerServiceName>testfordocker</DockerServiceName>
  </PropertyGroup>
  <ItemGroup>
    <None Include=".dockerignore" />
    <None Include="docker-compose.override.yml">
      <DependentUpon>docker-compose.yml</DependentUpon>
    </None>
    <None Include="docker-compose.yml" />
  </ItemGroup>
</Project>
dbreshears commented 2 months ago

You can add a label to your docker-compose.yml or docker-compose.override.yml to associate the service with the project.

build: 
  blah..blah
labels:
  com.microsoft.visual-studio.project-name: "TestforDocker"

https://github.com/microsoft/DockerTools/issues/355#issuecomment-1355506604

StormeNet commented 2 months ago

Thanks! It would be great if this was in the official documentation.

dbreshears commented 2 months ago

Agreed, we plan to update our docs. Thanks!

StormeNet commented 2 months ago

I hope I don't step on anyone's toes here, but yesterday a colleague showed me how he does this in a one of the competition's IDE (Rider), and it was just setting a path to the docker-compose.yml and selecting which service(s) to attach to and debug. Not even five minutes of work.

Now here I'm working to get the same done in visual studio and it has taken me so far two days. And this only because the docker-compose.yml is up a few directories and so are the dockerfiles. And furthermore, implementing this dcproj way of doing things, forces to update my dockerfiles as the dotnet restore now can't find the *.dcproj which is defined in the solution parent directory, so I need to update those to restore not the solution but the csproj files.

This seems to be a lot of hassle for something so trivial. Hope you guys can simplify this in the future.

dbreshears commented 2 months ago

Thanks for your feedback! We will look into how we can simplify making this association.

fiveisprime commented 1 month ago

I hope I don't step on anyone's toes here, but yesterday a colleague showed me how he does this in a one of the competition's IDE (Rider), and it was just setting a path to the docker-compose.yml and selecting which service(s) to attach to and debug. Not even five minutes of work.

Now here I'm working to get the same done in visual studio and it has taken me so far two days. And this only because the docker-compose.yml is up a few directories and so are the dockerfiles. And furthermore, implementing this dcproj way of doing things, forces to update my dockerfiles as the dotnet restore now can't find the *.dcproj which is defined in the solution parent directory, so I need to update those to restore not the solution but the csproj files.

This seems to be a lot of hassle for something so trivial. Hope you guys can simplify this in the future.

You are absolutely not stepping on toes, and, in fact, I'd love to learn more about how we can improve this. Would you be willing to spend 15 min with me walking through this? I want to completely understand what you're seeing and talk through some ideas on how to improve. If so, you can book time on my calendar here: https://aka.ms/MeetWithMatt