Click here to Skip to main content
15,901,666 members
Articles / Containers / Docker
Tip/Trick

Using Docker-Compose for Defining and Running Multi-container application with ASP.NET Core MVC, Web API and MongoDB

Rate me:
Please Sign up or sign in to vote.
3.15/5 (5 votes)
8 Jan 2018CPOL3 min read 15.9K   151   8   3
Using Docker-Compose for defining and running multi-container application with ASP.NET Core MVC, Web API and MongoDB

Introduction

This sample demonstrates how to use docker-compose to host multi container application with the ASP.NET Core MVC, WebAPI and MongoDB.

Prerequisites

You will need to have prior knowledge of ASP.NET Core MVC, Docker and MongoDB.

The sample project requires the following tools to be installed on Windows platform:

  • Visual Studio 2015 or 2017
  • .NET Core 1.1 SDK
  • Docker Community Edition or higher
  • Configure Docker to use Linux Containers to support

Docker Compose?

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Using Compose is basically a three-step process:

  1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere
  2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment
  3. Lastly, run docker-compose up and Compose will start and run your entire app

A docker-compose.yml looks like this:

version: '2'
services:
   MobileCart:
    image: mobilecart/dockerdemo
    container_name: "mobilecart"
    ports:
     - "8080:80"
    volumes:
     - C:\DockerLogs\MobileCart:/app/Logs

   CustomerRestAPI:
    image: customerserviceapi/dockerdemo
    container_name: "customerrestapi"
    ports:
     - "13402:80"
    volumes:
     - C:\DockerLogs\MobileCart:/app/Logs

   InventoryRestAPI:
    image: inventoryserviceapi/dockerdemo
    container_name: "inventoryrestapi"
    ports:
     - "13477:80"
    volumes:
     - C:\DockerLogs\InventoryService:/app/Logs

Sample Project Overview

The sample project code is designed to mock the order processing of ecommerce application. The project consists of 6 applications:

  1. Mobile Cart: It is a user-facing event-driven application, where the user places the order. It is developed using ASP.NET Core MVC Application.

The below RESTFul Services take the request from the Mobile Cart application and return the appropriate response.

  1. Inventory Service: Accepts the product Id and returns the product info fetched from the products collection in MongoDB datasource
  2. Customer Service: Accepts the customer Id and returns the customer info fetched from the customer collection in MongoDB datasource
  3. Order Approval Service: Validates the order and returns the order approved status
  4. Shipping Service: Accepts the orderId and returns the shipping status of the Items
  5. Notification Service: Accepts the orderId and sends the notification once the items are delivered

Please refer to the sequence diagram below to understand the flow of request in the sample project.

Sequence Diagram of the Sample Application

Image 1

Running the Sample

Running the sample requires:

  1. Downloading and Installing the .NET Core 1.1 SDK and Docker Community Edition
  2. Downloading the sample application
  3. Publishing each project to its respective "/bin/Release/PublishOutput" folder
  4. Setting up the shared drives and proxy in docker client
  5. Building container images and
  6. Finally starting the multi-container application using docker-compose

Firstly, install the .NET Core 1.1 SDK and Docker Community Edition.

Next, download and unzip the sample application into C:/ or D:/ drive of your choice.

Create a folder for logs, e.g., C:\DockerLogs\MobileCart.

Setup the share drive on docker client: Goto docker client-> Shared Drives -> Select drive -> Click Apply -> If prompted for credentials -> Enter the credentials -> Click Ok

If Proxy setup is required: Goto docker client -> Proxies -> Select Manual proxy configuration ->. Enter the proxy details [<username>:<password>@<proxy>:<port>]-> Click Apply .

Building Container Images

Before running the multi container application, first custom container images have to be built for the applications in the sample.

Open Powershell and type the following commands:

// cd C:\MobileCart\MobileCart
// docker build -t mobilecart/dockerdemo .

Note: [docker build] command uses the Dockerfile in the project folder to build the image.

Repeat the same steps to build the images for CustomerServiceAPI, InventoryServiceAPI, rderApprovalServiceAPI, ShippingService API and NotificationServiceAPI Projects.

And to download the mongodb image from docker hub, type:

// docker pull mongo

To check whether all the images are available, type:

// docker images

This should list all the images.

Image 2

Run the Multi-container Application Using docker-compose

Finally, to run the multi container application, enter the command:

// cd C:\MobileCart\MobileCart
// docker-compose up

Note: [docker-compose] command uses the docker-compose.yml file in the mobilecart project folder to bring all the services up and running.

The above command will bring all the containers up and running.

Now, browse http://localhost:8080/Home/Index to run the sample application.

Image 3

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Software Developer

Comments and Discussions

 
BugZip file missing files Pin
dapc7411-Jan-18 7:10
professionaldapc7411-Jan-18 7:10 
QuestionImage problems Pin
Nelek7-Jan-18 8:27
protectorNelek7-Jan-18 8:27 
AnswerRe: Image problems Pin
Manjesh_A8-Jan-18 4:54
Manjesh_A8-Jan-18 4:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.