Code With Bisky

Mastering Docker Compose: Run Your Spring Boot Project with Ease | Step-by-Step Tutorial

Key Topics covered:

  • Introduction to Docker Compose and its benefits
  • Step-by-step guide to creating a docker-compose.yml file
  • Building a Docker image with Jenkins using a Jenkinsfile
  • Setting up containers for Spring Boot application
  • Configuring environment variables, volumes, and networks
  • Running and managing the Spring Boot application using Docker Compose commands

Description:

In this tutorial, we'll walk you through the process of containerizing a Spring Boot project and running it using Docker Compose. Docker Compose is a powerful tool for orchestrating multi-container Docker applications, and it's perfect for managing complex setups like Spring Boot projects.

By the end of this tutorial, you'll have a solid understanding of how to leverage Docker Compose to streamline the deployment and management of your Spring Boot projects.

Prerequisites:

  • You have read our previous Blogs
  • docker-compose has been installed on your Server

Docker Compose Introduction:

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define all the services, networks, and volumes for your application in a single docker-compose.yml file, making it easier to manage complex applications that consist of multiple interconnected containers

  • What you can do with Docker Compose
  • Define Services: You can specify the services (containers) your application needs, along with their configuration options, such as Docker images, environment variables, ports, volumes, etc.
  • Manage Dependencies: If your application relies on multiple services, like a Spring Boot application and a database, Docker Compose lets you define and manage their relationships and dependencies
  • Orchestrate Containers: Docker Compose allows you to start, stop, and manage all the containers in your application with a single command. It also handles container linking and networking for seamless communication.
  • Environment Configuration: You can define environment variables specific to your application, making it easier to configure different environments (e.g., development, testing, production) without modifying your application code.
  • Scaling: While Docker Compose is mainly used for local development and testing, you can also use it to define and scale multi-container applications on a single host
  • Simplified Setup:: Instead of running multiple docker run commands, Docker Compose condenses all the setup and orchestration into a single configuration file, enhancing the ease of use.

Below is the docker-compose.yml of our sprint boot project


version: '3'
services:
  spring-boot-integration:
    container_name: spring-integration-with-bisky-api
    image: your_spring_image
    ports:
      - "9095:9095"
    networks:
      - api-network
networks:
  api-network:

 

Create a docker-compose.yml nano docker-compose.yml and add above content. Press CTL+X and then type y to save and press enter

your_spring_image is the name of the spring boot application docker image. The spring-boot-integration service is configured to run the Spring Boot Application. The application is running on port 9095 and the incoming traffic will be directed on port 9095. The application will run on api-network

Access The Spring Boot API Reverse Proxying:

Add sub domain sb-integration.codewithbisky.com server block

Run below command and add the server block in that file nano /etc/nginx/sites-available/sb-integration.codewithbisky.com Press CTL+X and then type y to save and press enter


server {
    server_name  sb-integration.codewithbisky.com;
    index index.html index.htm;
    access_log /var/log/nginx/sb-integration-codewithbisky.log;
    error_log  /var/log/nginx/sb-integration-codewithbisky-error.log error;
    client_max_body_size 100M;
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:9095;
        proxy_redirect off;
    }
}

Create the new subdomain on you DNS Management portal first and run below commands to restart nginx and generate certificate


sudo ln -s /etc/nginx/sites-available/sb-integration.codewithbisky.com /etc/nginx/sites-enabled/
sudo nginx -t
systemctl restart nginx
sudo certbot --nginx  -d sb-integration.codewithbisky.com

Create the new subdomain on you DNS Management portal first and run below commands to restart nginx and to generate certificates

To run the service, type docker-compose up -d `-d` if you want to run the service in background

We can access our api on https://sb-integration.codewithbisky.com/api/integration/swagger-ui/index.html

Conclusion:

In essence, Docker Compose is your key to simplified multi-container management. By using a single docker-compose.yml file, you effortlessly define, start, and stop interconnected containers that make up your application. It's like orchestrating a symphony of services with minimal effort. From local development to scaling up, Docker Compose is your go-to tool for smoother, hassle-free container experiences.

Thank you for joining us in this tutorial! If you found this tutorial helpful, don't forget to like, share, and subscribe to our channel for more DevOps and automation content.