Introduction to Docker Swarm: Orchestrating and Scaling Containers

Welcome to this comprehensive beginner’s guide on Docker Swarm. I will explain everything in detail, and by the end of this tutorial, you will learn how to use Docker Swarm to orchestrate and scale containers. If you are new to Docker, don’t worry! I will guide you through each step, making sure everything is clear and understandable.

To learn more about Docker, please check Docker Tutorials page.

Introduction to Docker

Docker is an open-source tool that simplifies the process of managing application processes in containers. Containers allow developers to package an application with all of its dependencies into a standardized unit for software development. This guarantees that the application will run on any other Linux machine regardless of any customized settings or previously installed packages that could differ from machine to machine.

Docker Swarm: What is it?

Docker Swarm is a container orchestration tool, meaning that it allows the user to manage multiple containers deployed across multiple host machines. One of the key benefits of Docker Swarm is the high level of availability offered for applications. Docker Swarm provides advanced features such as service discovery, load balancing, secure secret management, and template-based service scaling.

Installing Docker

Before we start with Docker Swarm, we need to have Docker installed on our system. Docker is available for both Linux and Windows, and it can be downloaded from the official Docker website. Once the download is complete, follow the installation instructions based on your operating system. For MacOS, read How to install Docker on MacOS.

Initializing a Docker Swarm

A Docker Swarm is a group of either physical or virtual machines that are running the Docker application and have been configured to join together in a swarm. A swarm consists of several Docker nodes, which can be thought of as an individual machine or VM that runs the Docker application.

To initialize a Docker Swarm, we use the following command:

docker swarm init --advertise-addr <MANAGER-IP>

Replace <MANAGER-IP> with the IP address of the machine you’re running the command on. This machine will function as the manager node.

Services in Docker Swarm

Services are the definition of the tasks to execute on the nodes. It’s an abstraction of the containers and the commands to run inside the containers. To create a service, use the following command:

docker service create --replicas 1 --name helloworld alpine ping

This command creates a service named “helloworld” that uses the alpine Docker image and runs the command ping

Scaling Services

One of Docker Swarm’s primary benefits is its ability to scale applications easily. To scale a service in Docker Swarm, use the following command:

docker service scale <SERVICE-ID>=<NUMBER-OF-TASKS>

Replace <SERVICE-ID> with the ID of the service you want to scale, and <NUMBER-OF-TASKS> with the number of tasks you want to run.

Deploying a Java Spring Boot Application on Docker Swarm

In this section, we’ll deploy a Java Spring Boot application with a MySQL database and Tomcat server. Before we proceed, ensure you have a basic Java Spring Boot application ready. We will containerize this application, store it in a Docker image, and finally deploy it in the Docker Swarm.

Creating a Dockerfile

First, we need to create a Dockerfile for our Spring Boot application.

FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Here, we’re creating a Docker image based on openjdk:8-jdk-alpine which is a lightweight Java environment. The VOLUME command is used to enable access from your container to your hard-disk, EXPOSE 8080 is used to expose port 8080 to the outside world. The ARG JAR_FILE=target/*.jar specifies that our JAR file is located in the target directory and we’re copying it as app.jar into our image. Finally, the ENTRYPOINT specifies the command to run when the Docker container starts.

Building a Docker Image

In your terminal, navigate to the directory that contains your Dockerfile and run the following command:

docker build -t my-spring-boot-app .

This command builds a Docker image from your Dockerfile and tags it as my-spring-boot-app. The . at the end specifies that the Dockerfile is in the current directory.

Creating a Docker Compose File

Docker Compose allows you to define multi-container applications, so it’s ideal for our use case where we have an application server (Spring Boot app) and a database server (MySQL).

Let’s create a docker-compose.yml file:

version: '3'
    image: my-spring-boot-app
      - "8080:8080"
      - db
    image: mysql:8.0
      MYSQL_ROOT_PASSWORD: rootpass
      MYSQL_DATABASE: test
      MYSQL_USER: user
      MYSQL_PASSWORD: pass
      - db_data:/var/lib/mysql

In this file, we define two services: my-spring-boot-app and db. The my-spring-boot-app service uses the image we created and exposes port 8080. It depends on the db service, which uses the mysql:8.0 image and defines some environment variables to configure the MySQL instance. The volumes key allows us to persist data generated by and used by Docker containers.

Deploying with Docker Swarm

Before deploying, make sure your Docker Swarm is initialized. You can do this by running docker swarm init.

Then, deploy your stack with the following command:

docker stack deploy -c docker-compose.yml myapp

This will deploy your services to the swarm. You can see your services by running docker service ls.

Congratulations! You have successfully deployed a Java Spring Boot application on Docker Swarm.


In this tutorial, I explained the basics of Docker and Docker Swarm, how to install Docker, initialize a Docker Swarm, create services, and how to scale them. I also showed you how to containerize and deploy a Java Spring Boot application with a MySQL database using Docker Swarm.

Remember, the key to learning is practice. Try to deploy different applications, play around with the number of services, and experiment with different settings to get a feel for what Docker Swarm can do.