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.
Related
I have a .net dashboard application where a user can export the current page as pdf via playwright.
This works locally, however, when I run the application in Docker I get this error:
Microsoft.Playwright.PlaywrightException: Executable doesn't exist at /root/.cache/ms-playwright/chromium-1005/chrome-linux/chrome
╔════════════════════════════════════════════════════════════╗
║ Looks like Playwright was just installed or updated. ║
║ Please run the following command to download new browsers: ║
║ ║
║ pwsh bin\Debug\netX\playwright.ps1 install ║
║ ║
║ <3 Playwright Team ║
╚════════════════════════════════════════════════════════════╝
Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /src
COPY ["Dashboard/Dashboard.csproj", "Dashboard/"]
RUN dotnet restore "Dashboard/Dashboard.csproj"
COPY . .
WORKDIR "/src/Dashboard"
RUN dotnet add package Microsoft.Playwright
RUN dotnet build "Dashboard.csproj" -c Release -o /app/build
RUN dotnet tool update --global PowerShell
RUN pwsh /app/build/playwright.ps1 install --with-deps
FROM build AS publish
RUN dotnet publish "Dashboard.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Dashboard.dll"]
The error came from me not correctly understanding Docker stages.
Here a fixed Dockerfile, where I installed Playwright in the base stage:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
RUN apt-get update
RUN apt-get -y install curl gnupg
RUN curl -sL https://deb.nodesource.com/setup_16.x | bash -
RUN apt-get -y install nodejs
RUN npx playwright install
RUN apt-get update && apt-get install -y --no-install-recommends \
fonts-liberation\
libasound2\
libatk-bridge2.0-0\
libatk1.0-0\
libatspi2.0-0\
libcairo2\
libcups2\
libdbus-1-3\
libdrm2\
libgbm1\
libglib2.0-0\
libgtk-3-0\
libnspr4\
libnss3\
libpango-1.0-0\
libx11-6\
libxcb1\
libxcomposite1\
libxdamage1\
libxext6\
libxfixes3\
libxrandr2
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /src
COPY ["Dashboard/Dashboard.csproj", "Dashboard/"]
RUN dotnet restore "Dashboard/Dashboard.csproj"
COPY . .
WORKDIR "/src/Dashboard"
RUN dotnet build "Dashboard.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Dashboard.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Dashboard.dll"]
Add the blow line after COPY --from=publish /app/publish .
RUN chown -R pwuser:pwuser /app
For anyone else, can find in document
After build success, have playwright.ps1 file created, just run it with powershell:
# dir: /your_source_folder
powershell.exe bin/Debug/netX/playwright.ps1 install
# or pwsh bin/Debug/netX/playwright.ps1 install
Or you can install special browser, like chrome, firefox:
powershell.exe bin/Debug/netX/playwright.ps1 install firefox
powershell.exe bin/Debug/netX/playwright.ps1 install chrome
Cache folders:
Windows: %USERPROFILE%\AppData\Local\ms-playwright
MacOS: ~/Library/Caches/ms-playwright
Linux: ~/.cache/ms-playwright
And add to "Post-build event" into csproj:
powershell.exe -Command $(TargetDir)playwright.ps1 install firefox;
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.
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"]
I have dotnet core 2.2 (aspnet core) app running in Docker container. I'm using the simplest possible Dockerfile you can find in any basic tutorial:
use microsoft/dotnet:2.2-sdk as base image
copy *.csproj
restore packages
build
publish to /app folder
use microsoft/dotnet:2.2.1-aspnetcore-runtime to run the app from /app folder
Now I'd like to grab some data from another website. It is a SPA, so I need to use a browser to render the page first - I decided to use Selenium with ChromeDriver because I'm already a little bit familiar with them.
I've added Selenium.WebDriver v3.141 and Selenium.WebDriver.ChromeDriver v73.0 to my project, set Selenium there. Locally on Windows it works fine. But when I run this via Docker I'm getting:
The file /app/chromedriver does not exist. The driver can be downloaded at http://chromedriver.storage.googleapis.com/index.html
So now I'm wondering how can I run Selenium + single instance Chrome (there is no need to set up Selenium Grid for my purpose) with dotnet core 2.2 in Docker.
I suppose I need to create custom Dockerfile which:
installs selenium, chrome and all their dependencies
installs dotnet
does the same as my current Dockerfile to build and run my app
But I'm not really sure how to do this. Especially how to "nest" Dockerfiles.
Should I do this composition in a single Dockerfile? Should I create Dockerfile for Selenium + ChromeDriver and use it as base image for next step?
So I recently had the same problem.
TL;DR; You have to install chrome into the docker image by putting the
commands in the Docker file.
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch
# Install Chrome
RUN apt-get update && apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg \
hicolor-icon-theme \
libcanberra-gtk* \
libgl1-mesa-dri \
libgl1-mesa-glx \
libpango1.0-0 \
libpulse0 \
libv4l-0 \
fonts-symbola \
--no-install-recommends \
&& curl -sSL https://dl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb [arch=amd64] https://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list \
&& apt-get update && apt-get install -y \
google-chrome-stable \
--no-install-recommends \
&& apt-get purge --auto-remove -y curl \
&& rm -rf /var/lib/apt/lists/*
# Add your dotnet core project build stuff here
Easier solution - I pushed this as a docker image in my docker hub repo so you can use it as your base image. See this example of my dotnet core 2.2
FROM masteroleary/selenium-dotnetcore2.2-linux:v2 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM masteroleary/selenium-dotnetcore2.2-linux:v2 AS build WORKDIR /src
COPY ["MyProject.csproj", ""]
RUN dotnet restore "MyProject.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "MyProject.csproj" -c Prod -o /app
FROM build AS publish
RUN dotnet publish "MyProject.csproj" -c Prod -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MyProject.dll"]
How did this happen?
Basically created a new project in visual studio for dotnet core 2.2 mvc with docker support.
Intentions are to run my dotnet core app in a linux container
Assumed that by installing nuget packages Selenium.Support, Selenium.WebDriver, Selenium.WebDriver.ChromeDriver anything I needed would be included in the docker container automatically since Selenium.WebDriver supports .NetStandard 2.0 (BTW the others don't, just realized that)
Turns out you have to install chrome into the docker image by putting the commands in the Docker file.
I've explained the whole learning process here including how I found this working code: https://hub.docker.com/r/masteroleary/selenium-dotnetcore2.2-linux
Since the appearance in dotnet core of self-contained applications I think a better approach is to use the official selenium docker:
https://hub.docker.com/r/selenium/standalone-chrome
and build the application self contained.
Here is my dockerfile:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 as build-env
WORKDIR /app
COPY . ./
RUN dotnet publish MyApp.csproj -c Release -o out --self-contained -r linux-x64 /p:PublishTrimmed=true
FROM selenium/standalone-chrome
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["./MyApp"]
this is updated version for dotnet 6.0
also This is a multi stage Docker File and help with faster build
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
RUN apt update && apt install -y \
apt-transport-https \
ca-certificates \
curl \
gnupg \
hicolor-icon-theme \
libcanberra-gtk* \
libgl1-mesa-dri \
libgl1-mesa-glx \
libpango1.0-0 \
libpulse0 \
libv4l-0 \
fonts-symbola \
--no-install-recommends \
&& curl -sSL https://dl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& echo "deb [arch=amd64] https://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list \
&& apt-get update && apt-get install -y \
google-chrome-stable \
--no-install-recommends \
&& apt-get purge --auto-remove -y curl \
&& rm -rf /var/lib/apt/lists/*
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
/* The .net App Docker Configuration */
FROM build AS publish
RUN dotnet publish -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
RUN echo ls -a
ENTRYPOINT ["dotnet", "{Entry Point File Name}.dll"]
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