Skip to content

Running web apps in a Docker container

Running web apps in a Docker container requires a little more set up than just running your standard back end services. This method requires that your app be wrapped by .netCore as recommended by Microsoft when creating a SPA. This allows you to deploy everything as a simple DLL.

The only change that is required from a normal container deployment for a back end service is the container itself. Most front end frameworks these days use NodeJS. We either need to ensure that this is already installed on the container image we are using, or install it in the container.

A standard Docker file for building container images for a .netcore web API will look roughly like below

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
WORKDIR /src
COPY ["src/myAPI.csproj", "src/myAPI/"]
RUN dotnet restore "src/myAPI/myAPI.csproj"
COPY . .
WORKDIR "/src/src/myAPI"
RUN dotnet build "myAPI.csproj" -c Release -o /app

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

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

This set up will be familiar to anyone who has used Visual Studio to generate a Docker file for there app. The container uses the “mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim” template from Microsoft. It then copies files into the required directories, restores any packages that are needed and builds then publishes the app to a DLL.

Option A – Use a different template

The first option would be to use a different template that already includes Nodejs. You can find these hosted on GitHub and the like. You would need to use one the has both NodeJS and .netCore set up on it.

Option B – Install Node.js

If you want to use a template provided by Microsoft like the example above, you will need to edit your Docker file a bit. The commented change below will install node.js onto any container, so you can use it wherever you want.

FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build
# Setup NodeJs
RUN apt-get update && \
    apt-get install -y wget && \
    apt-get install -y gnupg2 && \
    wget -qO- https://deb.nodesource.com/setup_10.x | bash - && \
    apt-get install -y build-essential nodejs
# End setup
WORKDIR /src
COPY ["src/myAPI.csproj", "src/myAPI/"]
RUN dotnet restore "src/myAPI/myAPI.csproj"
COPY . .
WORKDIR "/src/src/myAPI"
RUN dotnet build "myAPI.csproj" -c Release -o /app

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

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

The docker file above will produce a container with .netCore and NodeJS installed. Your web application will now be able to be served from this container image.

Published inOperations

Comments are closed.