Host Core Application in Docker Container

How to host Core application in docker container without installing Nginx

Category: Docker Tags: ASP.NET Core 2, Container, Docker

Code and Configuration files

    In previous article, we hosted application on EC2 instance. We created an instance, installed dotnet core and Nginx. In this article, we will host same application on Docker. You can learn about Docker in my previous articles, given links below:

  1. What is Docker and how to run a python script on Docker container
  2. Pushing a custom image to DockeHub and creating container on Azure using an image

You must go through above articles and I recommend you to practice yourself if you have no idea about Docker.


  1. Visual studio installed
  2. Docker Installed

Step 1: Create Core application to run on port 80

    I will use same application from previous article but instead of running it on http://localhost:5000 I will configure to run it on http://*:80 so it will listen all http requests on port 80 and run without any reverse proxy like Nginx.

Change Program.cs as below:

public static void Main(string[] args)
    //using kestrel and iis both
    /*CreateWebHostBuilder(args).UseKestrel().UseIIS().UseUrls(new string[] { "http://localhost:5000" }).Build().Run()*/;
    CreateWebHostBuilder(args).UseKestrel().UseIIS().UseUrls(new string[] { "http://*:80" }).Build().Run();

You can see I have changed the port, now in configuration files I change the url to:


  "Logging": {

    "LogLevel": {

      "Default": "Warning"



  "AllowedHosts": "*",

  "AppConfig": {

    //"Url": "http://localhost:5000"

    "Url": "http://localhost"



Do same for appsettings.Development.json, locate project directory in command prompt and give command “dotnet run”. You could see application running on http://[::]:80 .


Running Core Application on port 80
Fig 1: Running Core Application on port 80


Now create a folder called “docker” somewhere and inside it, create one more folder called “published” to publish our project. Right click on solution explorer and publish the code to this "published" folder. My Folder Structure is C:\ -> docker -> published.

Step 2: Create DockerFile

    We need to create a DockerFile to build a Docker image. Go to folder where you published the code, create a file named DockerFile without any extension and paste below text in file:


#Create folder www-hosting in docker container

RUN mkdir /www-hosting

#copy current directory files from local machine to docker's www-hosting folder

COPY / /www-hosting

#setting working directory in container

WORKDIR /www-hosting

#run dotnet application

ENTRYPOINT ["dotnet", "CoreAppSample.dll"]

Step 3: Build Docker image

    To build Docker image, open command prompt and locate directory where you have DockerFile and published code. Now run the command:

docker build -t coreappsample .

“coreappsamle” is just tagging a name to image. You will see below output on console:


Building Docker Image
Fig 2: Building Docker Image

Step 4: Run the image in Docker

    Now you can run image “coreappsample” by command given below:

docker run -it --rm -p 5000:80 --name aspnetcore coreappsample

Above command runs the image and have some parameters, -it for interactive, -rm to remove if any container with same name exist and -p to map the ports. We are mapping port 5000 of local machine to 80 of container where our application is running. You will get output: Core Application running Docker Container
Fig 3: Core Application running Docker Container


You can see application is running on port 80 inside container. Now hit http://localhost:5000 in browser you will be able to access the application:


Output of Core Running on Docker
Fig 4: Output of Core Running on Docker

Run Using docker-compose.yml file:

    You must be thinking why I created published folder inside docker folder. Why didn’t put everything inside docker folder itself. I would like to create a yml file which can be used to configure image, port, rules etc. instead of passing parameters in command which is sometimes frustrating to remember and configure.

Let’s create docker-compose.yml file inside C:\docker folder which is one level up where DockerFile and Code exists. Below is the text I have written in yml file:

version: '2'




   context: published

   dockerfile: published\DockerFile


   - "5000:80"

We can see we defined version of docker-compose installed in machine, you can find version of docker-compose by command “docker-compose -v”. Second line creates a service, and the third line is name of the service, can be given anything. Then build tag has context to define folder where code exists and deockerfile tag to define location of docker file. And importantly port mapping from 5000 to 80 which we gave in command in previous section. Now we locate our command prompt to C:\docker and give command:

Docker-compose up

This will build the image and run it on defined ports mentioned in yml file:


Running Docker container using YML file
Fig 5: Running Docker container using YML file


You can again hit browser with http://localhost:5000 to see application's output.

Note:- We were not required to use Nginx here as we did previously in hosting this application to EC2. Because application were listening on http://localhost:5000 inside the instance but instance were listening on 80 port of Nginx, so we routed any requests of port 80 to http://localhost:5000 . Instance is powerful machine and can run different applications on different ports within it so using reverse proxy is a wise choice, additionally Nginx comes with advance features which kestrel doesn’t support but it doesn’t mean we can’t deploy an application without reverse proxy and this article demonstrate it.

Like 0 People
Last modified on 20 May 2019
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 143
Questions: 12
Given Best Solutions: 12 *


No Comments Yet

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User