Building a Simple PaaS with Sidecars (from Designing Distributed Systems book) in C#
Tags: architecture, containers, distributedsystems, git
Published: 2018-03-05 14:01:57

---
title: "Building a Simple PaaS with Sidecars (from Designing Distributed Systems book) in C#"
published: true
tags: Architecture,containers,distributed systems,git
canonical_url: 
---

The example which motivated this post comes from the excellent book **Designing Distributed Systems** by [Brendan Burns](https://twitter.com/brendandburns) (co-founder of the Kubernetes project). By the way, this book is available for free [here](https://azure.microsoft.com/en-us/resources/designing-distributed-systems/en-us/?wt.mc_id=AID627576_QSG_SCL_224576).

In the book, Brendan Burns shares an exciting use of the  [Sidecars pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/sidecar).

> The sidecar pattern can be used for more than adaptation and monitoring. It can also be used as a means to implement the complete logic for your application in a simplified, modular manner. As an example, imagine building a simple platform as a service (PaaS) built around the git workflow. Once you deploy this PaaS, simply pushing new code up to a Git repository results in that code being deployed to the running servers. We’ll see how the sidecar pattern makes building this PaaS remarkably straightforward.
> 
> [..] in the sidecar pattern there are two containers: the main application container and the sidecar. In our simple PaaS application, the main container is a Node.js server that implements a web server. The Node.js server is instrumented so that it automatically reloads the server when new files are updated. This is accomplished with the nodemon tool.
> 
> The sidecar container shares a filesystem with the main application container and runs a simple loop that synchronizes the filesystem with an existing Git repository.

Here is a graphic representation available in the book.

![](http://www.elemarjr.com/wp-content/uploads/2018/02/sidecar.png)

My goal was to create this example using ASP.net core. This source code is available on [my GitHub](https://github.com/ElemarJR/sidecar-paas-dotnetcore/blob/master/application/Startup.cs).

![](http://www.elemarjr.com/wp-content/uploads/2018/02/dotnet_sidecar.png)

## Creating a server that “automatically reloads when files are updated.”

I didn’t make an effort to create a sophisticated example. It is just a new project based on the “empty web app” template.

To get it up and running, on docker, I followed the [Nate McMaster’s instructions](http://www.natemcmaster.com/blog/2017/11/13/dotnet-watch-and-docker/) (I strongly recommend you to read these instructions).

Here is the [Dockerfile of the application](https://github.com/ElemarJR/sidecar-paas-dotnetcore/blob/master/application/Dockerfile).

```
FROM microsoft/aspnetcore-build:2.0 ENV DOTNET\_USE\_POLLING\_FILE\_WATCHER 1 WORKDIR /code/app EXPOSE 80 COPY Application.csproj . COPY Directory.Build.props . COPY NuGet.config . RUN dotnet restore ENTRYPOINT dotnet watch run
```

The _/code/app_ directory will be mapped to a host’s volume.

## The Sync With Git Sidecar

The logic to sync with Git is the same presented by Brendan Burns.

```
#!/bin/bash while true; do git pull origin master sleep 10 done
```

The [_Dockerfile_ for the sidecar](https://github.com/ElemarJR/sidecar-paas-dotnetcore/blob/master/sidecar/Dockerfile) is also pretty straightforward.

```
FROM microsoft/aspnetcore-build:2.0 WORKDIR /repo RUN apt-get update && apt-get install -y dos2unix COPY bash-paas.sh /entrypoint.sh RUN dos2unix /entrypoint.sh && apt-get --purge remove -y dos2unix && rm -rf /var/lib/apt/lists/\* ENTRYPOINT /entrypoint.sh
```

Again, the _/repo_ directory will be mapped to a host’s volume (which contains the repository used by the Application container).

The utility dos2unix was used to fix the line endings.

## Composing

Finally, to get this example running, I created a basic [Docker Compose configuration.](https://github.com/ElemarJR/sidecar-paas-dotnetcore/blob/master/docker-compose.yml)

```
version: '3' services: application: image: application build: context: application dockerfile: Dockerfile volumes: - ./application:/code/app ports: - "5000:80" sidecar: image: sidecar build: context: sidecar dockerfile: Dockerfile volumes: - .:/repo
```

## How to test it on your machine

To get it running on your machine (assuming that you already have docker installed):

1. Fork my repo
2. Clone the forked repo on your computer
3. Build it (_docker-compose build)_
4. Run (_docker-compose_ up)
5. Navigate to _localhost:5000_

Now, when you change the forked repo, the sidecar will pull the updates, and the application container will automatically restart the web app.

![](http://www.elemarjr.com/wp-content/uploads/2018/02/running.png)

 

## Last words

I am not a “container specialist.” So, probably you can find a lot of improvements opportunities. Feel free to give me your feedback. Right?

The post [Building a Simple PaaS with Sidecars (from Designing Distributed Systems book) in C#](http://www.elemarjr.com/en/2018/02/building-a-simple-paas-with-sidecars-from-designing-distributed-systems-book-in-c/) appeared first on [Elemar JR](http://www.elemarjr.com/en).