当asp.net核心应用程序在docker容器中运行时,UseStaticFiles不起作用

时间:2017-10-15 11:55:40

标签: asp.net-mvc asp.net-core

我正在尝试将asp.net核心应用程序部署到docker容器中。应用程序是使用dotnet new mvc创建的。当应用程序运行时,一切正常是非docker环境。但是,在docker容器中,Browser无法加载wwwroot文件夹中的所有静态文件。 这是Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseWebRoot(Path.Combine(Directory.GetCurrentDirectory(),"wwwroot"))
            .UseUrls("http://*:5050")
            .Build();
}

这是Startup.cs

   public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

}

这是dockerfile

FROM microsoft/aspnetcore
RUN mkdir -p /app
COPY app/* /app/
WORKDIR /app
CMD ["dotnet","/app/app.dll"]

1 个答案:

答案 0 :(得分:0)

我在dockerfile中犯了一些错误。

以下命令无法递归复制源目录

COPY app/* /app/

相反,应该按如下方式更正COPY命令以支持递归

COPY app/ /app/