microsoft / DockerTools

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

Auto generated empty appsettings.{ASPNETCORE_ENVIRONMENT}.json during project warmup #344

Closed Erezinho closed 1 year ago

Erezinho commented 2 years ago

Working with VS 2022 Preview, I have a .net 6 web api project with Docker, Linux containers. Since I have many variants of appsettings json file I decided to group them all in a sub-folder 'Settings' under root folder e.g. < path to repo >/MyProject/Settings/

I use mounting to pass the corresponding appsettings.{ASPNETCORE_ENVIRONMENT}.json to the container's /app/ folder.

During VS project warmup and only in Debug, an empty appsettings.{ASPNETCORE_ENVIRONMENT}.json is created in root folder. This adds the file to the solution, root folder and git's un-staged changes - undesired behavior which requires me (and other team members) to clean up the development environment every now and then.

Is there a way to avoid it? some flag to set ? Any way to edit VS's default location for appsettings ? (because when the files are in root folder, this does not happen)

Otherwise it looks like a bug ..

Thanks!

ravipal commented 2 years ago

I am not able to reproduce this issue. Does your application code write any settings that could cause this file creation?

danegsta commented 2 years ago

@Erezinho I'm suspicious this may be an interaction between how you're mounting the appsettings file into the container and the default VS behavior during debug mode (fast mode) which volume mounts the application into the container to speed up container build times during local debugging (see: https://docs.microsoft.com/en-us/visualstudio/containers/container-build?view=vs-2022). Are you mounting the appsettings volume via the DockerfileRunArguments configuration property or through VOLUME entries in your Dockerfile?

Erezinho commented 1 year ago

@danegsta - I though about it as well , because I mount from Configuration to /app and VS mounts the entire solution folder to /app, eventually I end up with the file back to solution folder. I did some trail and error (release/debug, generating different files etc) and it preproduced when expecting it to and vice-versa.

I'm using DockerfileRunArguments when running only my project and docker-compose volume mounting when running with other project as one docker network. Happens in both cases.

So it seems 'by design' One way mapping could help here but afaik it is not possible.

danegsta commented 1 year ago

@Erezinho you may want to consider referencing your configs from the sub-folder you've got them in by adding additional JSON configuration files: https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.jsonconfigurationextensions.addjsonfile?view=dotnet-plat-ext-6.0

If you're using the default WebApplicationBuilder, you should be able to something as simple as builder.Configuration.AddJsonFile pointed at the path to the config file in the container. That way you wouldn't need to do any volume mounting to place the config at the root of the container to be picked up by the automatic configs.

Erezinho commented 1 year ago

Indeed its another option. I'll still need to mount the folder, but to same relative path (I don't include the folder in the image)

I'll do some thinking to decide between shifting back all appsettings to root folder or using specific path when loading appsettings.

Thanks!