它是一个 VSStudio 2017 ASP.NET Core Web 应用程序项目,带有 SQL Server 2017 for Linux,使用 Docker-compose。码头文件是:
FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY CoreWebApp/CoreWebApp.csproj CoreWebApp/
RUN dotnet restore CoreWebApp/CoreWebApp.csproj
COPY . .
WORKDIR /src/CoreWebApp
RUN dotnet build CoreWebApp.csproj -c Release -o /app
FROM build AS publish
RUN dotnet publish CoreWebApp.csproj -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
#ENTRYPOINT ["dotnet", "CoreWebApp.dll"]
和 docker-compose.yml:
version: '3.4'
services:
sql.data:
image: microsoft/mssql-server-linux:2017-latest
environment:
- SA_PASSWORD=Sql_Admin1
- ACCEPT_EULA=Y
ports:
- "5434:1433"
container_name: sqlserverlnx-2017
corewebapp:
image: corewebapp:dev
build:
context: .
dockerfile: CoreWebApp/Dockerfile
container_name: corewebapp
depends_on:
- sql.data
我想在应用程序启动之前将迁移应用到数据库。根据Docker Compose 文档中本指南的 setp 4,我将 Dockerfile 的 ENTRYPOINT 行替换为:
#ENTRYPOINT ["dotnet", "CoreWebApp.dll"]
RUN chmod +x ./entrypoint.sh
CMD /bin/bash ./entrypoint.sh
并创建了一个 entrypoint.sh 文件,如下所示:
#!/bin/bash
set -e
run_cmd="dotnet CoreWebApp.dll"
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
但迁移不适用。我可以看到 entrypoing.sh 执行时没有错误的构建输出,如以下日志尾所示:
##... previous log steps removed for simplicity
2>**Step 18/18 : CMD /bin/bash ./entrypoint.sh**
2> ---> Running in 2dbecae7cd43
2>Removing intermediate container 2dbecae7cd43
2> ---> 878096c73494
2>Successfully built 878096c73494
2>Successfully tagged corewebapp:dev
2>docker-compose -f "G:\Dockerizing ASPNet Core\CoreWebApp\docker-compose.yml" -f "G:\Dockerizing ASPNet Core\CoreWebApp\docker-compose.override.yml" -f "G:\Dockerizing ASPNet Core\CoreWebApp\obj\Docker\docker-compose.vs.release.g.yml" -p dockercompose7669752967822022310 --no-ansi up -d --no-build
2>sqlserverlnx-2017 is up-to-date
2>Recreating corewebapp ...
2>Recreating corewebapp ... done
[编辑:]我通过PowerShell控制台进入容器,发现一些问题: 1. entrypoint.sh的代码页不是Unix风格的。固定的。2. 生成的图像不包含 dotnet SDK,然后失败:
until dotnet ef database update; do
出现以下错误:
您的意思是运行 dotnet SDK 命令吗?请从以下位置安装 dotnet SDK: https ://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
我将 Dockerfile 中最后阶段的最终图像替换为使用“build”而不是“base”,如下所示:
FROM build AS final
WORKDIR /app
COPY --from=publish /app .
并且绝对 dotnet sdk 包含在最终图像中,但它不能解决问题。现在手动运行迁移会产生以下错误: “未找到项目。更改当前工作目录或使用 --project 选项”