Building Multi-Container Application with Docker Compose: A Beginners’ Guide

In this tutorial, I will walk you through how to build multi-container applications using Docker Compose. This is a beginner-friendly guide that explains everything you need to know to get started.

You will learn how to create a web application with Spring Boot that uses MySQL as a database server. We will also discuss how to link containers using Docker Compose and understand networking in Docker Compose.

Prerequisites

Before we begin, make sure you have the following installed on your machine:

Creating a Web Application with Spring Boot

Spring Boot is a popular framework that simplifies the setup of Spring applications. We will use it to create a basic web application that interacts with a MySQL database.

Create a new Spring Boot application

Firstly, navigate to Spring Initializr. Here, you can easily set up a new Spring Boot project. Choose the following options:

  • Project: Maven
  • Language: Java
  • Spring Boot: The latest stable version
  • Packaging: Jar
  • Java: 11 or your current version

In the “Dependencies” section, add “Spring Web” and “Spring Data JPA”. These are essential for creating a web application and interacting with the database.

Click “Generate” to download the project. Extract it to a suitable location and open the project in your IDE.

Configure the application

In your application.properties file, add the following lines to configure the connection to your MySQL database:

spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=myusername
spring.datasource.password=mypassword
spring.jpa.hibernate.ddl-auto=update

Replace mydatabase, myusername, and mypassword with your actual database details.

How to Link Containers Using Docker Compose

Now, let’s create a Dockerfile for our Spring Boot application and define our multi-container application using Docker Compose.

Create a Dockerfile for the Spring Boot application

In the root directory of your Spring Boot application, create a new file named Dockerfile. This file will contain instructions to Docker on how to build an image of our application. Add the following to your Dockerfile:

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

Create a Docker Compose file

In the same root directory, create a new file named docker-compose.yml. This is where we define our multi-container application. Add the following to the file:

version: '3'
services:
  app:
    build: .
    ports:
      - "8080:8080"
    depends_on:
      - db
  db:
    image: mysql:8.0
    environment:
      MYSQL_DATABASE: mydatabase
      MYSQL_USER: myusername
      MYSQL_PASSWORD: mypassword
      MYSQL_ROOT_PASSWORD: rootpassword

Replace mydatabase, myusername, mypassword, and rootpassword with your actual details.

Now, your Spring Boot application and MySQL database are linked in the Docker Compose file.

Explanation of Networking in Docker Compose

Docker Compose sets up a single network for your application by default. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

In our docker-compose.yml file, the services app and db are part of the same network. This is why the app service can reach the db service at the hostname db, for instance to connect to the MySQL database.

Each service can reach each other and communicate on various ports over this network. In our case, the app service can reach the db service on the MySQL port (3306 by default), and vice versa.

The ports option in the docker-compose.yml file maps the port 8080 inside the app container to the port 8080 of the host machine. This means that the Spring Boot application is accessible on localhost:8080 on your host machine.

In a more complex application, you might want to create more networks, and you can do so by defining them in the networks section of the docker-compose.yml file. Then you can specify which network each service should join.

Conclusion

Now, you should know how to create a web application with Spring Boot that uses a MySQL database, and how to use Docker Compose to run this application in a multi-container environment. You’ve also learned how networking works in Docker Compose.

Remember, Docker Compose is a powerful tool that can simplify the process of managing and orchestrating multiple containers. It’s especially useful for local development and testing.

To run your application, navigate to the directory containing the docker-compose.yml file in your terminal and type docker-compose up. You should see Docker Compose start two containers – one for your Spring Boot application and one for your MySQL database.

Happy coding!