Hangfire Docker with Multiple Servers

I’ve been using Hangfire for almost 2 years. It’s a wonderful job schedule API with persistent storage. Community have a lot of examples about how to use Hangfire. However, almost all of them use the application (UI) as a hangfire server. Here, I will explain how to use Hangfire with docker and multiple servers. Let’s begin.


First, let’s create an API. I will show my example on ASP.NET Core 2.2 with PostgreSQL. Open your terminal.

$ mkdir Hangfire.UI
$ cd Hangfire.UI
$ dotnet new webapi
$ dotnet add package Hangfire.Core
$ dotnet add package Hangfire.AspNetCore
$ dotnet add package Hangfire.PostgreSql
"ConnectionStrings": {      "HangfireConnection": "Server=postgresql;Port=5432;Database=demo;User Id=demo; Password=111111;"},

Background Server

Secondly, we need a separate background server project. Create this project besides Hangfire.UI project.

$ mkdir Hangfire.Server
$ cd Hangfire.Server
$ dotnet new console
$ dotnet add package Hangfire.Core
$ dotnet add package Hangfire.PostgreSql
hang.server1_1  | Application started. Press Ctrl+C to shut down.
hang.server1_1 | Hosting environment: Production
hang.server1_1 | Content root path: /app/


The UI and Server projects must share the same code base for the Jobs. Therefore, I will create a new library project beside those projects.

$ mkdir Hangfire.Jobs
$ cd Hangfire.Jobs
$ dotnet new classlib
$ dotnet add reference ../Hangfire.Jobs


Since, the both UI and Server projects are .NET Core 2.2 projects. The Dockerfile should be almost same. (I tried my best while creating these dockerfiles). Hangfire.UI ‘s Dockerfile is shown below. Open a new file named Dockerfile and copy following commands into it.


Docker-compose creates an environment which run multiple images and enables them to communicate with each other. Go to main folder and create docker-compose.yml file and copy following content into it.

$ docker-compose up --build


In this post, we have successfully showed how to run multiple Hangfire servers using docker images. We have separated the UI and Server part of the Hangfire, also dockerized those applications. To make it clear, you should distribute your Hangfire servers with kubernetes or service fabric. It is important to use advantages of the distributing jobs and dockerizing. See you in next post.


ITU. CE. LCWaikiki www.alimozdemir.com