Codementor Events

eShopOnContainers - Part 1 (Docker Compose)

Published Jan 25, 2023Last updated Feb 19, 2023
eShopOnContainers - Part 1 (Docker Compose)

eShopOnContainers is a reference application for .NET Core, developed by Microsoft, that utilizes a simplified microservices architecture and Docker containers. In this write-up, I aim to fill in some information gaps in their documentation. Among the repository's contents are two frontend web applications, webmvc and webspa, and I will provide a comprehensive overview of the webmvc end-to-end process.

flow.PNG

The below wiki link of eShopOnContainers repo covers the setup steps on Docker Desktop on windows.

Docker Compose Windows setup

If you, like me, have become disconnected from MVC, these four videos (each 20-30 minutes in length) can help you review fundamental MVC concepts such as RenderBody, Layout, Partial View, RenderPartial, and more.

ViewStart in MVC
Layout View in MVC
Partial Views in MVC
HTML Render and Render Partial in MVC

Additionally, the following video provides an explanation of how Envoy Proxy operates. While Envoy is commonly associated with Istio's service mesh, in this instance, it is employed as a standalone service rather than a sidecar.

API Gateway with Envoy

It's important to note another significant point before we begin debugging the application: the container exposes external and internal ports, which are specified as (external:internal).

container-ports.PNG

External port is useful for accessing outside of Docker compose for testing service individually. Services within Docker compose never use the external port for communication, instead, it uses the internal port.

Similarly, localhost is never used within the docker-compose as a network address. This might confuse you with logic - if the container can be accessed using localhost on an external port then why can't the other container within the docker network can access other services using localhost on the external port? So, stressing again, if you want to connect from one service to other services within docker-compose then use the container/service name. Docker network has the great capability to map container/service names with the correct IP address.

The network named bridge is a special network. Unless you tell it otherwise, Docker always launches your containers in this network. And if you want to know Which network is your application running under? Inspect the application to verify that it is running in the default bridge network.

$ docker inspect --format='{{json .NetworkSettings.Networks}}'  <container>

Example:

network-1.PNG

You can also get these details in the visual studio console.
network-3.PNG

Run eShopOnContainers locally (Development Environment)

docker-compose.yml: This file contains the definition of all images needed for running eShopOnContainers.
docker-compose.override.yml: This file contains the base configuration for all images of the previous file

The standard way to start eShopOnContainers from CLI is:

docker-compose -f docker-compose.yml -f docker-compose.override.yml

This will start eShopOnContainers with all containers running locally, and it is the default development environment.

Debugging and Code Walkthrough

Open eShopOnContainers-ServicesAndWebApps.sln in Visual Studio. Ensure Docker Compose is startup project and then start debugging. This will open webmvc app in browser(localhost:5100).

I am keeping break points at multiple lines across the solution. The below line of code indicates MVC default route is the Catalog controller of MVC app.

1.png

So, it's hitting the Index of Catalog controller and calling Catalog Service to get the data.

2.png

Now comes the interesting point, it shows MVC is trying to connect webshoppingapigw for data. If you browse the given URL in your local machine it won't work, however, if you use localhost and webshoppingapigw external port this will return the date.

3.png

The second important point in the above screenshot is, from where PurchaseUrl is coming, the reason being it's not available in appsetting.json.

4.png

The answer is PurchaseUrl is being passed to webmvc as an environment variable.

5.png

Now, you might be thinking there is no such app as webshoppingapigw. Then what is this URL? The answer is it's container running envoy proxy -

6.png

Below you can see port "5202:80", this is the listner configuration. You can use 5202 to test on localhost but the container will use port 80 to connect.

7.png

If you know envoy, you can understand we are defining \c\ as the route to the catalog cluster.

8.png

And the cluster is defined to use catalog-api:80 as a socket-address.

9.png

Fare enough, it's now hitting CatalogController of Catalog.API -

10.png

And api is returning the below data to webMVC -

11.png

And receiving the same in webMVC -

12.png

Now the data is available for the MVC view. We have the following screenshots which is used to return HTML to render in the browser.

The following cshtml files are used for html rendering -

13.png

The RenderBody is defined in Index.cshtml.

14.png

The _viewStart.cshtml page is a special view page containing the statement declaration to include the Layout page. Instead of declaring the Layout page in every view page, we can use the _ViewStart page.

15.png

RenderBody is where the index.cshtml content is placed.

16.png

Below is an example of a Partial view.

17.png

Discover and read more posts from DhananjayKumar
get started
post commentsBe the first to share your opinion
Show more replies