它是使用Docker-compose的SQL Server 2017 for Linux的VSTudio 2017 ASP.NET核心Web应用程序项目。 dockerfile是:
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 documentation本指南的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选项”
对于没有dockerfile
的RUN chmod +x ./entrypoint.sh
,我建议你试试
WORKDIR /src/CoreWebApp
RUN dotnet build CoreWebApp.csproj -c Release -o /app
Run dotnet ef database update