dev
dev

Reputation: 195

how to include class library reference into docker file

i'm trying to build docker file for .net core application which is having reference of class library whenever i'm trying to build docker i'm getting below error.

Skipping project because it was not found

below is the docker file which i'm using

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 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 mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "testapp.dll"]

Upvotes: 7

Views: 8246

Answers (2)

Ash K
Ash K

Reputation: 3631

Answer using .NET 8 and chiseled containers that create smaller, more secure container images

Full source code here.


For creating a docker image of an app (MyCoolTestApp.API) with a dependent library(MyCoolTestApp.Lib) as shown below:

docker-with-classlib
├── MyCoolTestApp.API
│   ├── MyCoolTestApp.API.csproj
│   └── other files...
├── MyCoolTestApp.Lib
│   ├── MyCoolTestApp.Lib.csproj
│   └── other files...
├── MyCoolTestApp.sln
├── MyCoolTestApp.API.Dockerfile
├── README.md
├── .gitignore
└── .dockerignore

You can create docker image with this dockerfile:

# https://hub.docker.com/_/microsoft-dotnet
FROM mcr.microsoft.com/dotnet/sdk:8.0-jammy AS build
WORKDIR /source

# copy csproj files and restore as distinct layers
COPY "MyCoolTestApp.API/*.csproj" "MyCoolTestApp.API/"
COPY "MyCoolTestApp.Lib/*.csproj" "MyCoolTestApp.Lib/"
RUN dotnet restore "MyCoolTestApp.API/MyCoolTestApp.API.csproj"

# copy and build app and libraries
COPY "MyCoolTestApp.API/" "MyCoolTestApp.API/"
COPY "MyCoolTestApp.Lib/" "MyCoolTestApp.Lib/"
WORKDIR "/source/MyCoolTestApp.API"
RUN dotnet publish -o /app

# final stage/image
FROM mcr.microsoft.com/dotnet/nightly/aspnet:8.0-jammy-chiseled-composite
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["./MyCoolTestApp.API"]

Build the image like this by navigating to docker-with-classlib folder:

docker build -f MyCoolTestApp.API.Dockerfile . -t ashk/my-cool-api:0.1.0

Run the container like this:

docker run --rm -it -p 8000:8080 -e ASPNETCORE_ENVIRONMENT=Development ashk/my-cool-api:0.1.0

Upvotes: 6

Ewerton
Ewerton

Reputation: 530

You need to move the Dockerfile to the solution level, and reference the projects using [Folder]/[Project].csproj. Here is my Dockerfile (located in the solution level):

FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["SumApi/SumApi.csproj", "SumApi/"]
COPY ["Shared/Shared.csproj", "Shared/"]
RUN dotnet restore "SumApi/SumApi.csproj"
COPY . .
WORKDIR "/src/SumApi"
RUN dotnet build "SumApi.csproj" -c Release -o /app/build

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

FROM base AS final
WORKDIR /app`enter code here`
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "SumApi.dll"]

Learned it from here: https://imranarshad.com/dockerize-net-core-web-app-with-dependent-class-library/

Upvotes: 8

Related Questions