Getting Started with containers for .NET

Building, shipping and running .NET apps in containers

Alistair Chapman

@agc93

.NET
For
Hipsters

Alistair Chapman

@agc93

Who am I?

Alistair Chapman

 

agc93

 

agc93

.NET MVP

Information Security Architect @ Red Hat

Walking, talking case of impostor syndrome

Docker

  • Container technology with process and environment isolation

 

  • Works on Linux, Windows (mostly) and macOS (sort of)

.NET Core

  • The next generation of the .NET platform across every platform

 

  • Works on Windows, macOS (mostly) and Linux (sort of)

Working with Docker

The Essentials

  • Containers!
  • Images!
  • Buzzwords!

Building with Docker

  • Docker can also be used interactively
  • Creates isolated, disposable environments
  • Perfect for builds, packaging and cross-platform testing

Adding Docker to the Mix

  • Use a Dockerfile to build an image
  • You can then run your image to run your app in a container
  • Your base image decides the environment
FROM microsoft/dotnet:2.0-sdk

COPY . /app

WORKDIR /app

RUN ["dotnet", "restore"]

RUN ["dotnet", "build"]

EXPOSE 5000/tcp

ENV ASPNETCORE_URLS="http://*:5000"

CMD ["dotnet", "run"]
FROM microsoft/dotnet:2.0-sdk AS build-env

COPY . /app
WORKDIR /app
RUN dotnet restore
RUN dotnet publish -c Release -o out

FROM microsoft/dotnet:2.0-runtime

WORKDIR /app
COPY --from=build-env /app/out .

EXPOSE 5000/tcp
ENV ASPNETCORE_URLS="http://*:5000"
CMD ["dotnet", "demo.dll"]

Adding Docker to the Mix

  • Newer Docker versions add multi-stage build support
FROM microsoft/dotnet:2.0-sdk AS build-env
WORKDIR /app

# 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.0-runtime 
WORKDIR /app
COPY --from=build-env /app/out ./
ENTRYPOINT ["dotnet", "dotnetapp.dll"]
FROM microsoft/dotnet:2.0-sdk AS build-env
WORKDIR /app

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

# copy everything else and build
COPY . ./
RUN dotnet publish -c Release -r linux-x64 -o out

# build runtime image
FROM fedora:26
WORKDIR /app
RUN dnf install -y libicu libunwind
COPY --from=build-env /app/out ./
ENTRYPOINT ["./dotnetapp"]

Building with Docker

You can use containers to:

  • Bootstrap
  • Build
  • Package
  • Test
  • Deploy

Publishing Docker Images

Building FOR Docker

Doing What The Cool Kids Do

Building Cloud-Native Apps in Containers

Building FOR Docker

  • Requires a new approach to architecture
    • Loose coupling
    • Isolation and autonomy
    • Stateless
    • Discovery and composability
  • Best gains from apps designed as "cloud-native"
  • Extremely well-suited to microservices

Building FOR Docker

Building your app for containers

Building FOR Docker

Building your app for containers

Credit: Michael Ducy

Alistair Chapman

@agc93

(essentially everywhere)

 

 

https://agchapman.com/talks

https://blog.agchapman.com/