Spring Boot service in Docker Swarm

In this tutorial we will setup a Spring Boot service application by using Docker Swarm orchestration management tool.


Spring Boot is an extension of Spring framework that makes it easy to create stand-alone, production-grade Spring based Applications and Web Services that you can “just run”.

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly.

A Docker Swarm is a container orchestration tool running the Docker application.

Build Spring Boot Web Service

In this tutorial IntelliJ development IDE is used. Also please verify that you have Java JDK installed. You can get the latest prebuilt OpenJDK binaries from Eclipse Adoptuim project.

Open IntelliJ and create new project

And select project type Spring Initializr

New Spring Boot application with the following structure is created

Create a small Greetings REST controller that will print Swarm manager and service IP address. You will need to add spring-boot-starter-web dependency.

package com.example.demo;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.UnknownHostException;

public class GreetingsController {
    public String hello(HttpServletRequest request) throws UnknownHostException {

        try {
        } catch (InterruptedException e) {
            throw new RuntimeException(e);

        return "Hello World! Greetings from: " + request.getLocalAddr() + "  to: " + request.getRemoteAddr();

In this simple example we stop the execution for 15 sec in order to have time to open multiple tabs (or more browser instances) and send multiple requests to the service replicas and to be served from different replicas.

We assume you have Docker installed but if not please check Docker install guide.

In order to create contained image from the Spring Boot service we will need to add Docker file in the project directory with the following content

FROM eclipse-temurin:17-jdk-alpine
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

You can use mvnw wrapper script to build your service like:

# ./mvnw installed

and now you are ready to create Docker image for your service

# docker build -t demoservice .

List Docker images to verify that your image is created

# docker image ls

Now you are ready to start your Swarm and to create the service. In order to start the swarm you will execute:

# docker swarm init

and then you will create your service with:

# docker service create --name demoservice --publish published=8000,target=8080 --replicas 5 demoservice

This command will create 5 replicas of your service by the swarm. Please not the published port value which is the value that you will access the swarm and target port, which is port on which your service will listen in the swarm. Swarm manager will assign different IP to to every replica on the network created.

If you have a problem accessing your swarm you may try to add the following line in the aplication.properties file in the demoservice project:

# server.address =

And you are ready to test your swarm now. If you access your swarm from different tabs in the browser you will receive greetings from different replicas of your service in the swarm.

This is simple demo tutorial how to create a swarm on a local machine. In case more complex network needed please take a look into the Docker documentation.

All done!


Docker Swarm

Spring Boot

Leave a Reply

Your email address will not be published. Required fields are marked *