Missing node_modules folder in published asp.net core docker container

Recently I started working on my first asp.net core application using docker. I created the project in visual studio and added support for docker-compose as an orchestrator. This created a docker-compose project and also added dockerfile to my web project.

Below is the docker-compose project

ScreenClip

and the dockerfile added to the project.

FROM mcr.microsoft.com/dotnet/core/aspnet:3.0-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
WORKDIR /src
COPY ["MyApp/MyApp.csproj", "MyApp/"]
COPY ["MyApp.Services/MyApp.Services.csproj", "MyApp.Services/"]
RUN dotnet restore "MyApp/MyApp.csproj"
COPY . .
WORKDIR "/src/MyApp"
RUN dotnet build "MyApp.csproj" -c Release -o /app/build

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

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

Using this I was able to work locally where visual studio took care of everything e.g. building images and running containers in the background whenever I built and ran/debug the application. The experience is almost the same as you are developing without containers. In nutshell there is a lot of magic visual studio does in the background which if you are not fully aware might become a problem later on.

In my case when I was fully done with my local development and created a pipeline to build and deploy containers using azure DevOps pipeline. I faced the problem where while running my application I started getting …System.IO.DirectoryNotFoundException: /app/node_modules/

ScreenClip

The reason for this was that while building the image using dockerfile I was only publishing the dot net core project but was never generating the npm packages and hence the node_modules was missing.

So being a newbie now I had 2 questions

1. Why this never happened while I was working locally on my solution?… It should ideally be same since containers are same everywhere

2. How to fix this?

Answer to the first question came from the this page which give details regarding visual studio container tools.

Basically, when you are working in ‘debug’ configuration in visual studio it does not copy your app to the container but instead mounts the volume and maps the app source code from there. This is to provide you with an iterative experience while developing the app. It means that you can change your source code and build the app without recreating the containers. Actually this is excellent and the experience is the same as running the app from VS in IIS express. But this also means that docker images generating in debug configurations should not be pushed to a private or public registry for deployments as it does not have the app contents.

For publishing images, we need to use ‘Release’ configuration. And once I did that I got the same exact error message in visual studio as well.

ScreenClip

Now let’s find the answer to the 2nd question and fix this stuff. The specific error which I got was because I was using below line in my code and that’s where the code was breaking.You can see what this line does here.

ScreenClip

But that is not the root cause. Even if we don’t use this package this error will manifest in some other way since we are missing the npm packages. To fix this, I basically made sure that while building the image we install node.js first then do an npm install and finally copy the generated node_modules folder in the final published image. Following are the changes made to dockerfile.

ScreenClip

This works perfectly. Although I am not sure if this is the most efficient approach but this works well. An alternative could be using a base image which already has node.js installed but as of now, I don’t see any of the official Microsoft asp.net core images having this. Also, it’s possible we create a base image with node.js installed and publish it to a registry and then use that in our dockerfile.

For more details on building asp.net core applications using docker containers you can refer this excellent course on pluralsight by wes higbee

Tagged on: ,

One thought on “Missing node_modules folder in published asp.net core docker container

  1. Pingback: .Net Core 3.1 Azure App Service startup exception: System.IO.DirectoryNotFoundException – Sql2Go Consulting

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.