Node-Alpine images with Git

This will be short and sweet.

I was building out an image for a swagger diff api (link)
and quickly realised that the size of the Node-Jessie image is > 600mb. Which is just too bloody much for my 12 lines of expressjs.

This image is based on the popular Alpine Linux project, available in the alpine official image. Alpine Linux is much smaller than most distribution base images (~5MB), and thus leads to much slimmer images in general.
This variant is highly recommended when final image size being as small as possible is desired. The main caveat to note is that it does use musl libc instead of glibc and friends, so certain software might run into issues depending on the depth of their libc requirements. However, most software doesn’t have an issue with this, so this variant is usually a very safe choice. See this Hacker News comment thread for more discussion of the issues that might arise and some pro/con comparisons of using Alpine-based images.

To minimize image size, it’s uncommon for additional related tools (such as git or bash) to be included in Alpine-based images. Using this image as a base, add the things you need in your own Dockerfile (see the alpine image description for examples of how to install packages if you are unfamiliar).

The problem with the alpine image is that it does not contain GIT (as you can see from the above quote), which is a requirement for NPM to work properly.

So, the fast and easy way is to just add git to the dockerfile.

You simply add RUN apk --no-cache add git to your Dockerfile and you are good to go.

Doing this I ended up at an image size of 109mb down from 650mb, not bad.
Anyway, my Dockerfile went from


Note the change from
node:8.15-jessie to node:8.15-alpine as well.

If you want to know more about packaging simple node services inside a docker container, I highly recommend this article

Versioning WebApi and documenting version with Swagger

Versioning in WebApi

Note: This post draws alot of points from – but is more about implementation and documentation through swagger.
So please go read Troy’s post and then come back :).
There are a couple of ways of versioning a restful api
among them are:
1. Url versioning e.g. GET
2. Request-Header e.g. GET — Header: api-version: 1
There are pros and cons in both regards, Troy gets around them quite nicely.
Time to code!

Url Versioning

Easy – simply use either the RoutePrefix- or RouteAttribute

Read more about the Routing-Attribute here


Drawing from Troy’s post
We create a new attribute which uses a custom routing constraint

** Usage **

Effectively matching routes based on the “api-version” header.


When using swashbuckle (swagger implementation for .Net).
It knows about the default Route- and RoutePrefixAttribute, so Url Versioning is taken care of, out of the box.
But Header versioning is not.
Fortunately, it’s quite easy to implement an OperationFilter that adds the header as a parameter to the endpoints in question.
This implementation uses a list of versions and the endpoints relativepaths to know when to add the parameter and when not to.

How would you implement the OperationFilter?

How to use the autogenerated swagger client

A detailed post on how to use an autogenerated swagger client

This blogpost will focus on the generated C# client.
To get started you basically just need 1 thing.

  1. A Swagger.Json file

Creating the client

If you already have the client you can skip this part
So you have created an API and added Swagger to it or you have been given a swagger.json file or someone else has created a swagger enable API or any combination of the above.
Now you want to create a client for, thankfully

Can generate a client for you.
Get your swagger.json file (if using swashbuckle the default url for the file is {your api domain}/swagger/docs/v1)
Go the

and paste the swagger.json file into the modal that pops up.
The editor should notify you of any errors in the schema
For this article I have used the swagger.json from the petstore example (any valid swagger will do).
If no errors arise, you are ready to generate the client.
Once the zip file is done downloading you should unzip and compile it using the Compile.bat file, located inside the folder.
Once compiled you can find the client and it’s dependencies inside the /bin folder.
Then just add the reference to a new project.
I will be using a basic Console Application.

Using the client

Note: The generated client has 2 dependencies namely; Newtonsoft.Json and RestSharp, these should be referenced through Nuget instead of using the ones that come with the client.
I will provide a basic run-through of how to use the client with different types of authentication, and how to use the generated client.
2 things to note about the client.

  1. All endpoints for the are located in the IO.Swagger.Api namespace
  2. All configuration options for the httpclient is located in the IO.Swagger.Client namespace
    This is where we will provide:

* Authentication
* Default Headers
* UserAgent
* etc.
3. All response models are location in the IO.Swagger.Model namespace
I will start with the configuration as this needs to be out of the way to call the API.

Client Configuration

The examples given are general examples and are not related to the petstore client (apart from the api naming).
I will go through 2 types of Authentication

  1. Basic Authentication
  2. APIKey authentication

Basic Authentication in the generated client is very straightforward

The above example will use the Authorization Header as transport with whatever scheme is defined in the swagger.json file.
Using an APIKey is almost as easy as Basic Authentication:
There are 2 configuration options for apiKey ApiKeyPrefix and ApiKey.
They are both directly related to the header that will be used as transport for the token.

In the above example we are using the Authorization Header as transport and we are using the Bearer Scheme (ApiKeyPrefix)
The sent header will look like the following: Authorization: Bearer {Some token value}


Using the endpoints is almost insanely easy. Every API endpoint is wrapped in a nice easy to use method, with input and output models.
The petstore API uses an api_key header with the value of “special-key” for authentication.
The code for accessing the store inventory looks as the following

which generates the following output
The code for getting all pets with a particular status (to show the use of output models)

which generates the following output (as of this writing)


This concludes the post, you should find that using the generated API is pretty straightforward and easy.