【发布时间】:2021-07-22 07:33:42
【问题描述】:
我决定是时候学习 docker 了,为此我有一个在 IIS express 上完美运行的 asp.net core api 项目。这个项目是一个简单的 REST API。 这里的目标是设置一个基本的 get 入口点,检查它是否适用于 IIS Express,然后创建一个 Dockerfile 以部署我的应用程序。我发布这个问题是因为我试图在 web 和 stackoverflow 上找到响应。
为此,我已经有一个基本的启动类:
namespace app.rest
{
public class Startup
{
private readonly string ConnectionString;
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<AppService, AppServiceImpl>();
services.AddScoped<ApiEntryPointService, ApiEntryPointServiceImpl>();
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebApplication", Version = "v1" });
});
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApplication v1"));
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
// app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
在项目创建时自动实现的 Program 类。
namespace app.rest
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
还有一个简单的控制器:
namespace app.rest.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AppController
{
// GET api/<AppController>/5
[HttpGet]
public string Get()
{
return "Hello World";
}
}
}
当我使用 IIS Express 并且我可以看到我的 hello world 时,这可以正常工作。我的 dockerfile 可以构建镜像,我什至可以运行它。
这是我的 Dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS base
WORKDIR /appSource
EXPOSE 5000
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /appBuild
# Copy csproj and restore as distinct layers
COPY *.sln .
COPY *.csproj .
#RUN mkdir /tmp/build/
#COPY . /tmp/build
#RUN find /tmp/build -name *.csproj
RUN dotnet restore app.rest.csproj --verbosity detailed
# Copy everything else and build
COPY . .
WORKDIR /appBuild/app.rest
RUN dotnet build "../app.rest.csproj" -c Release -o /appSource --no-restore
FROM build AS publish
WORKDIR /appBuild/app.rest
RUN dotnet publish "../app.rest.csproj" -c Release -o /appSource
FROM base AS final
WORKDIR /appSource
COPY --from=publish /appSource .
ENTRYPOINT ["dotnet", "app.rest.dll"]
要构建图像,我只需键入:
docker build --pull -t app.rest .
当我暴露端口 5000 时,我会启动一个 docker run 命令并绑定端口(如果我理解正确的话):
docker run -d -p 5000:5000 -it app.rest
我检查了日志,没有什么特别的问题:
warn: Microsoft.AspNetCore.DataProtection.Repositories.FileSystemXmlRepository[60]
Storing keys in a directory 'C:\Users\ContainerUser\AppData\Local\ASP.NET\DataProtection-Keys' that may not be persisted outside of the c]0;C:\Program Files\dotnet\dotnet.exeontainer. Protected data will be unavailable when container is destroyed.
info: Microsoft.Hosting.Lifetime[0]
Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\appSource
我应该可以使用这个 URL 连接到这个 api:http://localhost:5000/api/App 不幸的是,事实并非如此。
【问题讨论】:
标签: c# docker asp.net-core dockerfile