檔案
# syntax=docker/dockerfile:1
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Build
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "myapp.dll"]
第一次構建它,我遇到了這個錯誤:
錯誤 CS5001:程式不包含適合入口點的靜態“Main”方法
我解決了它添加
<OutputType>Library</OutputType>
到我的 .csproj,然后我可以構建它。
此時,當嘗試使用
docker run -p 8082:8080 --rm dockerized-app:dev
Docker 拋出此錯誤:
在“/app/”中找不到執行應用程式所需的庫“libhostpolicy.so”。
在互聯網上尋找解決方案我發現我可以解決它添加
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
到 .csproj,我做了,再次構建,并且在運行時我收到這個我無法解決的錯誤:
在程式集“myapp,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null”中找不到入口點。
編輯:添加 API 的實際代碼。
控制器.cs:
[ApiController]
[Route("api/[controller]")]
public class MyAppController : ControllerBase
{
[EnableCors("AllowOrigin")]
[HttpGet]
public string GetText(Request request)
{
return request.text;
}
}
程式.cs:
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
啟動.cs:
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.AddControllers();
services.AddCors(options =>
{
options.AddPolicy("AllowAllHeaders",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors(builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
啟動設定.json:
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:58726",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "myapp",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"MyApp": {
"commandName": "Project",
"launchUrl": "myapp",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
myapp.csproj(有我之前提到的變化):
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<OutputType>Library</OutputType>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>
<ItemGroup>
<None Remove="Newtonsoft.Json" />
<None Remove="Models\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="Models\" />
</ItemGroup>
</Project>
And, just in case someone asks, it works locally, it starts and wait for receiving a request.
uj5u.com熱心網友回復:
您只需將 .csproj 檔案復制到映像中。您錯過了復制其余檔案的步驟。
# syntax=docker/dockerfile:1
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# vvvv This is missing
COPY . .
# Build
RUN dotnet publish -c Release -o out
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "myapp.dll"]
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/383903.html
標籤:docker asp.net-core asp.net-core-webapi
上一篇:Docker在AWSECS中運行
