Run simple ASP.Net Core Web Application on Docker
Tags: csharp, dotnet, docker, aspnet
Published: 2020-09-07 18:13:01

---
title: Run simple ASP.Net Core Web Application on Docker
published: true
description: 
tags: csharp,dotnet,docker,aspnet
//cover_image: https://direct_url_to_image.jpg
---

I try to taranslate my article, which is Japanes, to English.
https://qiita.com/orange634nty/items/ad81ded6545fc1c90a81

## What is this?

I have interested on C# server after reading [this article](https://tech.cygames.co.jp/archives/3181/).
So I try to create a very simple ASP.NET Core application run 
 on docker.

## Environment

- os: macOSX Mojave
- dotnet: version 2.2.101
- docker: version 18.09.0

## Create project

First I create ASP.NET Core project.
Use [dotnet new](https://docs.microsoft.com/ja-jp/dotnet/core/tools/dotnet-new?tabs=netcore21) command to create `ASP.NET Core Empty` from template.

Next open`App.cs` file and add `UseUrls("http://*:5000")`.
If you don't add this you only access to application from localhost.
[ASP.NET Core Web Host#Server URLs](https://docs.microsoft.com/ja-jp/aspnet/core/fundamentals/host/web-host?view=aspnetcore-2.2#server-urls)

```csharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace csharp_dotnet_server_docker
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseUrls("http://*:5000")  // <- add this!!
                .UseStartup<Startup>();
    }
}
```

Now application is ready.

## Setup Docker

Create a `Dockerfile`, which base image is [microsoft/dotnet](https://hub.docker.com/_/microsoft-dotnet-core).

```Dockerfile
FROM microsoft/dotnet

WORKDIR /app
COPY . /app
```

Create a `docker-compose.yaml` file like below.

```yaml
version: "3"
services:
  app:
    build: .
    container_name: "csharp-dotnet-server-docker"
    ports:
      - 5000:5000
    volumes:
      - .:/app
    command: ["dotnet", "watch", "run"]
```

I set `command: ["dotnet", "watch", "run"]`.
`dotnet watch run` command will run app, and watch file change.
If there is change the task will rebuild and rerun application automatic. It very usefull on developing.

## Build and run docker

Build image and run using docker-compose command.

```console
$ docker-compose build
# wait for build

$ docker-compose up -d
# wait for start

$ docker logs csharp-dotnet-server-docker
watch : Polling file watcher is enabled
watch : Started
Hosting environment: Development
Content root path: /app
Now listening on: http://[::]:5000
Application started. Press Ctrl+C to shut down.
```

After start running docker container, open `http://localhost:5000` on your browser.
You could see some message like this.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rwza6xym9d3r0sojnzxd.png)

## At the end

I was able to run an ASP.NET web application on docker quite easily.
In the future, I'd like to create a sample application using a DB on docker.

Code which use for this article is on [GitHub](https://github.com/orange634nty/csharp-dotnet-server-docker)

thanks for reading!