Skip to content

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

Closed

Description

@oising

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"]

Activity

  1. oising commented on Aug 12, 2024

    @oising
    Author

    Patrick Verbrugge (@patverb) said:

    Oisin Grehan (@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.

  2. patverb commented on Aug 12, 2024

    @patverb
    Contributor

    Oisin Grehan (@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.

  3. oising commented on Aug 12, 2024

    @oising
    Author

    Excellent! thank you Patrick Verbrugge (@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.

  4. patverb commented on Dec 1, 2025

    @patverb
    Contributor

    This should be fixed in the latest release of visual studio.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions