This is my Dockerfile
FROM mcr.microsoft.com/dotnet/nightly/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["MyApi.csproj", "."]
RUN dotnet restore "./MyApi.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "MyApi.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "MyApi.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApi.dll"]
RUN chmod x ./entrypoint.sh
CMD /bin/bash ./entrypoint.sh
This is 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
sleep 10
>&2 echo "Creating Database"
/opt/mssql-tools/bin/sqlcmd -S db -U sa -P P@55w0rd -i create-database.sql
This is create-database.sql
USE master
GO
IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = 'Demo')
BEGIN
CREATE DATABASE Demo;
END;
GO
But when I run it, I got this error
#17 0.615 chmod: cannot access './entrypoint.sh': No such file or directory
#17 ERROR: executor failed running [/bin/sh -c chmod x ./entrypoint.sh]: exit code: 1
CodePudding user response:
Your file is not in the directory in which you think it is.
Remember that the WORKDIR
command is effectively cd
for your Dockerfile
. Let's take a close look at the sequence of events in your question.
In the first stage, you have
WORKDIR /src
. You copy files from your local directory into the/src
directory:FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build WORKDIR /src COPY ["MyApi.csproj", "."] COPY . .
In your
final
phase, you copy files from/app/publish
into/app
:FROM base AS final WORKDIR /app COPY --from=publish /app/publish .
Your
CMD
attempts to runentrypoint.sh
in current directory, which is/app
because of thatWORKDIR /app
directive:CMD bash entrypoint.sh
However, you never explicitly move entrypoint.sh
from /src
to /app/publish
, and while I'm not familiar with the dotnet publish
command, I'm guessing that it doesn't move that file for you, either.
The solution is to make sure that entrypoint.sh
gets moved into /app
. The simplest solution may be to just copy it in explicitly in your final
stage:
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
COPY entrypoint.sh /app/entrypoint.sh
ENTRYPOINT ["dotnet", "MyApi.dll"]
CMD /bin/bash ./entrypoint.sh