DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Tutorial: Deploying Java EE Apps on Azure (Part 3)
  • Tutorial: Deploying Java EE Apps on Azure (Part 2)
  • Docker and DevOps: Developing Stateful Applications and Deploying in Docker
  • Java EE 6 Pet Catalog with GlassFish and MySQL

Trending

  • Generative AI: A New Tool in the Developer Toolbox
  • Unleashing the Power of Microservices With Spring Cloud
  • API Design
  • Automated Testing Lifecycle
  1. DZone
  2. Software Design and Architecture
  3. Containers
  4. Deploying Java EE Application to Docker Swarm Cluster

Deploying Java EE Application to Docker Swarm Cluster

Follow these simple steps to get your Java application deployed.

Arun Gupta user avatar by
Arun Gupta
·
Oct. 13, 15 · Tutorial
Like (8)
Save
Tweet
Share
6.19K Views

Join the DZone community and get the full member experience.

Join For Free

What is Docker Swarm?

Docker Swarm provides native clustering to Docker. Clustering using Docker Swarm 0.2.0 provides a basic introduction to Docker Swarm, and how to create a simple three node cluster. As a refresher, the key components of Docker Swarm are shown below:

In short, Swarm Manager is a pre-defined Docker Host, and is a single point for all administration. Additional Docker hosts are identified as Nodes and communicate with the Manager using TCP. By default, Swarm uses hosted Discovery Service, based on Docker Hub, using tokens to discover nodes that are part of a cluster. Each node runs a Node Agent that registers the referenced Docker daemon, monitors it, and updates the Discovery Service with the node’s status. The containers run on a node.

That blog provide complete details, but a quick summary to create the cluster is shown below:


# Create cluster
TOKEN=`docker run swarm create`
# Creating Swarm master
docker-machine create -d virtualbox --swarm --swarm-master --swarm-discovery token://$TOKEN swarm-master
# Creating swarm node 01
docker-machine create -d virtualbox --swarm --swarm-discovery token://$TOKEN swarm-node-01
Create swarm node 02
docker-machine create -d virtualbox --swarm --swarm-discovery token://$TOKEN swarm-node-02

Listing the cluster shows:


NAME            ACTIVE   DRIVER       STATE     URL                         SWARM
swarm-master             virtualbox   Running   tcp://192.168.99.106:2376   swarm-master (master)
swarm-node-01            virtualbox   Running   tcp://192.168.99.107:2376   swarm-master
swarm-node-02   *        virtualbox   Running   tcp://192.168.99.108:2376   swarm-master

It has one master and two nodes.

Deploy a Java EE application to Docker Swarm

All hosts in the cluster are accessible using a single, virtual host. Swarm serves the standard Docker API, so any tool that communicates with a single Docker host communicate can scale to multiple Docker hosts by communicating to this virtual host.

Docker Container Linking Across Multiple Hosts explains how to link containers across multiple Docker hosts. It deploys a Java EE 7 application to WildFly on one Docker host, and connects it with a MySQL container running on a different Docker host. We can deploy both of these containers using the virtual host, and they will then be deployed to the Docker Swarm cluster.

Lets get started!

MySQL on Docker Swarm

  1. Start the MySQL container
    
    docker run --name mysqldb -e MYSQL_USER=mysql -e MYSQL_PASSWORD=mysql -e MYSQL_DATABASE=sample -e MYSQL_ROOT_PASSWORD=supersecret -p 3306:3306 -d mysql
  2. Status of the container can be seen as:

    
    CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                           NAMES
    b49d627a0431        mysql:latest        "/entrypoint.sh mysq   5 minutes ago       Up 4 minutes        192.168.99.107:3306->3306/tcp   swarm-node-01/mysqldb
  3. It shows the container is running on swarm-node-01. Make sure you are connected to the Docker Swarm cluster using eval $(docker-machine env --swarm swarm-master).
  4. Find IP address of the host where this container is started:

    
    ~> docker inspect --format '{{ .Node.Ip }}' $(docker ps -q --filter 'name=*mysqldb*')
    192.168.99.107

WildFly on Docker Swarm

  • Start WildFly application server by passing the IP address of the host and the port on which MySQL server is running: 
  • Status of the container can be seen as:
    
    ~> docker ps
    CONTAINER ID        IMAGE                                  COMMAND                CREATED             STATUS              PORTS                           NAMES
    ab5717083812        arungupta/wildfly-mysql-javaee7:host   "/opt/jboss/wildfly/   25 minutes ago      Up 25 minutes       192.168.99.108:8080->8080/tcp   swarm-node-02/mywildfly   
    b49d627a0431        mysql:latest                           "/entrypoint.sh mysq   34 minutes ago      Up 33 minutes       192.168.99.107:3306->3306/tcp   swarm-node-01/mysqldb
  • Enjoy!

    Docker (software) clustering Java EE application Host (Unix) Java (programming language)

    Published at DZone with permission of Arun Gupta, DZone MVB. See the original article here.

    Opinions expressed by DZone contributors are their own.

    Related

    • Tutorial: Deploying Java EE Apps on Azure (Part 3)
    • Tutorial: Deploying Java EE Apps on Azure (Part 2)
    • Docker and DevOps: Developing Stateful Applications and Deploying in Docker
    • Java EE 6 Pet Catalog with GlassFish and MySQL

    Comments

    Partner Resources

    X

    ABOUT US

    • About DZone
    • Send feedback
    • Careers
    • Sitemap

    ADVERTISE

    • Advertise with DZone

    CONTRIBUTE ON DZONE

    • Article Submission Guidelines
    • Become a Contributor
    • Visit the Writers' Zone

    LEGAL

    • Terms of Service
    • Privacy Policy

    CONTACT US

    • 3343 Perimeter Hill Drive
    • Suite 100
    • Nashville, TN 37211
    • support@dzone.com

    Let's be friends: