Build and Deploy a Docker C# Application


This tutorial will walk you through how to build and deploy a Docker container with a C# Web-Application.

Before You Begin

To successfully complete this tutorial, you need:


For this tutorial we will use a basic “Hello World” C# web-application. To start create a directory to store your application and create a file called webserver.cs. Below are the contents of my C# web-application:

using System;
using System.Net;
using System.Threading;
using System.Linq;
using System.Text;
namespace SimpleWebServer
    public class WebServer
        private readonly HttpListener _listener = new HttpListener();
        public WebServer()
            if (!HttpListener.IsSupported)
                throw new NotSupportedException("Needs Windows XP SP2, Server 2003 or later.");
        public void start()
                HttpListenerContext ctx = _listener.GetContext();
                new Thread(new Worker(ctx).ProcessRequest).Start();

        public void Stop()

        static public void Main ()
            WebServer webServer = new WebServer();

    public class Worker
        private HttpListenerContext context;
       public Worker(HttpListenerContext context)
          this.context = context;
       public void ProcessRequest()
          StringBuilder sb = new StringBuilder();
          sb.Append("<html><body><h1>Hello World from C# and Distelli</h1>");
          byte[] b = Encoding.UTF8.GetBytes(sb.ToString());
          context.Response.ContentLength64 = b.Length;
          context.Response.OutputStream.Write(b, 0, b.Length);

Step 1. Create Dockerfile

To build our application in a Docker container, we need to create a Dockerfile. A Dockerfile contains the instructions to build your application in a container. The Dockerfile we will use for our C# application is below:

FROM mono:

RUN mkdir -p release 
COPY release/* /release/

CMD [ "mono", "release/webserver.exe" ]

Breakdown of the Dockerfile

In the first part of our Dockerfile we define our base image. For more information on the mono base image, please visit their GitHub.

FROM mono:

Next, we create our release directory in our image and copy the contents of our compiled C# application:

Note: We will compile before we build our Docker Image. It will take place during our Build phase.

RUN mkdir -p release 
COPY release/* /release/

Finally, we expose port 8080 and run our application

EXPOSE  8080
CMD [ "mono", "release/webserver.exe" ]

Push Files to Your Repository

The final step in creating our application is to push our files into a GitHub or BitBucket repository. If you are unfamiliar with using repositories, you can find more information for GitHub here and more information for BitBucket here.

Step 2. Install the Pipelines Agent on your Server

If you do not have a Pipelines account, please Sign Up for one now.

The next step in deploying our app is to make sure you have a server to run your application on. You can use any type of server - public cloud, private cloud or physical. Pipelines will let you deploy your code to Vagrant VMs or servers in a VPC. You can even deploy to the server under your desk. For this tutorial I am going to be deploying to a server hosted by DigitalOcean.

Note: You will need remote access to the server you are deploying to & root (administrator) permissions.

Install Pipelines Agent on Your Server

To be able to deploy your application to your server you will need to install the Pipelines Agent. You will need remote access to your server to complete the installation. Please consult the below information on how to install the Pipelines Agent on your server.

Linux and macOS X

To install on Linux or macOS X you can use either curl or wget with one of the following syntaxes.

wget example
wget -qO- | sh
curl example
curl -sSL | sh

Complete the Install

To complete the install of the agent, you must issue the /usr/local/bin/distelli agent install command.

/usr/local/bin/distelli agent install
ServerA:~$ <b>wget -qO- | sh</b>
This script requires superuser privileges to install packages
Please enter your password at the sudo prompt

[sudo] password for bmcgehee: 
    Installing Distelli CLI 3.51 for architecture 'Linux-x86_64'...
To install the agent, run:
    sudo /usr/local/bin/distelli agent install
ServerA:~$ <b>sudo /usr/local/bin/distelli agent install</b>
Distelli Email:
    1: User: jdoe
    2: Team: janedoe/TeamJane
Team [2]: <b>1</b>
Server Info:
Starting upstart daemon with name:  

Verify the Install

To validate the agent is installed and working use the /usr/local/bin/distelli agent status command.

Note: This installation requires root (administrator) permissions.

/usr/local/bin/distelli agent status
Distelli Agent (serverA) is Running with id 

If you would like more information on installing the Pipelines agent, visit Installing the Pipelines Agent.

Step 3. Build And Upload the Docker Image

Now that we have our application in GitHub/BitBucket and our server is ready to be deployed to, we are going to build our Docker Image. Log into your Pipelines account and click the New App button in the right hand corner

Create a new Pipelines Application

Select the repository type where you are storing your application’s files.

Choose Your Source Control

After clicking the button to connect to our repository, select the appropriate repository that contains the files.

Choose Your Pipelines Repo

Select the appropriate branch for your deployment. We have a master branch, but you can deploy any branch from your repository.

Choose Your Pipelines Repo's Branch

Next, we set our build steps. This is the step where you declare that you are building a Docker container. In the right corner, click Build With Docker File, and enter in your Docker Hub or ECR credentials

Build Your Application With Docker

Once you have entered in your credentials, Pipelines will inject the Docker build commands. Before we build our image we still need to compile our code. Enter in the following commands above your Docker Build commands:

mkdir release
sudo apt-get update
sudo apt-get install mono-mcs -y
mcs webserver.cs -out:release/webserver.exe

Docker C# Build Steps

Finally, we make sure all of settings look correct and then click the Looks Good. Start Build! button to build your Docker container

Build Your Application With Docker

After your build has started, click the build to view its progress. You can view realtime logs of your application building.

Successful Build of C# Docker Container

Setting Up Port Mapping

To be able to access the application running in our Docker container, we need to set up Docker Port Mapping. Navigate to your application’s page in Pipelines, click the Settings tab, and expand the Docker section. You will see a section to define your Docker Port Mapping. Set your Container Port to 8080 and the Host Port to 80.

Build Your Application With Docker

Step 4. Deploy Your Docker Container

Now that we have successfully built our Docker image, we are ready to deploy it to our server. On the Builds page click New Deployment in the right hand corner.

New Pipelines Deployments

The New Deployment page opens. Select the Deploy a Release

Deploy Pipelines Release

Now, select the application you want to deploy. Select the application we created earlier in the tutorial.

Select Pipelines Application for Deployment

Next, select the release you would like to deploy. For now there should be only one release for deployment.

Choose Your Pipelines Release

The last step in our deployment is to select the environment you want to deploy to. First, you create an environment by entering a name and selecting the Add Environment.

Select Pipelines Deployment Environment

Next, select the Environment you just created and click All Done.

Select Pipelines Deployment Environment

Next, add your server. Click Add Servers to get started. Select the server you configured earlier in the tutorial, and add it to your account. Once you have added your server, you can close the Add Servers panel and continue with your deployment. You will see a final option to set your delay between deployments on your servers.

Start Your Pipelines Deployment

Click Start Deployment to begin your deployment. You are redirected to a page where you can view the progress of your deployment. Log enables you to view real time streaming logs for the deployment.

Pipelines Deployment Successful

Now navigate to “http://<-Your Server’s IP Address->” and see your C# Web-Application!

Docker C# Successful Deployment

And that’s it! You have built a C# Web-App, built a Docker image, and deployed that image as a container.

How helpful was this page?
Puppet sites use proprietary and third-party cookies. By using our sites, you agree to our cookie policy.