Introduction to Docker-Compose with Spring Boot Example

Introduction to Docker-Compose – With Spring Boot Example

This is a beginner tutorial on how to build and deploy microservices using Docker-Compose. But first, we’ll learn what docker-compose is and what it’s used for.

  1. Introduction to Docker Compose
  2. Create the Spring Boot API
  3. Create the Spring Boot UI with Thymeleaf
  4. Create the Packages
  5. Create the Docker File
  6. Create the Docker Compose File
  7. Fire Up the Containers!

 

1. Introduction to Docker-Compose

In the previous tutorial, we learnt how to Dockerize our Spring application. That was just a single spring boot application.

What if you have two or more application where one depends on the other. For example, there’s an API for the backend. Then you also have a frontend UI application create with Thymeleaf that fetches data from the API backend.

Using Docker-compose, you can create this two images at the same time and run the containers as well.

Let’s now take a demo using Spring Boot.

 

2. Create Spring Boot API

You can get the complete API application here in my Github repository.

 

3. Create a Spring Boot UI using Thymeleaf

I already created this application. So you can just get it from my Github repository here.

So once you have these application, start the API (backend) and then start the UI. You’ll have a list of names displayed on the UI. This list is coming from the backend.

Here’s the controller endpoint that fetches data from the backend:

@GetMapping("/home")
public  String home(Model model){
    Object[] objects =  restTemplate.getForObject("http://apihost:8080/friends", Object[].class);
    List<Object> friends = Arrays.asList(objects);
    model.addAttribute("friends", friends);
    return "index";
}

Note the hostname apihost used for making the request. This is the name of the container for the api application. We’ll talk about this when we create the docker-compose.yaml file.

 

4. Create the Packages (Jar files)

We would need to then create the jar files for both applications using the command below:

mvn clean install

You’ll have to do this for both application. After this command executes, you’ll see the jar file available in the target folder. The file names will be:

  • compose-api-0.0.1-SNAPSHOT
  • compose-ui-0.0.1-SNAPSHOT

For fun: you can create the docker files for both applications and start them separately. See the steps here.

 

5. Create the Docker Files

Now, you need to create the Docker files for both applications. The content of the docker files would almost be the same as shown below:

FROM adoptopenjdk/openjdk11:alpine-jre
EXPOSE 8080
ARG JAR_FILE=target/compose-api-0.0.1-SNAPSHOT.jar
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

For the ui app, you simply need to change the name of the jar file in line 3.

At this point,  you can create the images of both applications separately and it works just fine.

 

6. Create the Docker Compose File

You need to create the docker-compose.yml file in any of the applications.

So create a third directory that would hold the docker compose. For clarity, I have moved the Dockerfiles and jar files of each application into a separate directory. This is shown below (watch the video to see a step by step on how to do this):

Docker-compose tutorial -directory structure
Docker-compose tutorial -directory structure

 

The docker-compose file is shown below. Note that it is placed inside the docker-compose folder.

version: '3'
services:
  compose-api:
    container_name: apihost
    image: "compose-api"
    build: api/
    ports:
    - "8080:8080"
  compose-ui:
    image: "compose-ui"
    build: ui/
    ports:
    - "8081:8081"
    depends_on:
      - compose-api

 

Note two things:

  • the container name for this api is given as apihost. This is same as we used with the restTemplate
  • the compose-ui service depends on the compose-api service

You can get a clearer explanation from the video on my YouTube Channel

 

7. Fire up the Containers!

Finally, you need to fire up the containers. To do that, in your terminal, navigate to the location of the docker-compose file and run the command below:

docker-compose up --build

 

Next steps – If you came this far and got it working, thumbs up to you. What about using a database? In the next tutorial, we would add a third container, which would spin up an image of a PostgreSQL. Do subscribe to my YouTube Channel to get updates.

Thanks for learning!

 

User Avatar

kindsonthegenius

Kindson Munonye is currently completing his doctoral program in Software Engineering in Budapest University of Technology and Economics

View all posts by kindsonthegenius →

Leave a Reply

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