ASP.Net Core & Docker - Project file does not exist - c#

I have this Dockerfile
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 5000
RUN apt-get -qq update && apt-get -qqy --no-install-recommends install wget gnupg \
git \
unzip
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get install -y nodejs
WORKDIR /app
#EXPOSE 5001
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["mediere.csproj", "."]
RUN dotnet restore "./mediere.csproj"
COPY . .
WORKDIR "/src/."
RUN apt-get -qq update && apt-get -qqy --no-install-recommends install wget gnupg \
git \
unzip
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get install -y nodejs
#RUN npm install zone.js#0.11.3 --save
WORKDIR /app
RUN dotnet build "mediere.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "mediere.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "mediere.dll"]
and running docker-compose up throws me this error:
=> ERROR [build 11/11] RUN dotnet build "mediere.csproj" -c Release -o /app/build 2.5s
------
> [build 11/11] RUN dotnet build "mediere.csproj" -c Release -o /app/build:
#23 2.241 Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET
#23 2.241 Copyright (C) Microsoft Corporation. All rights reserved.
#23 2.241
#23 2.245 MSBUILD : error MSB1009: Project file does not exist.
#23 2.245 Switch: mediere.csproj
------
executor failed running [/bin/sh -c dotnet build "mediere.csproj" -c Release -o /app/build]: exit code: 1
ERROR: Service 'web' failed to build : Build failed
and I don't see why.
I'm using a default ASP.Net core & Angular template from VS 2019 and I'm trying to dockerize it.
Here's the folder structure:
why am I getting this error?
Am I missing something obvious?

It's because you're setting the current working directory to /app right before building the project:
WORKDIR /app
The project doesn't exist in that directory. There should be no need to have that line at all because the working directory before this instruction gets executed is the correct one.

Related

Unable to load the service index for source error for a CodeArtifact feed using a Dockerfile

It's my first question here so let me know if I have to change something or add more information.
We’re using AWS CodeArtifact for storing our packages and when we try to build a Docker image from our Dockerfile it fails because it's unable to load the source during the restore process.
We have a web API in .Net we want to deploy using AWS Fargate. This project runs smoothly from Visual Studio 2022 using Docker but we can’t build the image from PowerShell after adding our packages from CodeArtifact.
Our approach to include the credentials in the build is to pass the NuGet.Config stored on the host using Buildkit.
This’s our Dockerfile:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
ENV ASPNETCORE_URLS=http://+:49151
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY . .
WORKDIR /src
COPY ["Src/Presentation/Project.API/Project.API.csproj", "Src/Presentation/Project.API/"]
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
--mount=type=secret,id=nugetconfig \
dotnet restore "Src/Presentation/Project.API/Project.API.csproj" \
--configfile /run/secrets/nugetconfig
COPY . .
WORKDIR "/src/Src/Presentation/Project.API"
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
dotnet build "Project.API.csproj" -c Release -o /app/build \
--no-restore
FROM build AS publish
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
dotnet publish "Project.API.csproj" -c Release -o /app/publish \
--no-restore
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Project.API.dll"]
Our script in PowerShell:
docker buildx build --secret id=nugetconfig,src=$HOME\AppData\Roaming\NuGet\NuGet.Config -f "Src\Presentation\Project.API\Dockerfile" -t my-dotnet-image .
Output:
#14 [build 6/9] RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages --mount=type=secret,id=nugetconfig dotnet restore "Src/Presentation/Project.API/Project.API.csproj"
--configfile /run/secrets/nugetconfig
#14 1.175 Determining projects to restore...
#14 2.504 /src/Src/Presentation/Project.API/Project.API.csproj : error NU1301: Unable to load the service index for source
https://domain-123456789012.d.codeartifact.us-east-2.amazonaws.com/nuget/repository/v3/index.json.
What are we missing here? Once we have the Dockerfile working we want to use CDK for deploying the Docker image alongside our infrastructure.
We’re using aws codeartifact login command to authenticate with the service. We work on windows with a Linux container

Docker skip project on build

I have dockerfile with this config
FROM mcr.microsoft.com/dotnet/sdk:6.0 as builder
WORKDIR /app
COPY . .
RUN apt-get update;apt-get install curl; apt-get -y install zip
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
RUN unzip awscliv2.zip
RUN ./aws/install
ENV PATH="${PATH}:/root/.dotnet/tools"
#RUN dotnet tool install -g AWS.CodeArtifact.NuGet.CredentialProvider
#
#RUN dotnet codeartifact-creds install
#
#RUN aws codeartifact login --tool dotnet --domain monspire --domain-owner 986853728599 --repository monspire
RUN dotnet restore
RUN dotnet publish -c Release TooSee.Web.Host.csproj
# create the runnable container...
FROM mcr.microsoft.com/dotnet/sdk:6.0
# install dependencies
RUN apt-get update \
&& apt-get install -y --allow-unauthenticated \
libc6-dev \
libgdiplus \
libx11-dev \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY --from=builder /app/src/TooSee.Web.Host/bin/Debug/net6.0/publish/ .
ARG ENVIRONMENT
ENV ENVIRONMENT=${ENVIRONMENT}
EXPOSE 80
CMD ["dotnet", "TooSee.Web.Host.dll"]
And try to run it via docker build -t tooseeapi .
But for reasons it skipped .Core project
Here is log
=> ERROR [builder 9/9] RUN dotnet publish -c Release TooSee.Web.Host.csproj 6.9s
[builder 9/9] RUN dotnet publish -c Release TooSee.Web.Host.csproj:
#15 0.750 Microsoft (R) Build Engine version 17.2.0+41abc5629 for .NET
#15 0.750 Copyright (C) Microsoft Corporation. All rights reserved.
#15 0.750
#15 2.054 Determining projects to restore...
#15 2.057 Skipping project "/TooSee.Web.Core/TooSee.Web.Core.csproj" because it was not found.
#15 2.060 Skipping project "/TooSee.Web.Core/TooSee.Web.Core.csproj" because it was not found.
#15 2.385 All projects are up-to-date for restore.
#15 3.150 /usr/share/dotnet/sdk/6.0.302/Microsoft.Common.CurrentVersion.targets(2066,5): warning : The referenced project '../TooSee.Web.Core/TooSee.Web.Core.csproj' does not exist. [/app/TooSee.Web.Host.csproj]
Dockerfile is under this path - C:\Users\nemes\Documents\GitHub\tooseeapi\src\TooSee.Web.Host
And .Core is here
C:\Users\nemes\Documents\GitHub\tooseeapi\src\TooSee.Web.Core
But if I run dotnet publish -c Release TooSee.Web.Host.csproj I got no errors
Why it's skipped and how I can fix this?
you have to keep your docker file at this location
C:\Users\nemes\Documents\GitHub\tooseeapi\src
and copy both project files separately. something like
COPY ["TooSee.Web.Host/TooSee.Web.Host.csproj", "./"]
COPY ["TooSee.Web.Core/TooSee.Web.Core.csproj", "./"]

error MSB3073: The command "npm install" exited with code 1

i was containerizing my .Net + React.js application but during the process I have encountered an unexpected error. I got myself acquainted with similar posts but none of the solutions solved my problem. Since the build log is quite long I have placed in pastebin:
https://pastebin.com/PhfYW3zm
The dockerfile which I am using comes from the official documentation, and that's why it comes to me as a surpise that it does not work:
https://learn.microsoft.com/en-us/visualstudio/containers/container-tools-react?view=vs-2022
The Dockerfile itself:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
RUN apt-get update
RUN apt-get install -y curl
RUN apt-get install -y libpng-dev libjpeg-dev curl libxi6 build-essential libgl1-mesa-glx
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash -
RUN apt-get install -y nodejs
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
RUN apt-get update
RUN apt-get install -y curl
RUN apt-get install -y libpng-dev libjpeg-dev curl libxi6 build-essential libgl1-mesa-glx
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash -
RUN apt-get install -y nodejs
WORKDIR /src
COPY ["WebApp/WebApp.csproj", "WebApp/"]
RUN dotnet restore "WebApp/WebApp.csproj"
COPY . .
WORKDIR "/src/WebApp"
RUN dotnet build "WebApp.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "WebApp.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApp.dll"]
Deleting the npm install tags from .csproj as suggested in this thread https://github.com/dotnet/sdk/issues/9593 by user PKLeso resolved the problem.
Edit:
This will delete frontend from your container completely if I remember correctly. However if you want to remain it within container just make sure that npm install on your frontend leaves no errors. Beacuse otherwise MSB3073 error occurs.

Kafka with .net core docker file config

My app is .net core 3.1 based. Trying to connect to kafka , publish and consume topics. Locally the code is working fine no error .Running the code in wsl2 ubuntu env using vs code. Now I want to create a docker image for the same code and publish in azure. The docker image is getting created but while trying to run the image getting error
No provider for SASL mechanism GSSAPI:Recompile librdkafka with libsasal2 or openssl support. Current build options:Plain Sasl_scram ouathbearer.
Please help with the docker file as what exactly to install and how to do so
Docker file :
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
WORKDIR /app
RUN apt-get update
RUN apt-get install -y sasl2-bin \
libsasl2-modules-gssapi-mit\
krb5-user \
libkrb5-dev \
libsasl2-dev\
libzstd-dev
RUN echo "Done"
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /src
COPY ["abc.csproj", "./"]
RUN dotnet restore "./abc.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "./abc.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "abc.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "abc.dll"]

Convert HTML to PDF using wkhtmltopdf, docker in ASP.NET Core

I'm trying to convert HTML to PDF using wkhtmltopdf in my ASP.NET Core app.
I've added a wkhtmltopdf.exe to my project and marked as a Copy to an output to always.
Here's my code:
var htmlContent = receiptBody;
var wkhtmltopdf = new FileInfo(#"/app/Configuration/Wkhtmltopdf/wkhtmltopdf.exe");
var converter = new HtmlToPdfConverter(wkhtmltopdf);
var pdfBytes = converter.ConvertToPdf(htmlContent);
The file has been founded but on:
var pdfBytes = converter.ConvertToPdf(htmlContent);
I'm getting an error:
System.Exception: Cannot generate PDF: Broken pipe --->
System.IO.IOException: Broken pipe
I run my app using Docker and here is my dockerfile:
FROM microsoft/aspnetcore:1.1.2 ARG source WORKDIR /app
ENV ASPNETCORE_URLS http://project-test:80
EXPOSE 80
COPY ${source:-obj/Docker/publish} .
RUN apt-get update
RUN apt-get install-y libgdiplus
ENTRYPOINT ["dotnet", "ProjectTest.dll"]
Maybe should somehow install wkhtmltopdf for linux?
This piece of code works fine for me with the latest .net core 6.0.1 image:
FROM mcr.microsoft.com/dotnet/aspnet:6.0.1-bullseye-slim AS base
RUN apt update
RUN apt install -y libgdiplus
RUN ln -s /usr/lib/libgdiplus.so /lib/x86_64-linux-gnu/libgdiplus.so
RUN apt-get install -y --no-install-recommends zlib1g fontconfig libfreetype6 libx11-6 libxext6 libxrender1 wget gdebi
RUN wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox_0.12.5-1.stretch_amd64.deb
RUN gdebi --n wkhtmltox_0.12.5-1.stretch_amd64.deb
RUN apt install libssl1.1
RUN ln -s /usr/local/lib/libwkhtmltox.so /usr/lib/libwkhtmltox.so
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0.1 AS build
WORKDIR /src
COPY ["docgen/docgen.csproj", "docgen/"]
RUN dotnet restore "docgen/docgen.csproj"
COPY . .
WORKDIR "/src/docgen"
RUN dotnet build "docgen.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "docgen.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "docgen.dll"]
It's all about platform and binaries.
You can generate your own docker image with WKHTMLTOPDF and use it.
In my opinion, it is a cleaner way.
Here is a simple example:
Download WkHtmlToPdf - Debian 10 (Buster) - amd64 file:
https://wkhtmltopdf.org/downloads.html
Put downloaded .deb file into "deps" subfolder below where the Dockerfile is
Add WkHtmlToPdf installation into the Dockerfile.
Make sure the download .deb filename matches the line:
ENV WKHTMLTOX wkhtmltox_0.12.6-1.buster_amd64.deb
And ensure the .NET Core images use Debian Buster.
Dockerfile:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /app
## copy csproj and restore as distinct layers
COPY *.sln .
COPY aspnetapp/*.csproj ./aspnetapp/
RUN dotnet restore
## copy everything else and build app
COPY aspnetapp/. ./aspnetapp/
WORKDIR /app/aspnetapp
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS runtime
WORKDIR /app
## START - INSTALL WKHTMLTOPDF
ENV WKHTMLTOX wkhtmltox_0.12.6-1.buster_amd64.deb
ENV BUILD_PACKAGES build-essential
ENV MAIN_PACKAGES fontconfig libfreetype6 libjpeg62-turbo libxext6 libpng16-16 libx11-6 libxcb1 libxrender1 xfonts-75dpi xfonts-base
COPY deps/$WKHTMLTOX ./
RUN set -xe \
&& apt-get update -qq \
&& apt-get install --no-install-recommends -yq $BUILD_PACKAGES $MAIN_PACKAGES \
&& dpkg -i ${WKHTMLTOX} \
&& apt-get remove -y $BUILD_PACKAGES \
&& apt-get autoremove -y \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
&& rm -rf ${WKHTMLTOX} \
&& truncate -s 0 /var/log/*log
## END - INSTALL WKHTMLTPDF
COPY --from=build /app/aspnetapp/out ./
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
The INSTALL WKHTMLTOPDF code is based on:
https://gist.github.com/berkayakcay/1e4f0a355437f0db9c94935aa52283d2
The Dockerfile itself is based on:
https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/docker/building-net-docker-images?view=aspnetcore-3.1#the-dockerfile-1
You should download files wkhtmltopdf for linux and copy to output publish.
Also, give permissions to these files with this step in dockerfile
RUN chmod 755 ./wkhtmltopdf/Linux/wkhtmltopdf
RUN chmod 755 ./wkhtmltopdf/Linux/wkhtmltoimage
Be sure that you add the line after WORKDIR/app:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /app
RUN apt-get update -qq && apt-get -y install libgdiplus libc6-dev
For more info check https://github.com/fpanaccia/Wkhtmltopdf.NetCore

Categories

Resources