.NET Core Docker Image for SPA Applications

The problem is that the base image in your dockerfile (microsoft/aspnetcore:latest) does not have node installed.

So you have to install node so you can run the project. This is the dockerfile I came up with:

FROM microsoft/aspnetcore:2.0
ARG source
EXPOSE 80 5102
ENV ASPNETCORE_URLS http://*:80
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
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "Project.dll"]

Notice how on line 5 of the dockerfile I'm running a command to update apt-get. And then in line 8-9 node is installed to the docker image

There is still a problem, hot module replacement from webpack does not work. Not even a full refresh works. I'm still looking in to it.

UPDATE: so I looked into the hot module replacement problem, and it appears to be a limitation of docker for windows.

The workaround is to configure webpack so it can tell the browser to poll for changes on a determined amount of time. See this link to see how to configure it

UPDATE: Doing a little more research I found out that microsoft has an image you can use to build your project, it is called: microsoft/aspnetcore-build. This image has all the dependencies you need for building (including nodejs).

So at the end, what I did was leave my Dockerfile as it was (with microsoft/aspnetcore:2.0 as base image), and created a new Dockerfile for development which references the build image I mentioned before. With the help of docker compose I switch Dockerfiles depending on the environment.

This approach seems more convenient because when images are deployed to production environment they should have all its javascript code ready (in the case of a spa application with angular 2, react, etc), in other words they should not have a nodejs dependency, making them less heavy in size.


Based on @Daniels answer above, running Visual Studio 2017 v15.4 and ASP.NET Core 2.0 on Docker, here are the changes you need to make to allow correct Production and Development behavior for SPA applications (in my case I'm using Angular):

  • Add a new Dockerfile to your project which is a copy of the original. Lets call it Dockerfile.Development. Modify as follows:

    FROM microsoft/aspnetcore:2.0
    ARG source
    
    # BEGIN MODIFICATION - Node is needed for development (but not production)
    RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
    RUN apt-get install --assume-yes nodejs
    # END MODIFICATION
    
    WORKDIR /app
    EXPOSE 80
    COPY ${source:-obj/Docker/publish} .
    ENTRYPOINT ["dotnet", "MyService.dll"]
    
  • Modify the docker-compose.override.yml file in your solution to use this new dockerfile in development. It'll look something like this:

    version: '3'
    
    services:
      myservice:
        environment:
          - ASPNETCORE_ENVIRONMENT=Development
        ports:
          - "80"
        build:
          dockerfile: Dockerfile.Development
    
  • Modify the webpack.config.js file in your project to enable watching for changes, as follows:

    const clientBundleConfig = merge(sharedConfig, {
      entry: { 'main-client': './ClientApp/boot.browser.ts' },
      output: { path: path.join(__dirname, clientBundleOutputDir) },
      // BEGIN MODIFICATION
      watch: isDevBuild,
      watchOptions: {
        poll: isDevBuild ? 1000 : false
      },
      // END MODIFICATION
      plugins: [
        new webpack.DllReferencePlugin({
            context: __dirname,
            manifest: require('./wwwroot/dist/vendor-manifest.json')
        })
      ].concat(isDevBuild ? [
    

I got the Angular example template to work simply by installing nodejs into the base:

FROM microsoft/aspnetcore:2.0 AS base
RUN apt-get update && \
    apt-get install -y wget && \
    apt-get install -y gnupg2 && \
    wget -qO- https://deb.nodesource.com/setup_6.x | bash - && \
    apt-get install -y build-essential nodejs
WORKDIR /app
EXPOSE 80
... rest of Dockerfile ...

Everything else (Webpack Hot swap) didn't throw an error.