如何解决Dockerizing .NET Core Web API
我正在尝试首次将.NET Core 2.x Web API应用程序码头化。 这是我的文件:
Dockerfile
FROM mcr.microsoft.com/dotnet/core/sdk:2.1 AS build-env
WORKDIR /app
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
RUN dotnet ef database update
FROM mcr.microsoft.com/dotnet/core/aspnet:2.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet","docker-webapi.dll"]
docker-compose.yml
version: "3.7"
# define all containers we want to use
services:
db:
image: mcr.microsoft.com/mssql/server:2017-latest
# define the credentials to be used in our web application
environment:
ACCEPT_EULA: 'Y'
SA_PASSWORD: 'sa12345$'
ports:
- '1433:1433'
web:
build:
context: .
dockerfile: Dockerfile
image: docker-webapi
depends_on:
- db
ports:
- '3000:3000'
appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "User ID=sa;password=sa12345$;server=db;Database=master"
},"Logging": {
"LogLevel": {
"Default": "Warning"
}
},"AllowedHosts": "*"
}
从Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<AppDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddScoped<IEmployeeService,EmployeeService>();
}
无论何时我尝试使用以下任何命令进行构建:
docker-compose build .
or
docker build -t docker-webapi .
我得到了错误:
System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server.
The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
(provider: TCP Provider,error: 35 - An internal exception was caught) --->
System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000005,6): No such device or address
我在做什么错了?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。