DEV Community

Edoardo Inamo
Edoardo Inamo

Posted on • Originally published at kingsor.github.io

Deploy ASP.NET Core 2 apps with Docker

Originally published at https://kingsor.github.io/ on Apr 30, 2018

This post is a note to self about my first steps in the path for learning to use Docker. I started deploying an asp.net core web api in a container.

Prerequisites

Building a web api with asp.net 2.0

I created a simple todo list web api following this tutorial: Create a Web API with ASP.NET Core and Visual Studio for Windows.

In order to be able to test the web api without any specific client I added the Swagger support to the project using NSwag package.

I accomplished this task following this tutorial: Get started with NSwag and ASP.NET Core.

The NSwag project provides tools to generate Swagger specifications from existing ASP.NET Web API controllers and provide an embedded Swagger UI to interact with the web api.

The resulting project is TodoCoreWebApi hosted on GitHub.

Adding a Dockerfile

The following is the Dockerfile for building the docker image.


FROM microsoft/dotnet:2.0-sdk AS build-env
WORKDIR /app

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

# copy everything else and build
COPY ./TodoCoreWebApi/. ./
RUN dotnet publish -c Release -o out /p:PublishWithAspNetCoreTargetManifest="false"

# build runtime image
FROM microsoft/dotnet:2.0-runtime
WORKDIR /app
COPY --from=build-env /app/out .
# not valid for Heroku
# ENTRYPOINT ["dotnet", "TodoCoreWebApi.dll"]
# this is working
CMD ASPNETCORE_URLS="http://*:$PORT" dotnet TodoCoreWebApi.dll

Enter fullscreen mode Exit fullscreen mode

To understand the ins and out of building a Docker image I followed this tutorial: Building Docker Images for .NET Core Applications. I used the Dockerfile from the aspnetsample project available on the .NET Core Docker Samples repository on GitHub.

Building the Docker image

In the root of the project (where the Dockerfile is located) run this command:


docker build -t todo-core-webapi .

Enter fullscreen mode Exit fullscreen mode

After the build process is completed you can run the image with this command:


docker run -it --rm -p 8080:80 --name TodoCoreWebApi todo-core-webapi

Enter fullscreen mode Exit fullscreen mode

And test the web api at http://localhost:8080

Pushing the image to DockerHub

First, you need to register for a Docker ID.

After that you can create a new repository on DockerHub.

My Docker ID is neetpiq and the repository for the image I generated is neetpiq/todo-core-webapi.

Now, I need to tag my image.

docker tag todo-core-webapi neetpiq/todo-core-webapi:1.2
Enter fullscreen mode Exit fullscreen mode
docker tag todo-core-webapi neetpiq/todo-core-webapi:latest
Enter fullscreen mode Exit fullscreen mode

Finally, I can push the new version of my image.

docker push neetpiq/todo-core-webapi:1.2
Enter fullscreen mode Exit fullscreen mode
docker push neetpiq/todo-core-webapi:latest
Enter fullscreen mode Exit fullscreen mode

Now you may test this web api application image (latest) running this command:


docker run -d -p 8080:80 --name TodoCoreWebApi neetpiq/todo-core-webapi

Enter fullscreen mode Exit fullscreen mode

Or if you want a specific version:


docker run -d -p 8080:80 --name TodoCoreWebApi neetpiq/todo-core-webapi:1.2

Enter fullscreen mode Exit fullscreen mode

Notes

Windows Users

If you are using boot2docker on Windows (Docker Toolbox), please note the following:

The Linux VM in the boot2docker VirtualBox maps the c/Users directory in the VM instance to the C:\Users folder in Windows. So be sure your source code for your worker is in a folder under C:\Users, then cd to that folder in the context of the VM (in Boot2Docker terminal) and run it from there.

Resources

Top comments (0)