microsoft / DockerTools

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

Generating a Dockerfile from VS with CPM enabled omits COPY for package props files #439

Open oising opened 1 month ago

oising commented 1 month ago

When using Central Package Management, the dockerfile generated is missing the required COPY statements for Directory.Build.props (contains the use CPM directive) and Directory.Packages.props. I'm generating the dockerfile with the standard "add new item... / docker support..." menu. I leave the defaults as is: linux, net8, dockerfile etc.

I tried declaring use CPM directly in the csproj but it also fails to add the requisite COPY statements in the dockerfile.

I'm using:

dotnet 8.0 Visual Studio (17.10) Microsoft.VisualStudio.Azure.Containers.Tools.Targets(1.21.0) nuget (6.10) -- not sure if this has anything to do with anything

Directory.Build.props:

<Project>
  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
  </PropertyGroup>
</Project>

Adding the following lines to our dockerfile(s) "fixes" the issue:

COPY ["Directory.Build.props", "Directory.Build.props"]
COPY ["Directory.Packages.props", "Directory.Packages.props"]
oising commented 1 month ago

@patverb said:

@oising I was just checking, and it looks like we have do have a bug where if the Directory.Build.props file is in the same directory as the build context and project, we won't add the COPY statement. I'll get a fix out for that in 17.12.

Is that the same layout you have in your project that doesn't work or could you tell me the layout of your project with the locations of the solution, project, docker build context, and Directory.Build.props file please and I'll try to repo it.

The structure is like this:

- /
   - foo.sln
   - Directory.Build.props
   - Directory.Packages.props
   - groupingfolder/
      - projectfolder1/
        - project1.csproj
        - Dockerfile
      - projectfolder2/
        - project2.csproj
        - Dockerfile

yaml template (azure devops)

parameters:
- name: projectName
  type: string
  default: null
- name: imageRepository
  type: string
  default: null

steps:
- task: DownloadPipelineArtifact@2
  displayName: Download Artifact
  inputs:
    buildType: 'current'
    targetPath: '$(Agent.BuildDirectory)'

- task: Docker@2
  displayName: Docker Build&Push Image
  inputs:
    containerRegistry: '<registryname>'
    repository: ${{ parameters.imageRepository}}
    command: 'buildAndPush'
    Dockerfile: '$(Agent.BuildDirectory)/**/${{ parameters.projectName }}/Dockerfile'
    buildContext: './'
    tags: |
      latest
      $(Build.BuildId)

NOTE In Visual Studio 2022 (17.10 & 17.11) I can repro with "add new... docker support" menu item.

patverb commented 1 month ago

@oising thank you for getting me that project layout. I fixed a bug we had for looking at files to copy more than 1 directory higher than the project (since yours were in the "groupingfolder") and it should be released in 17.12. Sorry I missed this case initially.

oising commented 1 month ago

Excellent! thank you @patverb for the update. We've hand-modified the Dockerfile(s) to fix the issue by adding:

COPY ["Directory.Build.props", "Directory.Build.props"]
COPY ["Directory.Packages.props", "Directory.Packages.props"]

Putting this here for anyone else who finds this issue.