The cloud space is becoming really exciting now, and moving very fast, so my skills need to keep up! It’s all very well noodling around with individual technologies, but there’s nothing like a project, however far fetched and over-wrought, to bring things together.

After attending a brilliant talk by Shahid Iqbal (video), I started noodling with Docker, and then realized that I had a potential project. This blog is hosted on GitHub. Writing the blog locally I use Jekyll to check what I’m creating (this is the underlying technology behind GitHub Pages). Handily this also creates a whole bunch of HTML. Score!

Score!

The first stop for me was to create a simple ASP.NET Core website that currently serves purely static content. A reference to the Microsoft.AspNetCore.StaticFiles package is required:

<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.1.1" />

We can then tell ASP.NET to use the default file mappings (e.g. index.html when the root of a directory is requested), and serve static files:

app.UseDefaultFiles();
app.UseStaticFiles();

Lastly the static files are copied from the _site folder in my pages reposiory to the wwwroot folder the in the ASP.NET application. Testing the application this serves the expected content.

As a final step I added a custom middleware that will add a header showing the name of the machine serving the response (don’t worry! I wouldn’t do this in a real application!).

app.Use(async (context, next) => {
  context.Response.Headers.Add("X-machine-name", Environment.MachineName);
  await next.Invoke();
});

The last step (for this article) is to actually dockerize the application. And the Docker docs really show how to do this very easily. There are a couple of points that are not obvious:

  • The commands shown in the article should be run in a Administrator Powershell.
  • The docker cli needs to be logged in to the docker hub, this is easily done with the docker login command. But beware that the cli needs your docker id, not the email address you registered with.

You can have a look at the current application on my GitHub.

Addendum

The actual commands I used to build and run the app are as follows: docker build -t Blog.App . (N.B. You must be in the src/Blog.Application directory when you execute this command.) This creates an image called Blog.App.

To run the app you then use the command docker run -d -p 8080:80 --name blogapp Blog.App. The -d flag runs the image in the background. The -p publishes a (map of the) container to the host, in this case causing the containers port 80 to be exposed as port 8080. The --name parameter assigns a name to the running container. The final parameter is the image to be executed.