一个MySQL容器之后搬运工asp.net核心容器的起始(docker asp.net core c

2019-09-29 09:49发布

我有asp.net核心泊坞窗容器中,并与MySQL的容器。 现在我需要用asp.net核心容器等待mysql的容器,并准备(包括容器开始通过搬运工-compose.yml)。

喜欢的东西https://github.com/jwilder/dockerize ,wait-for-it.sh或等待换似乎并不为asp.net核心容器的工作。

有某人知道如何解决这个问题?

UPDATE

这是我的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

如果我有搬运工人,撰写启动容器我得到的错误信息:

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

我怎样才能结合入口点,并在一个文件中CMD /斌/庆典./app/entrypoint.sh命令。 我想我需要的入口点为DOTNET applicatioin运行和CMD为entrypoint.sh等待数据库容器。 是否有解决方案同时获得运行?

Answer 1:

https://docs.docker.com/compose/aspnet-mssql-compose/

泊坞窗的文档列表中如何获得ASP.net容器等待它通过具有DB连接的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


Answer 2:

而不是具有推迟从外面你也可以检查,等待您的dotnet应用程序的数据库连接您的dotnet应用程序的启动脚本。

在下面,你可以看到host.Run()被推迟到waitForDb()返回。

在下面我举的例子,我把逻辑检查SQL数据库(在泊坞窗容器中运行)是否已经启动了一个名为服务中DbHealthChecker (实际上,整个方法waitForDb可能是一个专门的服务)。 这健康检查使用的中描述的技术之一, 这个计算器线程检查数据库是否正在运行。

public class Program
{
  public static void Main(string[] args)
  {
    var host = CreateWebHostBuilder(args).Build();

    Task.Run(async () => {
      using (var scope = host.Services.CreateScope())
      {
        var services = scope.ServiceProvider;

        await waitForDb(services);

        // ... other initializations, e.g. seeding db ...
      }
    }).Wait();

    host.Run();
  }

  private static async Task waitForDb(IServiceProvider services)
  {
    // create your own connection checker here
    // see https://stackoverflow.com/questions/19211082/testing-an-entity-framework-database-connection

    var healthChecker = services.GetRequiredService<DbHealthChecker>();
    var maxAttemps = 12;
    var delay = 5000;

    for (int i = 0; i < maxAttemps; i++) {
      if (healthChecker.isConnected()) {
        return;
      }
      await Task.Delay(delay);
    }

    // after a few attemps we give up
    throw new HttpException(503);
  }
}


文章来源: docker asp.net core container starting after a mysql container