This is the one more step to get a dotnetcore web app made available in a kubernetes cluster. The instructions here are the bare minimum to create an web api app and make it available for deployment in a container.

See this post for instructions on how to create a kubernetes cluster.

Create a sample app

mkdir SampleApp
cd SampleApp
dotnet new webapi 

Create the Docker image

There are multiple runtimes you can use, each with different license, sizes and requirements. We will use the alpine based images (they are smaller).

Note that even the building of the application will happen as part of building the docker image.

create a Dockerfile file:

FROM microsoft/dotnet:2.1-sdk-alpine3.7 AS build-env


# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM microsoft/dotnet:2.1-aspnetcore-runtime-alpine3.7
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "SampleApp.dll"]

Build the image

From within the SampleApp folder, where the Dockerfile has also been created on:

docker build --rm -f "Dockerfile" -t sampleapp:latest .


docker run -p 8080:80 --rm --name app sampleapp


Open a web browser and navigate to http://localhost:8080/api/values

You should get a response like:



Categories: TL;DR


Leave a Reply

Avatar placeholder