Skip to content

ASP.NET Core WebAPI Containerization

Hardik Mistry edited this page Apr 12, 2018 · 1 revision

NOTE: Code snippet contains comment starting with a # [hash] symbol, remove the comment to avoid execution issues.

Create a ASP.NET Core WebAPI

Create a Dockerfile for an ASP.NET Core application

  1. Create a Dockerfile in your project folder.
  2. Add the text below to your Dockerfile for either Linux or Windows Containers. The tags below are multi-arch meaning they pull either Windows or Linux containers depending on what mode is set in Docker for Windows. Read more on switching containers.
  3. The Dockerfile assumes that your application is called aspnetapp. Change the Dockerfile to use the DLL file of your project.
FROM microsoft/aspnetcore-build:2.0 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/aspnetcore:2.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "your-app-name.dll"]
  1. To make your build context as small as possible add a .dockerignore file to your project folder and copy the following into it.
bin\
obj\

Build and run the Docker image

Open a command prompt and navigate to your project folder. Use the following commands to build and run your Docker image:

docker build -t your-app-name . # (. [dot] is required not a typo)
docker run -d -p 8080:80 --name image-name your-app-name

View the web page running from a container

Go to http://localhost:8080 to access your app in a web browser. If you are using the Nano Windows Container and have not updated to the Windows Creator Update there is a bug affecting how Windows 10 talks to Containers via “NAT” (Network Address Translation). You must hit the IP of the container directly. You can get the IP address of your container with the following steps:

Run docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" image-name

Copy the container ip address and paste into your browser. (For example, 172.16.240.197)

Edit application and refresh image

docker build -t your-app-name .
docker stop container-name
docker rm container-name
docker run -d -p 8080:80 --name image-name your-app-name

Push Images to a Container Registry