'Edit Asp.net core appsettings.json when in a docker app?

I've an Asp.Net core web application, that will get deployed within docker images. How can I allow users to provide an appsettings.json?

I build my docker image like this:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Backend/Some-Project.csproj", "Backend/"]
RUN dotnet restore "Backend/Some-Project.csproj"
COPY Backend Backend
WORKDIR "/src/Backend"
RUN dotnet build "Some-Project.csproj" -c Release -o /app/build


FROM build AS publish
RUN dotnet publish "Some-Project.csproj" -c Release -o /app/publish 

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Some-Project.dll"]

My current docker-compose.yml look like this:

version: "3"
services:
  mongo:
    image: mongo:latest
    container_name: "my-app-mongo"
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: admin
      MONGO_INITDB_DATABASE: my-app-db
    ports:
      - 27017:27017
      
  vs-central:
    image: my-app:latest
    container_name: "my-app"
    restart: always

How should modify those to replace the appsettings.json ? I taught about a volume, but since it's only one file and not a directory, I think it will not work



Solution 1:[1]

You can volume map a single file. It just requires that it exists on the host. If it doesn't, Docker will create a directory on the host and map that.

In your case you can do

  vs-central:
    image: my-app:latest
    container_name: "my-app"
    restart: always
    volumes:
      - ./appsettings.json:/app/appsettings.json:ro

I've added the ro modifier to make the file read-only, so it can't be modified by the container by mistake.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hans Kilian