aspnet / aspnet-docker

[Archived] ASP.NET Core Docker images for 1.x. Go to https://github.com/dotnet/dotnet-docker for 2.1 and up.
https://asp.net
719 stars 171 forks source link

Running microsoft/aspnetcore docker image gives me System.IO.FileNotFoundException: The configuration file 'appSettings.json' was not found and is not optional. The physical path is '/app/appSettings.json' #417

Closed morissonmaciel closed 6 years ago

morissonmaciel commented 6 years ago

Hello, I'm experiencing issues running a simple aspnet core 2.0 application in a aspnet core built image using only published files.

Steps to reproduce the issue

  1. Given following .csproj file:

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
    <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    </PropertyGroup>
    
    <ItemGroup>
    <Folder Include="wwwroot\" />
    </ItemGroup>
    
    <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.7" />
    </ItemGroup>
    
    <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3" />
    </ItemGroup>
    
    <ItemGroup>
    <Content Update="appSettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <CopyToPublishDirectory>Always</CopyToPublishDirectory>
    </Content>
    </ItemGroup>
    </Project>
  2. Run publish command aspnet publish -c Release -o out

  3. Then, using following Dockerfile:

    
    FROM microsoft/aspnetcore
    WORKDIR /app
    COPY /out .

EXPOSE 80

ENTRYPOINT ["dotnet", "Api.dll"]

4. Run build command `docker build -t api.web .`

5. After build the image, exec the docker run command `docker run -p 8056:80 -t api.web .`

6. Wait for std console show execution results

## Expected behavior

* Running docker container with application hosted in `http://localhost:8056` url

## Actual behavior

* aspnet command halts on **FileNotFoundException** unable to locate `appSettings.json` file

Unhandled Exception: System.IO.FileNotFoundException: The configuration file 'appSettings.json' was not found and is not optional. The physical path is '/app/appSettings.json'. at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload) at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load() at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers) at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build() at Api.Startup..ctor(IHostingEnvironment env)


## Additional information (e.g. issue happens only occasionally)

## Startup.cs file details
public class Startup
{
    public IConfiguration Configuration { get; }

    public Startup(IHostingEnvironment env)
    {
        var config = new ConfigurationBuilder()
                        .SetBasePath(env.ContentRootPath)
                        .AddJsonFile("appSettings.json")
                        .AddJsonFile("appSettings." + env.EnvironmentName + ".json", optional: true)
                        .AddEnvironmentVariables()
                        .Build();

    ...
    }
}

## Output of `dotnet --info`

Product Information: Version: 2.1.105 Commit SHA-1 hash: 141cc8d976

Runtime Environment: OS Name: Windows OS Version: 10.0.17134 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\2.1.105\

Microsoft .NET Core Shared Framework Host

Version : 2.0.7 Build : 2d61d0b043915bc948ebf98836fefe9ba942be11


## Output of `docker info`

Containers: 2 Running: 0 Paused: 0 Stopped: 2 Images: 6 Server Version: 18.05.0-ce Storage Driver: aufs Root Dir: /mnt/sda1/var/lib/docker/aufs Backing Filesystem: extfs Dirs: 24 Dirperm1 Supported: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 773c489c9c1b21a6d78b5c538cd395416ec50f88 runc version: 4fc53a81fb7c994640722ac585fa9ca548971871 init version: 949e6fa Security Options: seccomp Profile: default Kernel Version: 4.9.93-boot2docker Operating System: Boot2Docker 18.05.0-ce (TCL 8.2.1); HEAD : b5d6989 - Thu May 10 16:35:28 UTC 2018 OSType: linux Architecture: x86_64 CPUs: 1 Total Memory: 995.6MiB Name: default ID: KCFV:S3B5:35GQ:NUVW:WRHR:SWB4:FRPT:HGXW:MFPQ:2PLP:HHIN:62LL Docker Root Dir: /mnt/sda1/var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Labels: provider=virtualbox Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false

natemcmaster commented 6 years ago

Does it work on Linux (without Docker)? This error is commonly caused by file casing differences since Linux has a case-sensitive file system.

morissonmaciel commented 6 years ago

Hi @natemcmaster, Thanks in advance. I don't have a pure Linux machine for this test, but I've just changed uppercase/lowercase as you mentioned in your post, and rebuilt my dotnet core image.

Surprisingly, even with filename explicit using "S" in uppercase in both Windows filesystem and Linux machine image (I used docker container cp command to check files in container) is considering only with "s" lowercase.

I made a simple test in Startup.cs file and can confirm your point:

var all = System.IO.File.ReadAllText(System.IO.Path.Combine(env.ContentRootPath, "appsettings.json"));
Console.WriteLine(all);

The above line wrote all my appsettings.json content without any I/O exception.

Once and again Thanks

natemcmaster commented 6 years ago

So, your issue is fixed?

morissonmaciel commented 6 years ago

Yah @natemcmaster, for sure. Thanks a lot

natemcmaster commented 6 years ago

Happy to help.