【发布时间】:2018-12-23 21:17:01
【问题描述】:
我有一个带有 asp.net 核心的 docker 容器和一个带有 mysql 的容器。现在我需要等待 asp.net 核心容器,因为 mysql 容器已启动并准备就绪(两个容器都通过 docker-compose.yml 启动)。
https://github.com/jwilder/dockerize、wait-for-it.sh 或 wait-for 之类的东西似乎不适用于 asp.net 核心容器。
有人知道如何解决这个问题吗?
更新
这是我的 dockerfile:
FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore test.csproj
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out test.csproj
# Build runtime image
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY ./entrypoint.sh ./app/
RUN chmod +x ./app/entrypoint.sh
CMD /bin/bash ./app/entrypoint.sh
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "test.dll"]
这是我的 entrypoint.sh:
#!/bin/bash
set -e
run_cmd="dotnet run --server.urls http://*:80"
until dotnet ef database update; do
>&2 echo "SQL Server is starting up"
sleep 1
done
>&2 echo "SQL Server is up - executing command"
exec $run_cmd
如果我使用 docker-compose 启动容器,我会收到错误消息:
Unhandled Exception: System.FormatException: Value for switch '/app/entrypoint.sh' is missing.
test | at Microsoft.Extensions.Configuration.CommandLine.CommandLineConfigurationProvider.Load()
test | at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
test | at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
test | at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors)
test | at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
test | at test.Program.Main(String[] args) in /app/Program.cs:line 19
如何将 ENTRYPOINT 和 CMD /bin/bash ./app/entrypoint.sh 命令组合到一个文件中。我想我需要 ENTRYPOINT 来运行 dotnet 应用程序,并且需要 cmd 来让 entrypoint.sh 等待数据库容器。有解决方案让两者都运行吗?
【问题讨论】:
-
我想你已经看过docs.docker.com/compose/startup-order了。您不应该尝试等待您的数据库,无论如何启动 asp.net 容器,当数据库可访问时它将开始工作。否则,您应该修复您的应用程序以在没有数据库的情况下启动并等待。
-
问题是 asp.net 在启动时会迁移数据库(entity.framework),如果数据库没有准备好,asp.net 应用程序会给我一个错误消息,表明连接到数据库不工作。
-
这听起来更像是实体框架设置不正确。它应该只根据命令迁移,而不是立即尝试。
-
如果我首先启动容器。数据库是空的(那里没有表)。 asp.net core application exit with code 139 MySql 无法连接数据库主机。如果我再次启动应用程序,它的工作原理。所以我认为数据库不是第一次启动并运行。如果我这样做
docker-compose down --volumes,表将被删除,我再次收到错误
标签: docker .net-core docker-compose