在Docker中构建net core 5.0时显示错误“处理您的请求时发生错误”(Centos 8/VScode / Docker/EF Core)?

lh80um4z  于 2022-11-07  发布在  Docker
关注(0)|答案(1)|浏览(112)

当我在Visual Studio代码中创建ASP.NET核心Web应用程序,然后将Docker支持添加到项目中时,将创建一个Docker文件,如下所示:
#停靠文件#

FROM mcr.microsoft.com/dotnet/aspnet:5.0-focal AS base
WORKDIR /app
EXPOSE 8000

ENV BloggingDatabase="Server=xxx.xxx.xxx.xxx;Database=testdb;Trusted_Connection=True;User Id=myid;Password=myPassword;Integrated Security=false;"
ENV ASPNETCORE_URLS=http://+:8000

RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser

FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS build
WORKDIR /src
COPY ["MVCVue.csproj", "./"]
RUN dotnet restore "MVCVue.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "MVCVue.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MVCVue.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MVCVue.dll"]

我尝试从Visual Studio运行它代码可以找到本地数据库并连接到它。更改数据库设置环境变量运行Docker容器,但当我在Docker容器中运行应用程序时,无法连接到数据库运行容器,它返回此错误:

我的启动代码
#启动.cs#

namespace MVCVue
{

    public class Startup

    {

        public Startup(IConfiguration configuration)

        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)

        {
            services.AddControllersWithViews();
            services.AddDbContext<cpteContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase")));

services.AddAntiforgery(opiton => {
    opiton.FormFieldName = "MyAntiForgeryField";
    opiton.HeaderName = "ANTI-TOKEN-HEADERNAME";

});

        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

        {

            if (env.IsDevelopment())

            {

                app.UseDeveloperExceptionPage();

            }

            else

            {

                app.UseExceptionHandler("/Home/Error");

                app.UseHsts();

            }

            app.UseHttpsRedirection();

            app.UseStaticFiles();         
            app.UseRouting();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>

            {

                endpoints.MapControllerRoute(

                    name: "default",

                    pattern: "{controller=Home}/{action=Index}/{id?}");

            });

        }

    }

}

#应用程序设置.开发.json#

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "ConnectionStrings": {
    "BloggingDatabase": "Server=xxx.xxx.xxx.xxx;Database=testdb;Trusted_Connection=True;User Id=myid;Password=myPassword;Integrated Security=false;"
  }

}

数据库上下文#

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

        {

            var configuration = new ConfigurationBuilder()

                            .SetBasePath(Directory.GetCurrentDirectory())

                            .AddJsonFile("appsettings.json")

                           .Build();

            if (!optionsBuilder.IsConfigured)

            {

                var connectionString = configuration.GetConnectionString("BloggingDatabase");

                optionsBuilder.UseSqlServer(connectionString);

            }

        }

应用程序设置.json#

{

  "ConnectionStrings": {

    "BloggingDatabase": "Server=xxx.xxx.xxx.xxx;Database=testdb;Trusted_Connection=True;User Id=myid;Password=myPassword;Integrated Security=false;"

  },

  "Logging": {

    "LogLevel": {

      "Default": "Information",

      "Microsoft": "Warning",

      "Microsoft.Hosting.Lifetime": "Information"

    }

  },

  "AllowedHosts": "*"

}
hs1ihplo

hs1ihplo1#

我尝试代码加载项dockerfile环境变量after-show asp.net核心错误

ENV ASPNETCORE_ENVIRONMENT=Development

相关问题