Today I’ll show you how to create a simple Echo Server with ASP.NET Core and then a Docker Image using multi-stage build:

Create the Application


Open a PowerShell promt and run:

1mkdir echoserver
2cd echoserver
3dotnet new console
4dotnet add package Microsoft.AspNetCore -v 2.0.2

Replace the contents of Program.cs


Replace the contents of the Program.cs file with the following code:

 1namespace EchoServer
 2{
 3    using Microsoft.AspNetCore.Hosting;
 4    using Microsoft.AspNetCore.Builder;
 5    using Microsoft.AspNetCore;
 6
 7    class Program
 8    {
 9        static int Main(string[] args)
10        {
11            WebHost.CreateDefaultBuilder()
12                .UseKestrel()
13                .Configure(app =>
14                    {
15                        app.Run(httpContext =>
16                        {
17                            var request = httpContext.Request;
18                            var response = httpContext.Response;
19
20                            // Echo the Headers
21                            foreach (var header in request.Headers)
22                            {
23                                response.Headers.Add(header);
24                            }
25
26                            // Echo the body
27                            return request.Body.CopyToAsync(response.Body);
28                        });
29                    })
30                .Build()
31                .Run();
32
33            return 0;
34        }
35    }
36}

Build and Test


Run the folowing commands to build the application:

1dotnet build
2dotnet run

The Echo Server will be running on: http://localhost:5000 and to test it just send any request and the server should return it back to you.

Let’s try sending a GET request with a custom header:

1curl -H "Custom-Header: echo" http://localhost:5000/ -i

the response should read:

1HTTP/1.1 200 OK
2Date: Wed, 04 Apr 2018 10:10:55 GMT
3Server: Kestrel
4Content-Length: 0
5Accept: */*
6Host: localhost:5000
7User-Agent: curl/7.35.0
8Custom-Header: echo

Create a Dockerfile


In order to run the Echo Server in a container create a Dockerfile with the following contents:

 1# This stage builds the application
 2FROM microsoft/dotnet:2.0.6-sdk-2.1.101-jessie AS builder
 3COPY . src
 4WORKDIR src
 5RUN dotnet restore
 6RUN dotnet publish -c release
 7
 8# This stages uses the output from the build
 9FROM microsoft/dotnet:2.0.6-runtime-jessie
10COPY --from=builder src/bin/release/netcoreapp2.0/publish app
11WORKDIR app
12ENV ASPNETCORE_URLS http://*:80
13EXPOSE 80
14ENTRYPOINT ["dotnet", "echoserver.dll"]

Note: The previous Dockerfile defines a multi-stage build:

  • The first stage copies the source code to the image and builds the application.
  • The second stage uses the output of the first stage (builder) to create the final and optimized image (i.e. No need to remove the sdk or the source code).
  • Docker 17.05 or higher is needed in order to build and image based on a multi-stage Dockerfile.

Build and test the image


Run the folowing commands to build and run the docker image:

1docker build -t echoserver .
2docker run -p 5000:80 echoserver

To test the image just repeat the tests from step 3.

Hope it helps!

Feel free to get the code here.