DEV Community

orange634nty
orange634nty

Posted on

Run simple ASP.Net Core Web Application on Docker

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.
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 command to create ASP.NET Core Empty from template.

Next openApp.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

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.

FROM microsoft/dotnet

WORKDIR /app
COPY . /app

Create a docker-compose.yaml file like below.

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.

$ 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

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

thanks for reading!

Top comments (1)

Collapse
 
emmysteven profile image
Emmy Steven

Hi ๐Ÿ‘‹๐Ÿป

Your article has been helpful, however I discovered a little bit of typos and grammatical error in your article.

I'll love to volunteer to help proof read your work before you publish.
Above all your article is great!

Keep it up!