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.

Then we need the Hangfire packages.

Open and add those lines into

Next, open and add the connection string

Dont change the server part of connection string, PostgreSQL will be exposed with that name.

Next and the most important thing, dashboard settings. By default, dashboard can only be reached by localhost. Since we are using docker, it will stay in the image. Therefore, we have to expose the dashboard. Open and copy following code.

As I mentioned in the code, do not use this code in the production directly. You have to create your own scenario (e.g. only admin roles can reach hangfire.). Also, we have disabled anti forgery token for now, the security is not our first concern here. UI part is done.

Background Server

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

Run following package commands

The console must stay alive, all the time. Therefore we will use one of the best solutions of .NET Core.

We are setting the connection string and creating a background server with respect to it. I have set as for demonstration, you can use at production. Also, class has a property which allows you to use Dependency Injection with your jobs (e.g. Example). will suspend the main thread and prevent the server termination.

Output of the Background Server;


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.

A sample job using

This common project can be added to the UI and Server projects with following command.

We are done with the infrastructure. Additionally, we need to enqueue the example job (MyJob). At there should be where you can put an example job there. Or you can create your own controller for jobs.

The above code will enqueue the example job with random interval when anyone hit the action.

We are done with the codebase. We have separated the Hangfire Background Server and Dashboard, and created a common class library for jobs. What we have to do in the next part is, dockerize the projects and create environment with those containers.


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

Only difference between UI and Server is command on the file. The server project does not need any TCP port exposing.

Since we obtained multiple dockerized applications, we are almost done. Next, run the images using docker-compose command.


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

The compose file has a network name . This network will connect the images with each other. On the other hand, the dependency between images should be in a particular order.

PostgreSQL > UI > Servers.

Docker-compose up multiple servers which are called as and . Server 2 will use the same image with Server 1, therefore we can up more than one server using the same image. Keep in mind that using docker-compose is not ideal in every scenario, we need an orchestrator such as Kubernetes so that the number of servers can be increased on demand easily.

Let’s see the results. Go to project’s root folder and run

The docker-compose will build all images and run them. Next, you can go to


As you can see here, there are two servers running. Let’s try the job scheduling. Load twice the following endpoint http://localhost:5005/api/values to enqueue new jobs.

The image shows that jobs are distributed to the servers. Remember, we have set the worker count to 1 above.


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.

You can get the source files below.


ITU. CE. LCWaikiki www.alimozdemir.com