Oskar Dudycz

Pragmatic about programming

How to create a Docker image for the Marten application

2023-07-13 oskar dudyczDevOps

cover

Containerisation is something that pushed our industry much further. Generating immutable artefacts is a foundational aspect of the DevOps process. It’s much easier to troubleshoot, diagnose and catch earlier production issues. We can assume that no cowboy updates were involved.

I wrote already a few guides on this process:

Even though I’m mostly connected to the Event-Driven approach, they’re one of the most read articles on my blog, proving its importance.

I feel that the DevOps process in Event Sourcing tools is not appreciated enough; we talk too less about that, especially on the operations, continuous integration and delivery. That may be a reason why this is still a niche. People are afraid of how to run such systems in production.

Today, Let’s bury this hole a bit and explain how to build a Docker image of the application running Marten as an event store.

To run Marten on prod, you must have a Postgres database instance. Most of the cloud providers support them as managed services. You can run it on your own on Virtual Machine, on-premise or container. I’ll assume that you have such.

You also need to have a deployment for your application running Marten, for instance, Kubernetes, a container-based, virtual machine running docker images. Choose what suits your case and your preferences.

Whatever that is, you’ll need to build a container image. That config will need to include some of the Marten specifics. Which one?

In Marten, we’re using code generation extensively. It helps us to cut the number of allocations, as instead of using reflection a lot, and dictionary lookup, we’re building the Matryoshka doll model on top of your code to optimise the performance and resiliency.

Marten can generate code on the fly when you call it, which is helpful for the development phase, but may add overhead for regular usage, as it may increase the cold start of the application code. For production, you’d like to cut all overhead. We understand that, so we allow you to pre-built generated code and use static code instead of doing it on the fly.

You can read about that in our docs, but in short, you need to do a few steps:

  1. Add Marten.CommandLine NuGet package to your project:
dotnet add package Marten.CommandLine
  1. Register Marten commands by adding the following lines to your code:
var builder = WebApplication.CreateBuilder(args);

// add Marten
builder.Services.AddMarten();

builder.Host.ApplyOaktonExtensions();

var app = builder.Build

// has to be the last line and no app.Run is needed
return await app.RunOaktonCommands(args);

Marten internally is using Oakton for command line processing.

  1. Then call from the command line:
dotnet run -- codegen write
  1. You can also use the Optimised Artifact Workflow. Which will set dynamic code generation for the Development environment and setup code generation for the Production:
builder.Services.AddMarten()
    // Add this line to Marten config
    .OptimizeArtifactWorkflow(TypeLoadMode.Static);

If you configured everything correctly, your app is set up for Production usage.

Let’s include that in the Dockerfile configuration. We’ll use the optimised multistage build described in previous article. We’ll try to make it reusable and generic, so you can apply that easier to your project. We’ll use Docker to build arguments to have configurable inputs for:

  • .NET version,
  • Project file name,
  • whether to run code generation or not.

Why would we not want to run always code generation for our Docker image? We should always do it for deployment, but for Development Containers, we might want to skip that to reduce the built time.

Image definition could look as follows:

ARG dotnet_version=8.0
########################################
#  First stage of multistage build
########################################
#  Use Build image with label `builder
########################################
FROM mcr.microsoft.com/dotnet/sdk:${dotnet_version}-alpine AS builder
# Project file name, mandatory parameter, e.g. Helpdesk.Api
ARG project_name
ARG run_codegen=true

# Setup working directory for project
WORKDIR /app

COPY ./${project_name}.csproj ./

# Restore nuget packages
RUN dotnet restore ./${project_name}.csproj

# Copy project files
COPY ./ ./

# Run code generation depending on the build argument
RUN if [ "${run_codegen}" = true ] ; then dotnet run -- codegen write & dotnet run -- codegen test; else echo "skipping code generation"; fi

# Build project with Release configuration
# and no restore, as we did it already
RUN dotnet build -c Release --no-restore ./${project_name}.csproj

# Publish project to output folder
# and no build, as we did it already
WORKDIR /app/
RUN ls
RUN dotnet publish -c Release --no-build -o out

########################################
#  Second stage of multistage build
########################################
#  Use other build image as the final one
#    that won't have source codes
########################################
FROM mcr.microsoft.com/dotnet/aspnet:${dotnet_version}-alpine
ARG project_name
ARG dotnet_version=8.0

# Setup working directory for project
WORKDIR /app

# Copy published in previous stage binaries
# from the `builder` image
COPY --from=builder /app/out .

# Set URL that App will be exposed
ENV ASPNETCORE_URLS="http://*:5000"
ENV PROJECT_DLL="${project_name}.dll"

# sets entry point command to automatically
# run application on `docker run`
ENTRYPOINT dotnet $PROJECT_DLL

You’d need to put it in the runtime project location and call it container build as:

docker build --build-arg project_name=Helpdesk.Api . -t helpdesk

Or with all parameters

docker build --build-arg project_name=Helpdesk.Api --build-arg codegen=true --build-arg dotnet_version=8.0 . -t helpdesk

Beside the configurable input arguments, the Docker definition does one specific thing: pre-building generated code. It has to go after project files are copied and dependencies, but before the build. Why? Because if we use static type load, the application will fail if no files are generated.

This is the line responsible for doing the code generation, together with a dry run test.

RUN if [ "run_codegen" = true ] ; then dotnet run -- codegen write & dotnet run -- codegen test; else echo "skipping code generation"; fi

You can see all the changes in the Pull Request and play with the full sample in my repository.

Once you build the image, you can also push it to Container Registry

I’m considering adding an option in our command line to generate such Dockerfile; feel free to share your thoughts and ideas for improvements!

Read also other articles around DevOps process:

Cheers!

Oskar

p.s. Ukraine is still under brutal Russian invasion. A lot of Ukrainian people are hurt, without shelter and need help. You can help in various ways, for instance, directly helping refugees, spreading awareness, putting pressure on your local government or companies. You can also support Ukraine by donating e.g. to Red Cross, Ukraine humanitarian organisation or donate Ambulances for Ukraine.

👋 If you found this article helpful and want to get notification about the next one, subscribe to Architecture Weekly.

✉️ Join over 11500 subscribers, get the best resources to boost your skills, and stay updated with Software Architecture trends!

Loading...
Event-Driven by Oskar Dudycz

cover

Through my window, I see the result of good plans but poor execution. Opposite my flat, there is a partially completed construction place. Buildings were supposed to be eye-catching Mediterranean style apartments. Delivery date? Two years ago. Actual? More and more unknown.

Some time ago, I heard that using Event Sourcing makes creating Event-Driven Architecture easier. The arguments were correct, that if we’re already publishing events to trigger business workflows, then at some point, we may want to also store events to not lose information. Agreed. However, I also heard that keeping the state as events will simplify things. We’ll have a source of truth with a record of the system behaviour. This will allow, e.g. to confront the results of the operations with the recorded state. I’d agree with that, with one distinction. It’s easier as long as you already know Event Sourcing.

Many people in the DDD community claim that the essential is to properly break down the system into autonomous parts called bounded contexts. Once we have it, the rest is secondary and will sort itself out. For sure.

Many seasoned programmers speak similarly about new technologies. They claim that they can translate past experience into new technologies. That’s true that by analogy, they can catch the big picture quicker. But isn’t it a bold assumption to say that Win.Forms specialist will learn Angular quickly?

The end result may differ a lot from the initial ideas. I saw the plan of those buildings next to me. Now I can see the effects of the execution. Or actually, the lack.

I believe that we should carefully acknowledge not only the point of view of our authorities but also their seating point. If we want to find out how to form a wall, do we ask an architect or a foreman? An architect may know the theory, but the practice is what we’re looking for. On the other hand, if you want to know where to put the wall, you prefer the architect to do measurements. At least if you don’t want to have the roof falling to your head.

After I had torn a ligament in my knee, I went to two qualified orthopedists. One said I should have surgery and do a reconstruction. The second stated that there is no need for that; rehabilitation should be enough. Guess which one had a specialization in surgery and which in rehabilitation?

People usually give us advice from the point where they’re currently standing. They are entitled to a biased view. An architect who rarely does programming will tend to downplay the value of implementation and tactical patterns. Midlevel developers will focus on technicalities instead of the global system impact. The team manager or consultant will emphasize the importance of soft skills (or esoteric techniques known only to them).

The truth is that we need all of them. The excellent plan will fall on the bad execution. The best execution for the wrong case will be just a waste of time. We should carefully evaluate the advice considering what we need and what an expert can give us.

Therefore, when we’re reading an article, watching a talk, let’s also pay attention to the place where the person is standing. The perspective from there may be much different from where we are right now. That can be good, as it may push us in the right direction. But it may also be misleading, as we accidentally take biases of this person without understanding the tradeoffs. Personally, I prefer to follow not only people from pedestal but also those that are closer to my position. A bit further in the journey, but not too far. That helps me to calibrate my view as those people are more relative to my daily struggles.

Polish historical leader Józef Piłsudzki reportedly used to say: “Right is like an ass, everyone has its own”.

Cheers!

Oskar