star213
star213

Reputation: 227

Docker image doesnt work - probably wrong paths on build

I'm trying to move ASP.NET Core project to docker linux container, however I cannot build up a proper image. When I create default dockerfile (through VisualStudio Add -> DockerSupport) it's setting up paths for COPY to all of the projects. However when I run docker build -t . it pop's out error "no such file or directory". I've tried to stick with official docker documentation with moving asp.net core to docker containers and here is the code I ended with:

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 ./ ./
RUN dotnet restore "myproject.csproj"
COPY . .
WORKDIR "/src/myproject"
RUN dotnet build "myproject.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "myproject.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "myproject.dll"]

This is the command im using to build up an image:

docker build -t myapp -f myproject/Dockerfile .

I'm building it from the parent directory, because when I was using current working directory it wasn't finding all the .csproj files. When I run this image it doesn't work - I know there are wrong paths, however I have no idea where is the error in logic. Current error is "Program does not contain a static 'Main' method suitable for an entry point" and "A fatal error was encountered. The library 'libhostpolicy.so' required to execute the application was not found in '/app/'. Failed to run as a self-contained app. If this should be a framework-dependent app, specify the appropriate framework in /app/myproject.runtimeconfig.json"

Upvotes: 1

Views: 1866

Answers (2)

Edward
Edward

Reputation: 30046

You should not specify RUN dotnet build "myproject.csproj" -c Release -o /app with -o /app which will output in the same folder with build layer.

Try

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 ["myproject/*.csproj", "myproject/"]
RUN dotnet restore "myproject/myproject.csproj"
COPY . .
WORKDIR /src/myproject
RUN dotnet build "myproject.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "myproject.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "myproject.dll"]

Upvotes: 0

eVolve
eVolve

Reputation: 1456

Try the following based off this documentation replace myproject with project name

FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", “myproject.dll"]

Then build and run with the following commands:

docker build -t myproject .
docker run -d -p 8080:80 --name myapp myproject

Upvotes: 1

Related Questions