DEV Community


Deploy ASP.NET Core 2 apps with Docker

kingsor profile image Edoardo Inamo Originally published at ・4 min read

Originally published at 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 core web api in a container.


Building a web api with 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

# 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
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

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 .

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

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
docker tag todo-core-webapi neetpiq/todo-core-webapi:latest

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

docker push neetpiq/todo-core-webapi:1.2
docker push neetpiq/todo-core-webapi:latest

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

Or if you want a specific version:

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


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.


Discussion (0)

Editor guide