c# - 在 docker 容器 C# App 之外连接被拒绝
问题描述
我在 C# .Net Core (v 3.1) 中有一个应用程序,当我运行它时。它工作正常。
当我在 docker 容器中启动它时 - 它在容器中运行良好,但是Connection Refused
在尝试调用容器外的端点时出现错误
我的命令
docker build . -t project
docker run project -p 23183:23183
我的程序.cs
using System.IO;
using Microsoft.AspNetCore.Hosting;
namespace project
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseKestrel()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
}
我的 Startup.cs
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddRouting(options => options.LowercaseUrls = true);
services.AddIdentityServerAuth(Configuration);
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "Project API",
Version = "v1"
});
// Set the comments path for the Swagger JSON and UI.
string xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
string xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
options.OperationFilter<AddAuthorizationHeaderParameterOperationFilter>();
});
services.AddCors();
services.AddControllers(options => options.EnableEndpointRouting = false);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
DbStaticConfig.Init(Configuration.GetConnectionString("ProjectDb"));
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseSwagger(c =>
{
c.PreSerializeFilters.Add((document, request) =>
{
var paths = document.Paths.ToDictionary(item => item.Key.ToLowerInvariant(), item => item.Value);
document.Paths.Clear();
foreach (var pathItem in paths)
{
document.Paths.Add(pathItem.Key, pathItem.Value);
}
});
});
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Project API v1");
options.RoutePrefix = string.Empty;
options.ConfigObject.DeepLinking = true;
});
app.UseCors(x => x
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowed(origin => true)
.AllowCredentials());
app.UseRouting();
app.UseAuthentication();
app.UseHttpsRedirection();
app.UseEndpoints(x => x.MapControllers());
}
我的 Dockerfile
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as base
COPY . /app
WORKDIR /app
EXPOSE 23183/tcp
ENV ASPNETCORE_URLS http://*:23183
# Build
FROM base as build
WORKDIR /src
COPY *.sln ./
COPY project/project.csproj project/
RUN dotnet restore
COPY . .
WORKDIR /src/project
# Publish
FROM build AS publish
RUN dotnet publish -c Release -o /app/out
FROM base AS final
WORKDIR /app
COPY --from=publish /app/out ./
ENTRYPOINT ["dotnet", "project.dll", "--server.urls", "http://0.0.0.0:23183"]
解决方案
不确定您的问题到底是什么,但这就是我的 docker 文件的样子。希望能帮助到你:
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["path/to/project.csproj", "project/"]
COPY . .
WORKDIR "/src/project"
FROM build AS publish
RUN dotnet publish "project.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "project.dll"]
推荐阅读
- operating-system - Spyder IDE 版本操作系统要求
- google-sheets - 使下拉菜单选择遵循特定文本的公式?
- angular - 在 p-chips 和 formcontrolname 中间设置一个逻辑
- python-3.x - 程序运行后,如何在 txt 文件中获取输入
- swiftui - SwiftUI 和 AppKit:使用关闭对话框询问应用是否允许退出
- matlab - Matlab 会响应 VS 编译的 DLL 引发的异常吗?
- python - Python_获取一个数字序列中的计数,然后将其除以长度
- flutter - 如何使用底部导航栏导航到特定屏幕
- c++ - 天真的后缀数组优化 C++
- arrays - 使用 map 在数组中创建出现的哈希值