Reputation: 822
It is a VSTudio 2017 ASP.NET Core Web application project with SQL Server 2017 for Linux using Docker-compose. The dockerfile is:
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"]
and the 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
I want to apply migrations to the database before the application starts. Based on the setp 4 of this guide at Docker Compose documentation I replaced the ENTRYPOINT line of my Dockerfile by this:
#ENTRYPOINT ["dotnet", "CoreWebApp.dll"]
RUN chmod +x ./entrypoint.sh
CMD /bin/bash ./entrypoint.sh
And created an entrypoint.sh file like this:
#!/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
But the migration does not apply. I can see the build output that the entrypoing.sh is executed with no errors as in the following log tail:
##... 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
[Edit:] I entered to the container by PowerShell console and found some problems: 1. The code page of entrypoint.sh was not Unix style. Fixed. 2. The resulting image does not include dotnet SDK then it fails at:
until dotnet ef database update; do
with the following error:
Did you mean to run dotnet SDK commands? Please install dotnet SDK from: https://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
I replaced the final image at the last stage in the Dockerfile to use "build" instead "base" like this:
FROM build AS final
WORKDIR /app
COPY --from=publish /app .
and definitely the dotnet sdk is included in the final image but it does not solve the problem. Running manually the migration now produces the following error: "No project was found. Change the current working directory or use the --project option"
Upvotes: 0
Views: 1700
Reputation: 30046
For dockerfile
without RUN chmod +x ./entrypoint.sh
, I suggest you try like
WORKDIR /src/CoreWebApp
RUN dotnet build CoreWebApp.csproj -c Release -o /app
Run dotnet ef database update
Upvotes: 0