5

Recently I've created an aspnetcore-2.2 project from a react template, which, as i noticed, doesn't have docker support out of the box.

So I added dockerfile which was generated by VS'17:

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM microsoft/dotnet:2.2-sdk AS build
COPY ["SpaProj.WebUI/SpaProj.WebUI.csproj", "SpaProj.WebUI/"]
COPY ["SpaProj.Application/SpaProj.Application.csproj", "SpaProj.Application/"]
COPY ["SpaProj.Domain/SpaProj.Domain.csproj", "SpaProj.Domain/"]
COPY ["SpaProj.Persistence/SpaProj.Persistence.csproj", "SpaProj.Persistence/"]
RUN dotnet restore "SpaProj.WebUI/SpaProj.WebUI.csproj"
COPY . .
WORKDIR "/src/SpaProj.WebUI"
RUN dotnet build "SpaProj.WebUI.csproj" -c Release -o
FROM build AS publish
RUN dotnet publish "SpaProj.WebUI.csproj" -c Release -o /app

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

As far as my research goes, the latest dotnet images don't have nodejs pre-installed so the outcome was a foregone conclusion:

System.AggregateException: One or more errors occurred. (One or more errors occurred. (Failed to start 'npm'. To resolve this:.

[1] Ensure that 'npm' is installed and can be found in one of the PATH directories.
    Current PATH enviroment variable is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    Make sure the executable is in one of those directories, or update your PATH.

[2] See the InnerException for further details of the cause.)) ---> System.AggregateException: One or more errors occurred. (Failed to start 'npm'. To resolve this:. 

And so on.

After that, I was browsing web for like two complete days to resolve the issue, and after some research added few lines (which, as i thought, would help me to install/launch nodejs) into my dockerfile:

FROM microsoft/dotnet:2.2-aspnetcore-runtime AS Base
WORKDIR /app
EXPOSE 80 443
FROM microsoft/dotnet:2.2-sdk AS build

# install node and npm
ENV NODE_VERSION 10.16.0
ENV NODE_DOWNLOAD_SHA 2e2cddf805112bd0b5769290bf2d1bc4bdd55ee44327e826fa94c459835a9d9a
ENV NODE_DOWNLOAD_URL https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.gz
RUN wget "$NODE_DOWNLOAD_URL" -O nodejs.tar.gz \
    && echo "$NODE_DOWNLOAD_SHA  nodejs.tar.gz" | sha256sum -c - \
    && tar -xzf "nodejs.tar.gz" -C /usr/local --strip-components=1 \
    && rm nodejs.tar.gz \
    && ln -s /usr/local/bin/node /usr/local/bin/nodejs
RUN curl -sL https://deb.nodesource.com/setup_10.x |  bash -
RUN apt-get install -y nodejs
# install npm packages first, this is slow so we want to minimise the number of un-cached runs
WORKDIR /src/SpaProj.WebUI/ClientApp/
COPY SpaProj.WebUI/ClientApp/package.json .
COPY SpaProj.WebUI/ClientApp/package-lock.json .
RUN npm install
RUN npm audit fix
# restore dotnet before build to allow it sit to cache
WORKDIR /
COPY ["SpaProj.Application/SpaProj.Application.csproj", "src/SpaProj.Application/"]
COPY ["SpaProj.Domain/SpaProj.Domain.csproj", "src/SpaProj.Domain/"]
COPY ["SpaProj.Persistence/SpaProj.Persistence.csproj", "src/SpaProj.Persistence/"]
COPY ["SpaProj.WebUI/SpaProj.WebUI.csproj", "src/SpaProj.WebUI/"]
RUN dotnet restore src/SpaProj.WebUI/SpaProj.WebUI.csproj
# copy source files and build
COPY . /src
RUN dotnet build /src/SpaProj.WebUI/SpaProj.WebUI.csproj --no-restore -c Release
RUN dotnet publish /src/SpaProj.WebUI/SpaProj.WebUI.csproj --no-restore -c Release -o /app
# Copy compiled app to runtime container
FROM base AS final
COPY --from=build /app .
CMD ["dotnet", "SpaProj.WebUI.dll"]

But it didnt help at all. I also tried multistage build to gather dependencies from node image like this:

FROM 10.16.1-jessie as node-build
WORKDIR /src
COPY SpaProj.WebUI/ClientApp .
RUN npm install
RUN npm run build

The app was still throwing the same exception.

Did anyone had the same issue? Is there another was to dockerize this kind of app?

Kirill Runk
  • 159
  • 1
  • 6
  • Possible duplicate of [How to integrate 'npm install' into ASP.NET CORE 2.1 Docker build](https://stackoverflow.com/questions/51918919/how-to-integrate-npm-install-into-asp-net-core-2-1-docker-build) – Jim G. Aug 07 '19 at 17:52
  • This is why docker is not supported out of the box for the aspnetcore-react template. While I have not tried it, I believe the accepted method to deal with this is have 2 images, one for the ASP.NET backend and one for the front end (Node), and use docker compose to build and start both containers. I'd like to see how others solved this issue, because this is the exact reason I never bothered and jumped on Blazor the moment it was included in .NET Core – Adam Vincent Aug 07 '19 at 19:32
  • @AdamVincent Good point. How exactly I'm able to put back and front in two seperated images? Any tips on that? I also thinking to move on to Blazor eventually. But hey, if you're will always be picking easier way, how are you gonna grow? :) – Kirill Runk Aug 07 '19 at 19:46
  • I have asked one of my colleges, he might be able to dig up an end to end workshop on this topic, I'll share. In the mean time you can see some of the discussion I've had before [here on stackoverflow](https://stackoverflow.com/questions/44684179/add-angular-4-to-an-asp-netcore-project) but unfortunately, I never did arrive at a full end to end solution. – Adam Vincent Aug 07 '19 at 20:31
  • @AdamVincent, so I've stumbled on [this issue on github](https://github.com/dotnet/dotnet-docker/issues/930). There was a proposal to get nodejs in `base` image (which is a runtime build). So I did that, and it worked! But (!) it works only if you debugging your UI project by launching it from docker. Building it with `docker-compose build` in cli or visual studio fails (for now). ATM, I'm not sure what caused this, but we're moving there. Later I'll post an outcome. – Kirill Runk Aug 07 '19 at 22:23
  • update: So the problem with building my app with VisualStudio was due to I forgot to remove my database image & it now runs under VS with docker-compose as a startup. That's a good thing. But here comes the ugly(no bad): when I'm trying to build it with `docker-compose up` via CLI, it still fails with `exited with code 145` with a following message from the image `Did you mean to run dotnet SDK commands? Please install dotnet SDK from`. Any thoughts? – Kirill Runk Aug 07 '19 at 22:49
  • Sounds like you might have the wrong image. There's a few images for aspnetcore, the big thing to note, there's one ([SDK](https://hub.docker.com/_/microsoft-dotnet-core-sdk/)) for building the project and there's a couple options for running your published app. These [MS Docs](https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/docker/building-net-docker-images?view=aspnetcore-2.2) are the a good source for working with ASPNET Core and Docker – Adam Vincent Aug 08 '19 at 14:26
  • The thing that bugs me if I was using incorrect image, then why docker builds it with Visual Studio? – Kirill Runk Aug 08 '19 at 17:08

0 Answers0