microsoft / DockerTools

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

AspNetCore staticwebassets on .NET 6 #337

Closed Freakazoid182 closed 2 years ago

Freakazoid182 commented 2 years ago

I found that the following setup doesn't work:

For .NET 6 AspNetCore (maybe .NET 5 too) projects, the container tools create a (project_name).staticwebassets.runtime.CT.json file in the output folder if you have a wwwroot folder in your project.

It sets a ContentRoots path to /src/wwwroot. This is to ensure that the application running in the container knows where to service the static file asset from. The original (project_name).staticwebassets.runtime.json contains a Windows path, so the container tools override to the CT.json file by setting environment variable ASPNETCORE_STATICWEBASSETS="/app/bin/{project name}.staticwebassets.runtime.CT.json" on the docker exec command that starts the application in the container.

The issue is that the /src folder is always mounted to the folder where the Dockerfile is, which isn't necessarily the project folder containing wwwroot too.

In my case I have multiple projects which I want to share a single Dockerfile with, which I reference through the DockerfileFile build property, by configuring it to a Dockerfile in a parent folder. The end result is that the you get a System.IO.DirectoryNotFoundException: /src/wwwroot/ exception at startup.

So my specific situation is:

I couldn't find any configuration to set the folder which should be mounted to the /src folder on the container. It would be nice if the container tools allow to override this value like with other build properties.

Maybe I'm just doing something which isn't actually supported, but it feels like it should be possible. Or maybe I'm just overlooking a setting that's already available. Any help would be greatly appreciated.

Here's an example of my (project_name).staticwebassets.runtime.CT.json file:

{
  "ContentRoots": [
    "/src/wwwroot/"
  ],
  "Root": {
    "Children": {
      "foo.txt": {
        "Children": null,
        "Asset": {
          "ContentRootIndex": 0,
          "SubPath": "foo.txt"
        },
        "Patterns": null
      }
    },
    "Asset": null,
    "Patterns": [
      {
        "ContentRootIndex": 0,
        "Pattern": "**",
        "Depth": 0
      }
    ]
  }
}
Freakazoid182 commented 2 years ago

Ok I already figured out what I was doing wrong.

I thought the /src folder was mounted on the folder where the DockerfileFile build property was set to, but it's actually set to DockerfileContext property. This is a relative path to the DockerfileFile path. So setting the DockerfileContext property to the folder containing wwwroot for the project I wanted to run resolves the issue.

I still think there's scenario's where you still need to be able to override where /src is mapped to.

I hope this issue might help someone else running into a similar problem ✌