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:
- Java Development Kit (JDK)
- Docker
- Docker Compose
- Spring Boot
- An Integrated Development Environment (IDE) like IntelliJ IDEA, Eclipse, or VS Code.
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!
Frequently Asked Questions
- What is Docker Compose and why do I need it?
Docker Compose is a tool that allows you to define and manage multi-container Docker applications. It uses a YAML file to configure your application’s services, which can then be started all at once with a single command. Docker Compose simplifies the process of managing and orchestrating multiple containers, which is especially useful for local development and testing.To learn more about Docker Compose, have a look at Docker Compose tutorial for beginners.
- How does Docker Compose handle networking between containers?
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. - What is the difference between Docker and Docker Compose?
Docker is an open-source platform that allows you to automate the deployment, scaling, and management of applications within containers. Docker Compose, on the other hand, is a tool for defining and managing multi-container Docker applications. In other words, Docker Compose is a tool that works with Docker to simplify the management of multiple containers. - What is the purpose of the
docker-compose.yml
file?
Thedocker-compose.yml
file is used to define your multi-container application. It contains the configuration for all of the services that make up your application, and Docker Compose uses it to create and start your services. - Can I use Docker Compose for production environments?
While Docker Compose is primarily aimed at development and testing environments, it can be used in production, with some caveats. For complex, multi-node, and highly available applications, orchestration tools like Kubernetes are often recommended. It’s important to understand the trade-offs and ensure that Docker Compose meets your specific needs if you plan to use it in production. - How can I debug services in Docker Compose?
You can view the output of each service by runningdocker-compose logs <service_name>
. You can also usedocker-compose ps
to check the status of your services. If you need to inspect a running service, you can usedocker-compose exec <service_name> bash
to get a bash shell in the container. - What happens if one of my services defined in Docker Compose fails to start?
If a service fails to start, Docker Compose will stop all currently running services that were started withdocker-compose up
. You can usedocker-compose logs
to inspect the output and see why the service failed to start. - How do I stop my Docker Compose application?
You can stop your application by runningdocker-compose down
in your terminal. This command will stop and remove all the containers defined in yourdocker-compose.yml
file.