Containerizing a Dotnet Core MVC App in an Alpine Linux Docker Image

Creating a dotnet core mvc container is easy with this Dockerfile.

-

This is an early draft of a quick attempt at containerizing a Dotnet Core MVC app.

Creating a custom docker image with a docker file

First we'll create a new web mvc solution.

dotnet new mvc -n docker-mvc
cd docker-mvc/

Let's write up a quick dockerfile:

# Dockerfile
+ FROM mcr.microsoft.com/dotnet/core/sdk:2.1 AS build-env
+ WORKDIR /app
+ COPY *.csproj ./
+ RUN dotnet restore
+ COPY . ./
+ RUN dotnet publish -c Release -o /bin/alpine -r alpine.3.7-x64
+  
+ FROM microsoft/dotnet:2.1-runtime-alpine3.7
+ RUN mkdir -p /root/src/
+ WORKDIR /root/src/
+ COPY --from=build-env /bin/alpine .
+ ENTRYPOINT [\"dotnet\", \"docker-mvc.dll\"]

Let's be careful not to muddy up our container image by bring over any dotnet core build artifacts.

# .dockerignore
+ bin/
+ obj/
+ out/
+ TestResults/

Let's build it!

docker build \
  --tag alpine-mvc-img .
docker run \
  --name mvc-container \
  --publish 80:80 \
  --detach \
  alpine-mvc-img
curl localhost
docker stop mvc-container
docker rm mvc-container 
docker rmi alpine-mvc-img

Rich Minchuk

Technology Enthusiast and Wannabe Growth Hacker