.Net Core and Docker

By James Kenny
2 min read

Table of Contents

Building .net core applications into Docker containers. I've spent a bit of time lately moving some of my older applications into docker containers in dotnet core 2.0 and 2.1.

I've only started exploring docker with dot net core and what we can do and so far it really has changed how I think about what I'm doing and the way I am building things.

Demo site: https://dotnetdocker.serversncode.com/ Source code for this site is available https://github.com/james-kenny/Serversncode-Docker-demo

With .Net Core being on the edge things change. I've started this post and the https://dotnetdocker.serversncode.com as a way to play around with docker and understand it more.

I've hosted the site on a linode server. For more on setting up linode with docker check out setting up docker on linode and nginx reverse proxy for docker.

Working with Docker in .Net Core

With a dotnet core app we need to add a dockerfile to the project. This will do the build of our application and load it into the docker container and build it.

Docker File

.Net Core 2.0

While 2.0 is not going to be supported in the long term it's where I'll start with that.

FROM microsoft/aspnetcore:2.0 AS base
WORKDIR /app
EXPOSE 80

FROM microsoft/aspnetcore-build:2.0 AS build
WORKDIR /src
COPY *.sln ./
COPY ServersncodeDemo.Web/ServersncodeDemo.Web.csproj .Web/
RUN dotnet restore

COPY . .
WORKDIR /src/ServersncodeDemo.Web
RUN dotnet build ServersncodeDemo.Web.csproj -c Release -o /app

FROM build AS publish
RUN dotnet publish ServersncodeDemo.Web.csproj -c Release -o /app

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

.Net Core 2.1

Next we move to .net core 2.1 which will be supported long term.

FROM microsoft/dotnet:2.1.402-sdk-alpine3.7 AS build
WORKDIR /app

# copy csproj and restore as distinct layers
COPY *.sln .
COPY ServersncodeDemo.Web/*.*.csproj ./ServersncodeDemo.Web/
COPY ServersncodeDemo.Data/*.*.csproj ./ServersncodeDemo.Data/
RUN dotnet restore

# copy everything else and build app
COPY ServersncodeDemo.Web/. ./ServersncodeDemo.Web/
COPY ServersncodeDemo.Data/. ./ServersncodeDemo.Data/
WORKDIR /app/ServersncodeDemo.Web
RUN dotnet publish -c Release -o out

FROM microsoft/dotnet:2.1.4-aspnetcore-runtime-alpine3.7  AS runtime
WORKDIR /app
COPY --from=build /app/ServersncodeDemo.Web/out ./
ENTRYPOINT ["dotnet", "ServersncodeDemo.Web.dll"]

Docker Build

I have a blog post https://serversncode.com/docker-commands/ that covers common commands.

First up we build our docker image and give it a name.

docker build -t NAME .

Next we run it locally.

docker run --name NAME -d -p 8080:80 NAME

That's it, I've run a container and pointed the port 8080 to the default port of 80. In my docker file I never changed the port from 80. To change the port of your docker file use the

EXPOSE 8040

command to tell it to use a different port than 80.

What next

Next I'll start getting into the details of the docker file how it's created and what it all means.

Tagged in:

dotnet, dotnetcore, Docker, .NET

Last Update: September 09, 2019

About the Author